search.py 6.53 KB
Newer Older
1
import haystack.backends.solr_backend
fred's avatar
fred committed
2
from haystack.query import SearchQuerySet, RelatedSearchQuerySet
3
4
from haystack.constants import ID, DJANGO_CT, DJANGO_ID
from haystack.utils import get_identifier
fred's avatar
fred committed
5
from haystack.backends import EmptyResults
6

fred's avatar
fred committed
7
8
from pysolr import SolrError

9
10
from django.conf import settings

fred's avatar
fred committed
11
12
from emissions.models import Emission, Episode, NewsItem, SoundFile

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

class MoreLikeThisSearchQuerySet(SearchQuerySet):
    def more_like_this(self, model_instance, **kwargs):
        clone = self._clone()
        clone.query.more_like_this(model_instance, **kwargs)
        return clone


class CustomSolrSearchQuery(haystack.backends.solr_backend.SolrSearchQuery):
    def more_like_this(self, model_instance, **kwargs):
        self._more_like_this = True
        self._mlt_instance = model_instance

    def run_mlt(self, **kwargs):
        """Builds and executes the query. Returns a list of search results."""
        if self._more_like_this is False or self._mlt_instance is None:
            raise MoreLikeThisError("No instance was provided to determine 'More Like This' results.")

        additional_query_string = self.build_query()
        search_kwargs = {
            'start_offset': self.start_offset,
            'result_class': self.result_class,
            'models': self.models,
            'mlt.fl': 'tags',
        }

        if self.end_offset is not None:
            search_kwargs['end_offset'] = self.end_offset - self.start_offset

        results = self.backend.more_like_this(self._mlt_instance, additional_query_string, **search_kwargs)
        self._results = results.get('results', [])
        self._hit_count = results.get('hits', 0)


class CustomSolrSearchBackend(haystack.backends.solr_backend.SolrSearchBackend):
    def more_like_this(self, model_instance, additional_query_string=None,
                       start_offset=0, end_offset=None, models=None,
                       limit_to_registered_models=None, result_class=None, **kwargs):
        from haystack import connections

        # Deferred models will have a different class ("RealClass_Deferred_fieldname")
        # which won't be in our registry:
        model_klass = model_instance._meta.concrete_model

        index = connections[self.connection_alias].get_unified_index().get_index(model_klass)
        field_name = index.get_content_field()
        params = {
            'fl': '*,score',
            'mlt.fl': 'text,title',
            'mlt.qf': 'text^0.3 tags^2.0 title^1.0',
        }

        if start_offset is not None:
            params['start'] = start_offset

        if end_offset is not None:
            params['rows'] = end_offset

        narrow_queries = set()

        if limit_to_registered_models is None:
            limit_to_registered_models = getattr(settings, 'HAYSTACK_LIMIT_TO_REGISTERED_MODELS', True)

        if models and len(models):
            model_choices = sorted(['%s.%s' % (model._meta.app_label, model._meta.module_name) for model in models])
        elif limit_to_registered_models:
            # Using narrow queries, limit the results to only models handled
            # with the current routers.
            model_choices = self.build_models_list()
        else:
            model_choices = []

        if len(model_choices) > 0:
            if narrow_queries is None:
                narrow_queries = set()

            narrow_queries.add('%s:(%s)' % (DJANGO_CT, ' OR '.join(model_choices)))

        if additional_query_string:
            narrow_queries.add(additional_query_string)

        if narrow_queries:
            params['fq'] = list(narrow_queries)

        query = "%s:%s" % (ID, get_identifier(model_instance))

        try:
            raw_results = self.conn.more_like_this(query, field_name, **params)
        except (IOError, SolrError) as e:
            if not self.silently_fail:
                raise

            self.log.error("Failed to fetch More Like This from Solr for document '%s': %s", query, e)
            raw_results = EmptyResults()

        return self._process_results(raw_results, result_class=result_class)


haystack.backends.solr_backend.SolrEngine.query = CustomSolrSearchQuery
haystack.backends.solr_backend.SolrEngine.backend = CustomSolrSearchBackend

fred's avatar
fred committed
114
import haystack.views
fred's avatar
fred committed
115
from haystack.views import search_view_factory, FacetedSearchView
fred's avatar
fred committed
116
from haystack.forms import FacetedSearchForm, SearchForm
fred's avatar
fred committed
117
118


fred's avatar
fred committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class GlobalSearchForm(FacetedSearchForm):
    def no_query_found(self):
        sqs = super(GlobalSearchForm, self).no_query_found()
        if self.selected_facets:
            sqs = self.searchqueryset.all()
            for facet in self.selected_facets:
                if ":" not in facet:
                    continue
                field, value = facet.split(":", 1)
                if value:
                    sqs = sqs.narrow(u'%s:"%s"' % (field, sqs.query.clean(value)))
        return sqs


fred's avatar
fred committed
133
134
135
class SearchView(FacetedSearchView):
    def extra_context(self):
        context = super(SearchView, self).extra_context()
fred's avatar
fred committed
136
        context['sectionName'] = 'Search'
fred's avatar
fred committed
137
138
139
140
141
142
143
144
        context['selected_categories'] = [
                x.split(':', 1)[1] for x in self.request.GET.getlist('selected_facets')
                if x.startswith('categories_exact')]
        context['selected_tags'] = [
                x.split(':', 1)[1] for x in self.request.GET.getlist('selected_facets')
                if x.startswith('tags_exact')]
        return context

fred's avatar
fred committed
145
sqs = SearchQuerySet().models(Emission, Episode, NewsItem).facet('categories').facet('tags')
fred's avatar
fred committed
146
147

view = search_view_factory(SearchView,
fred's avatar
fred committed
148
        form_class=GlobalSearchForm,
fred's avatar
fred committed
149
        searchqueryset=sqs)
fred's avatar
fred committed
150
151
152



fred's avatar
fred committed
153
class ListenArchivesForm(FacetedSearchForm):
fred's avatar
fred committed
154
155
156
157
158
159
160
161
    def no_query_found(self):
        return self.searchqueryset.all()

    def search(self):
        sqs = super(ListenArchivesForm, self).search()
        return sqs.load_all()


fred's avatar
fred committed
162
class ListenArchivesView(FacetedSearchView):
fred's avatar
fred committed
163
164
165
    template = 'listen/archives.html'

    def __init__(self):
fred's avatar
fred committed
166
        sqs = RelatedSearchQuerySet().models(SoundFile).facet('categories').order_by('-date')
fred's avatar
fred committed
167
168
169
170
171
172
        super(ListenArchivesView, self).__init__(searchqueryset=sqs,
                form_class=ListenArchivesForm, results_per_page=20)

    def extra_context(self):
        context = super(ListenArchivesView, self).extra_context()
        context['sectionName'] = "Listen"
fred's avatar
fred committed
173
174
175
176
        context['selected_categories'] = [
                x.split(':', 1)[1] for x in self.request.GET.getlist('selected_facets')
                if x.startswith('categories_exact')]
        context['facets']['fields']['categories'].sort()
fred's avatar
fred committed
177
178
179
        return context

listenArchives = search_view_factory(ListenArchivesView)