Ticket #2707: xapian.py.patch

File xapian.py.patch, 2.3 KB (added by sdm@…, 8 years ago)

New version of Xapian backend (need query.py.patch)

  • django/contrib/search/xapian.py

     
    44import xapwrap.document
    55from itertools import imap
    66
    7 from base import Indexer, ResultSet
     7from query import ResultSet, Hit
     8from base import Indexer
    89
    910# TODO: This is incomplete.
    1011
     
    1718        else:
    1819            update_queue = documents
    1920
    20         for row in documents:
     21        for row in update_queue:
    2122            keys = []
     23            text_fields = []
    2224            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())
    2629            idx.index(d)
    2730        idx.close()
    2831
    2932    def search(self, query, order_by='RELEVANCE'):
    30         idx = Index(self.path)
     33        idx = xapwrap.index.Index(self.path)
    3134        if order_by == 'RELEVANCE':
    3235            results = idx.search(query, sortByRelevence=True)
    3336        else:
     
    3740            while order_by[0] in '+-':
    3841                order_by = order_by[1:]
    3942            results = idx.search(query, order_by, sortAscending=ascending)
    40         return XapianResultSet(results)
     43        return XapianResultSet(results,self)
    4144
    4245
    4346class XapianResultSet(ResultSet):
     
    4952        return len(self._hits)
    5053
    5154    def __iter__(self):
    52         for hit in self._hits):
    53             yield XapianHit(hit, self._indexer)
     55        for hit in self._hits:
     56            yield XapianHit(hit,self._indexer)
    5457
    5558
    56 class XapianHit(object):
     59class XapianHit(Hit):
    5760    def get_pk(self):
    58         return self.data['pk']
     61        # FIXME: Hardcoded 'pk' field.
     62        return self.data['uid']
    5963
     64    def __getitem__(self, item):
     65        return self.data.__getitem__(item)
     66
    6067    def get_score(self):
    6168        return self.data['score']
    6269
    6370    score = property(get_score)
    64 
Back to Top