summaryrefslogtreecommitdiff
path: root/django/contrib/search/xapian.py
blob: a1dbdbb8e9840f98f7c8b1dc97cb38a714d26e4e (plain)
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
from django.db import models
from datetime import datetime
import xapwrap.index
import xapwrap.document
from itertools import imap

from base import Indexer, ResultSet

# TODO: This is incomplete.

class XapianIndexer(Indexer):
    def update(self, documents=None):
        idx = xapwrap.index.Index(self.path, True)

        if documents is None:
            update_queue = self.model.objects.all()
        else:
            update_queue = documents

        for row in documents:
            keys = []
            for name, field in self.attr_fields.iteritems():
                keys.append(xapwrap.document.SortKey(name, getattr(self.model, field.name)))

            d = xapwrap.document.Document(textFields=fields, sortFields=keys, uid=row._get_pk_val())
            idx.index(d)
        idx.close()

    def search(self, query, order_by='RELEVANCE'):
        idx = Index(self.path)
        if order_by == 'RELEVANCE':
            results = idx.search(query, sortByRelevence=True)
        else:
            ascending = True
            if isinstance(order_by, basestring) and order_by.startswith('-'):
                ascending = False
            while order_by[0] in '+-':
                order_by = order_by[1:]
            results = idx.search(query, order_by, sortAscending=ascending)
        return XapianResultSet(results)


class XapianResultSet(ResultSet):
    def __init__(self, hits, indexer):
        self._hits = hits
        self._indexer = indexer

    def __len__(self):
        return len(self._hits)

    def __iter__(self):
        for hit in self._hits):
            yield XapianHit(hit, self._indexer)


class XapianHit(object):
    def get_pk(self):
        return self.data['pk']

    def get_score(self):
        return self.data['score']

    score = property(get_score)