srchub

srchub Git Source Tree


Root/pluf/src/Pluf/Search.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
# ***** BEGIN LICENSE BLOCK *****
# This file is part of Plume Framework, a simple PHP Application Framework.
# Copyright (C) 2001-2007 Loic d'Anterroches and contributors.
#
# Plume Framework is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# Plume Framework is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# ***** END LICENSE BLOCK ***** */
 
/**
 * Class implementing a small search engine.
 *
 * Ideal for a small website with up to 100,000 documents.
 */
class Pluf_Search
{
    /**
     * Search.
     *
     * Returns an array of array with model_class, model_id and
     * score. The list is already sorted by score descending.
     *
     * You can then filter the list as you wish with another set of
     * weights.
     *
     * @param string Query string.
     * @return array Results.
     */
    public static function search($query, $stemmer='Pluf_Text_Stemmer_Porter')
    {
        $query = Pluf_Text::cleanString(html_entity_decode($query, ENT_QUOTES, 'UTF-8'));
        $words = Pluf_Text::tokenize($query);
        if ($stemmer != null) {
            $words = self::stem($words, $stemmer);
        }
        $words_flat = array();
        foreach ($words as $word=>$c) {
            $words_flat[] = $word;
        }
        $word_ids = self::getWordIds($words_flat);
        if (in_array(null, $word_ids)) {
            return array();
        }
        return self::searchDocuments($word_ids);
    }
 
    /**
     * Stem the words with the given stemmer.
     */
    public static function stem($words, $stemmer)
    {
        $nwords = array();
        foreach ($words as $word => $occ) {
            $word = call_user_func(array($stemmer, 'stem'), $word);
            if (isset($nwords[$word])) {
                $nwords[$word] += $occ;
            } else {
                $nwords[$word] = $occ;
            }
        }
        return $nwords;
    }
 
    /**
     * Search documents.
     *
     * Only the total of the ponderated occurences is used to sort the
     * results.
     *
     * @param array Ids.
     * @return array Sorted by score, returns model_class, model_id and score.
     */
    public static function searchDocuments($wids)
    {
        $db =& Pluf::db();
        $gocc = new Pluf_Search_Occ();
        $where = array();
        foreach ($wids as $id) {
            $where[] = $db->qn('word').'='.(int)$id;
        }
        $select = 'SELECT model_class, model_id, SUM(pondocc) AS score FROM '.$gocc->getSqlTable().' WHERE '.implode(' OR ', $where).' GROUP BY model_class, model_id HAVING COUNT(*)='.count($wids).' ORDER BY score DESC';
        return $db->select($select);
    }
 
    /**
     * Get the id of each word.
     *
     * @param array Words
     * @return array Ids, null if no matching word.
     */
    public static function getWordIds($words)
    {
        $ids = array();
        $gword = new Pluf_Search_Word();
        foreach ($words as $word) {
            $sql = new Pluf_SQL('word=%s', array($word));
            $l = $gword->getList(array('filter' => $sql->gen()));
            if ($l->count() > 0) {
                $ids[] = $l[0]->id;
            } else {
                $ids[] = null;
            }
        }
        return $ids;
    }
 
    /**
     * Index a document.
     *
     * The document must provide a method _toIndex() returning the
     * document as a string for indexation. The string must be clean
     * and will simply be tokenized by Pluf_Text::tokenize().
     *
     * So a recommended way to clean it at the end is to remove all
     * the HTML tags and then run the following on it:
     *
     *  return Pluf_Text::cleanString(html_entity_decode($string,
     *                                ENT_QUOTES, 'UTF-8'));
     *
     * Indexing is resource intensive so it is recommanded to run the
     * indexing in an asynchronous way. When you save a resource to be
     * indexed, just write a log "need to index resource x" and then
     * you can every few minutes index the resources. Nobody care if
     * your index is not perfectly fresh, but your end users care if
     * it takes 0.6s to get back the page instead of 0.1s.
     *
     * Take 500 average documents, index them while counting the total
     * time it takes to index. Divide by 500 and if the result is more
     * than 0.1s, use a log/queue.
     *
     * FIXME: Concurrency problem if you index at the same time the same doc.
     *
     * @param Pluf_Model Document to index.
     * @param Stemmer used. ('Pluf_Text_Stemmer_Porter')
     * @return array Statistics.
     */
    public static function index($doc, $stemmer='Pluf_Text_Stemmer_Porter')
    {
        $words = Pluf_Text::tokenize($doc->_toIndex());
        if ($stemmer != null) {
            $words = self::stem($words, $stemmer);
        }
        // Get the total number of words.
        $total = 0.0;
        $words_flat = array();
        foreach ($words as $word => $occ) {
            $total += (float) $occ;
            $words_flat[] = $word;
        }
        // Drop the last indexation.
        $gocc = new Pluf_Search_Occ();
        $sql = new Pluf_SQL('DELETE FROM '.$gocc->getSqlTable().' WHERE model_class=%s AND model_id=%s', array($doc->_model, $doc->id));
        $db =& Pluf::db();
        $db->execute($sql->gen());
        // Get the ids for each word.
        $ids = self::getWordIds($words_flat);
        // Insert a new word for the missing words and add the occ.
        $n = count($ids);
        $new_words = 0;
        $done = array();
        for ($i=0;$i<$n;$i++) {
            if ($ids[$i] === null) {
                $word = new Pluf_Search_Word();
                $word->word = $words_flat[$i];
                try {
                    $word->create();
                    $ids[$i] = $word->id;
                } catch (Exception $e) {
                    // most likely concurrent addition of a word, try
                    // to read it.
                    $_ids = self::getWordIds(array($words_flat[$i]));
                    if ($_ids[0] !== null) {
                        // if we miss it here, just forget about it
                        $ids[$i] = $_ids[0];
                    }
                }
                $new_words++;
            }
            if (isset($done[$ids[$i]])) {
                continue;
            }
            $done[$ids[$i]] = true;
            $occ = new Pluf_Search_Occ();
            $occ->word = new Pluf_Search_Word($ids[$i]);
            $occ->model_class = $doc->_model;
            $occ->model_id = $doc->id;
            $occ->occ = $words[$words_flat[$i]];
            $occ->pondocc = $words[$words_flat[$i]]/$total;
            $occ->create();
        }
        // update the stats
        $sql = new Pluf_SQL('model_class=%s AND model_id=%s',
                            array($doc->_model, $doc->id));
        $last_index = Pluf::factory('Pluf_Search_Stats')->getList(array('filter' => $sql->gen()));
        if ($last_index->count() == 0) {
            $stats = new Pluf_Search_Stats();
            $stats->model_class = $doc->_model;
            $stats->model_id = $doc->id;
            $stats->indexations = 1;
            $stats->create();
        } else {
            $last_index[0]->indexations += 1;
            $last_index[0]->update();
        }
        return array('total' => $total, 'new' => $new_words, 'unique'=>$n);
    }
}

Archive Download this file

Branches

Number of commits:
Page rendered in 0.27446s using 11 queries.