|
| 1 | +''' This implements the full-text indexer over two RDBMS tables. The first |
| 2 | +is a mapping of words to occurance IDs. The second maps the IDs to (Class, |
| 3 | +propname, itemid) instances. |
| 4 | +''' |
| 5 | +import re |
| 6 | + |
| 7 | +from indexer_dbm import Indexer |
| 8 | + |
| 9 | +class Indexer(Indexer): |
| 10 | + disallows = {'THE':1, 'THIS':1, 'ZZZ':1, 'THAT':1, 'WITH':1} |
| 11 | + def __init__(self, db): |
| 12 | + self.db = db |
| 13 | + self.reindex = 0 |
| 14 | + |
| 15 | + def close(self): |
| 16 | + '''close the indexing database''' |
| 17 | + # just nuke the circular reference |
| 18 | + self.db = None |
| 19 | + |
| 20 | + def force_reindex(self): |
| 21 | + '''Force a reindexing of the database. This essentially |
| 22 | + empties the tables ids and index and sets a flag so |
| 23 | + that the databases are reindexed''' |
| 24 | + self.reindex = 1 |
| 25 | + |
| 26 | + def should_reindex(self): |
| 27 | + '''returns True if the indexes need to be rebuilt''' |
| 28 | + return self.reindex |
| 29 | + |
| 30 | + def add_text(self, identifier, text, mime_type='text/plain'): |
| 31 | + ''' "identifier" is (classname, itemid, property) ''' |
| 32 | + if mime_type != 'text/plain': |
| 33 | + return |
| 34 | + |
| 35 | + # first, find the id of the (classname, itemid, property) |
| 36 | + a = self.db.arg |
| 37 | + sql = 'select _textid from _textids where _class=%s and '\ |
| 38 | + '_itemid=%s and _prop=%s'%(a, a, a) |
| 39 | + self.db.cursor.execute(sql, identifier) |
| 40 | + r = self.db.cursor.fetchone() |
| 41 | + if not r: |
| 42 | + id = self.db.newid('_textids') |
| 43 | + sql = 'insert into _textids (_textid, _class, _itemid, _prop)'\ |
| 44 | + ' values (%s, %s, %s, %s)'%(a, a, a, a) |
| 45 | + self.db.cursor.execute(sql, (id, ) + identifier) |
| 46 | + else: |
| 47 | + id = int(r[0]) |
| 48 | + # clear out any existing indexed values |
| 49 | + sql = 'delete from _words where _textid=%s'%a |
| 50 | + self.db.cursor.execute(sql, (id, )) |
| 51 | + |
| 52 | + # ok, find all the words in the text |
| 53 | + wordlist = re.findall(r'\b\w{2,25}\b', str(text).upper()) |
| 54 | + words = {} |
| 55 | + for word in wordlist: |
| 56 | + if not self.disallows.has_key(word): |
| 57 | + words[word] = 1 |
| 58 | + words = words.keys() |
| 59 | + |
| 60 | + # for each word, add an entry in the db |
| 61 | + for word in words: |
| 62 | + # don't dupe |
| 63 | + sql = 'select * from _words where _word=%s and _textid=%s'%(a, a) |
| 64 | + self.db.cursor.execute(sql, (word, id)) |
| 65 | + if self.db.cursor.fetchall(): |
| 66 | + continue |
| 67 | + sql = 'insert into _words (_word, _textid) values (%s, %s)'%(a, a) |
| 68 | + self.db.cursor.execute(sql, (word, id)) |
| 69 | + |
| 70 | + def find(self, wordlist): |
| 71 | + '''look up all the words in the wordlist. |
| 72 | + If none are found return an empty dictionary |
| 73 | + * more rules here |
| 74 | + ''' |
| 75 | + l = [word.upper() for word in wordlist if 26 > len(word) > 2] |
| 76 | + |
| 77 | + a = ','.join([self.db.arg] * len(l)) |
| 78 | + sql = 'select distinct(_textid) from _words where _word in (%s)'%a |
| 79 | + self.db.cursor.execute(sql, tuple(l)) |
| 80 | + r = self.db.cursor.fetchall() |
| 81 | + if not r: |
| 82 | + return {} |
| 83 | + a = ','.join([self.db.arg] * len(r)) |
| 84 | + sql = 'select _class, _itemid, _prop from _textids '\ |
| 85 | + 'where _textid in (%s)'%a |
| 86 | + self.db.cursor.execute(sql, tuple([int(id) for (id,) in r])) |
| 87 | + # self.search_index has the results as {some id: identifier} ... |
| 88 | + # sigh |
| 89 | + r = {} |
| 90 | + k = 0 |
| 91 | + for c,n,p in self.db.cursor.fetchall(): |
| 92 | + key = (str(c), str(n), str(p)) |
| 93 | + r[k] = key |
| 94 | + k += 1 |
| 95 | + return r |
| 96 | + |
| 97 | + def save_index(self): |
| 98 | + # the normal RDBMS backend transaction mechanisms will handle this |
| 99 | + pass |
| 100 | + |
| 101 | + def rollback(self): |
| 102 | + # the normal RDBMS backend transaction mechanisms will handle this |
| 103 | + pass |
| 104 | + |
0 commit comments