Ticket #2707: xapian.py.patch
File xapian.py.patch, 2.3 KB (added by , 18 years ago) |
---|
-
django/contrib/search/xapian.py
4 4 import xapwrap.document 5 5 from itertools import imap 6 6 7 from base import Indexer, ResultSet 7 from query import ResultSet, Hit 8 from base import Indexer 8 9 9 10 # TODO: This is incomplete. 10 11 … … 17 18 else: 18 19 update_queue = documents 19 20 20 for row in documents:21 for row in update_queue: 21 22 keys = [] 23 text_fields = [] 22 24 for name, field in self.attr_fields.iteritems(): 23 keys.append(xapwrap.document.SortKey(name, getattr(self.model, field.name))) 24 25 d = xapwrap.document.Document(textFields=fields, sortFields=keys, uid=row._get_pk_val()) 25 field_name = str(getattr(row, field.name)) 26 #keys.append(xapwrap.document.SortKey(name, field_name)) 27 text_fields.append(xapwrap.document.TextField(name,field_name)) 28 d = xapwrap.document.Document(textFields=text_fields,sortFields=keys, uid=row._get_pk_val()) 26 29 idx.index(d) 27 30 idx.close() 28 31 29 32 def search(self, query, order_by='RELEVANCE'): 30 idx = Index(self.path)33 idx = xapwrap.index.Index(self.path) 31 34 if order_by == 'RELEVANCE': 32 35 results = idx.search(query, sortByRelevence=True) 33 36 else: … … 37 40 while order_by[0] in '+-': 38 41 order_by = order_by[1:] 39 42 results = idx.search(query, order_by, sortAscending=ascending) 40 return XapianResultSet(results )43 return XapianResultSet(results,self) 41 44 42 45 43 46 class XapianResultSet(ResultSet): … … 49 52 return len(self._hits) 50 53 51 54 def __iter__(self): 52 for hit in self._hits ):53 yield XapianHit(hit, 55 for hit in self._hits: 56 yield XapianHit(hit,self._indexer) 54 57 55 58 56 class XapianHit( object):59 class XapianHit(Hit): 57 60 def get_pk(self): 58 return self.data['pk'] 61 # FIXME: Hardcoded 'pk' field. 62 return self.data['uid'] 59 63 64 def __getitem__(self, item): 65 return self.data.__getitem__(item) 66 60 67 def get_score(self): 61 68 return self.data['score'] 62 69 63 70 score = property(get_score) 64