~widelands-dev/widelands-website/django_staticfiles

462.3.1 by franku
first functioning search with haystack and whoosh; working: wlmaps, pybb.topic, pybb.post
1
from haystack import indexes
2
from pybb.models import Topic, Post
3
4
5
class TopicIndex(indexes.SearchIndex, indexes.Indexable):
6
462.3.17 by franku
put more information in the index for pybb
7
    """Create a search index. Changes made here need to be reindexed. Defined
8
    fields are stored in the index, so when displaying the result the data is
9
    read from the index and do not hit the database.
462.3.1 by franku
first functioning search with haystack and whoosh; working: wlmaps, pybb.topic, pybb.post
10
11
    Except the 'text' field all defined fields will be in the index.
12
13
    'text' indicates the template where the concatenated data
14
           is gathered and the search runs over.
15
16
    'date' is the field which is used for sorting
17
18
    """
19
20
    text = indexes.CharField(document=True, use_template=True)
21
    date = indexes.DateTimeField(model_attr='created')
22
    name = indexes.CharField(model_attr='name')
462.3.17 by franku
put more information in the index for pybb
23
    # Following fields get stored in the index but are not be used for indexing
24
    # This avoids hitting the database when the results are rendered
25
    user = indexes.CharField(model_attr='user', indexed=False)
26
    topic_link = indexes.CharField(model_attr='get_absolute_url', indexed=False)
462.3.1 by franku
first functioning search with haystack and whoosh; working: wlmaps, pybb.topic, pybb.post
27
28
    def get_model(self):
29
        return Topic
30
462.3.17 by franku
put more information in the index for pybb
31
    def index_queryset(self, using=None):
32
        """Do not index hidden topics."""
33
        return self.get_model().objects.exclude(posts__hidden=True)
34
462.3.6 by franku
make running update_index faster; added highlighting; adjust search indexes
35
    def get_updated_field(self):
462.3.17 by franku
put more information in the index for pybb
36
        return 'updated'
37
462.3.1 by franku
first functioning search with haystack and whoosh; working: wlmaps, pybb.topic, pybb.post
38
39
class PostIndex(indexes.SearchIndex, indexes.Indexable):
40
41
    text = indexes.CharField(document=True, use_template=True)
42
    date = indexes.DateTimeField(model_attr='created')
43
    body_text = indexes.CharField(model_attr='body_text')
462.3.17 by franku
put more information in the index for pybb
44
    # Following fields get stored in the index but are not be used for indexing
45
    # This avoids hitting the database when the results are rendered
46
    user = indexes.CharField(model_attr='user', indexed='false')
47
    post_link = indexes.CharField(
48
        model_attr='get_absolute_url', indexed='false')
462.3.1 by franku
first functioning search with haystack and whoosh; working: wlmaps, pybb.topic, pybb.post
49
50
    def get_model(self):
51
        return Post
462.3.7 by franku
added encyclopedia workers; own templates for the search view
52
462.3.17 by franku
put more information in the index for pybb
53
    def index_queryset(self, using=None):
54
        """Do not index hidden posts."""
55
        return self.get_model().objects.exclude(hidden=True)
56
462.3.6 by franku
make running update_index faster; added highlighting; adjust search indexes
57
    def get_updated_field(self):
462.3.17 by franku
put more information in the index for pybb
58
        return 'created'