|
| 1 | +''' This implements the full-text indexer using Whoosh. |
| 2 | +''' |
| 3 | +import re, os |
| 4 | + |
| 5 | +from whoosh import fields, qparser, index, query, analysis |
| 6 | + |
| 7 | +from roundup.backends.indexer_common import Indexer as IndexerBase |
| 8 | + |
| 9 | +class Indexer(IndexerBase): |
| 10 | + def __init__(self, db): |
| 11 | + IndexerBase.__init__(self, db) |
| 12 | + self.db_path = db.config.DATABASE |
| 13 | + self.reindex = 0 |
| 14 | + self.writer = None |
| 15 | + self.index = None |
| 16 | + self.deleted = set() |
| 17 | + |
| 18 | + def _get_index(self): |
| 19 | + if self.index is None: |
| 20 | + path = os.path.join(self.db_path, 'whoosh-index') |
| 21 | + if not os.path.exists(path): |
| 22 | + # StandardAnalyzer lowercases all words and configure it to |
| 23 | + # block stopwords and words with lengths not between |
| 24 | + # self.minlength and self.maxlength from indexer_common |
| 25 | + stopfilter = analysis.StandardAnalyzer( #stoplist=self.stopwords, |
| 26 | + minsize=self.minlength, |
| 27 | + maxsize=self.maxlength) |
| 28 | + os.mkdir(path) |
| 29 | + schema = fields.Schema(identifier=fields.ID(stored=True, |
| 30 | + unique=True), |
| 31 | + content=fields.TEXT(analyzer=stopfilter)) |
| 32 | + index.create_in(path, schema) |
| 33 | + self.index = index.open_dir(path) |
| 34 | + return self.index |
| 35 | + |
| 36 | + def save_index(self): |
| 37 | + '''Save the changes to the index.''' |
| 38 | + if not self.writer: |
| 39 | + return |
| 40 | + self.writer.commit() |
| 41 | + self.deleted = set() |
| 42 | + self.writer = None |
| 43 | + |
| 44 | + def close(self): |
| 45 | + '''close the indexing database''' |
| 46 | + pass |
| 47 | + |
| 48 | + def rollback(self): |
| 49 | + if not self.writer: |
| 50 | + return |
| 51 | + self.writer.cancel() |
| 52 | + self.deleted = set() |
| 53 | + self.writer = None |
| 54 | + |
| 55 | + def force_reindex(self): |
| 56 | + '''Force a reindexing of the database. This essentially |
| 57 | + empties the tables ids and index and sets a flag so |
| 58 | + that the databases are reindexed''' |
| 59 | + self.reindex = 1 |
| 60 | + |
| 61 | + def should_reindex(self): |
| 62 | + '''returns True if the indexes need to be rebuilt''' |
| 63 | + return self.reindex |
| 64 | + |
| 65 | + def _get_writer(self): |
| 66 | + if self.writer is None: |
| 67 | + self.writer = self._get_index().writer() |
| 68 | + return self.writer |
| 69 | + |
| 70 | + def _get_searcher(self): |
| 71 | + return self._get_index().searcher() |
| 72 | + |
| 73 | + def add_text(self, identifier, text, mime_type='text/plain'): |
| 74 | + ''' "identifier" is (classname, itemid, property) ''' |
| 75 | + if mime_type != 'text/plain': |
| 76 | + return |
| 77 | + |
| 78 | + if not text: |
| 79 | + text = u'' |
| 80 | + |
| 81 | + if not isinstance(text, unicode): |
| 82 | + text = unicode(text, "utf-8", "replace") |
| 83 | + |
| 84 | + # We use the identifier twice: once in the actual "text" being |
| 85 | + # indexed so we can search on it, and again as the "data" being |
| 86 | + # indexed so we know what we're matching when we get results |
| 87 | + identifier = u"%s:%s:%s"%identifier |
| 88 | + |
| 89 | + # FIXME need to enhance this to handle the whoosh.store.LockError |
| 90 | + # that maybe raised if there is already another process with a lock. |
| 91 | + writer = self._get_writer() |
| 92 | + |
| 93 | + # Whoosh gets upset if a document is deleted twice in one transaction, |
| 94 | + # so we keep a list of the documents we have so far deleted to make |
| 95 | + # sure that we only delete them once. |
| 96 | + if identifier not in self.deleted: |
| 97 | + searcher = self._get_searcher() |
| 98 | + results = searcher.search(query.Term("identifier", identifier)) |
| 99 | + if len(results) > 0: |
| 100 | + writer.delete_by_term("identifier", identifier) |
| 101 | + self.deleted.add(identifier) |
| 102 | + |
| 103 | + # Note: use '.lower()' because it seems like Whoosh gets |
| 104 | + # better results that way. |
| 105 | + writer.add_document(identifier=identifier, content=text) |
| 106 | + self.save_index() |
| 107 | + |
| 108 | + def find(self, wordlist): |
| 109 | + '''look up all the words in the wordlist. |
| 110 | + If none are found return an empty dictionary |
| 111 | + * more rules here |
| 112 | + ''' |
| 113 | + |
| 114 | + wordlist = [ word for word in wordlist |
| 115 | + if (self.minlength <= len(word) <= self.maxlength) and |
| 116 | + not self.is_stopword(word.upper()) ] |
| 117 | + |
| 118 | + if not wordlist: |
| 119 | + return {} |
| 120 | + |
| 121 | + searcher = self._get_searcher() |
| 122 | + q = query.And([ query.FuzzyTerm("content", word.lower()) |
| 123 | + for word in wordlist ]) |
| 124 | + |
| 125 | + results = searcher.search(q, limit=None) |
| 126 | + |
| 127 | + return [tuple(result["identifier"].split(':')) |
| 128 | + for result in results] |
| 129 | + |
0 commit comments