File: __init__.py

package info (click to toggle)
django-haystack 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: python: 23,475; xml: 1,708; sh: 74; makefile: 71
file content (1109 lines) | stat: -rw-r--r-- 36,622 bytes parent folder | download | duplicates (2)
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
import copy
from copy import deepcopy
from time import time

from django.conf import settings
from django.db.models import Q
from django.db.models.base import ModelBase
from django.utils import tree
from django.utils.encoding import force_str

from haystack.constants import DEFAULT_ALIAS, FILTER_SEPARATOR, VALID_FILTERS
from haystack.exceptions import FacetingError, MoreLikeThisError
from haystack.models import SearchResult
from haystack.utils import get_model_ct
from haystack.utils.loading import UnifiedIndex

VALID_GAPS = ["year", "month", "day", "hour", "minute", "second"]

SPELLING_SUGGESTION_HAS_NOT_RUN = object()


def log_query(func):
    """
    A decorator for pseudo-logging search queries. Used in the ``SearchBackend``
    to wrap the ``search`` method.
    """

    def wrapper(obj, query_string, *args, **kwargs):
        start = time()

        try:
            return func(obj, query_string, *args, **kwargs)
        finally:
            stop = time()

            if settings.DEBUG:
                from haystack import connections

                connections[obj.connection_alias].queries.append(
                    {
                        "query_string": query_string,
                        "additional_args": args,
                        "additional_kwargs": kwargs,
                        "time": "%.3f" % (stop - start),
                        "start": start,
                        "stop": stop,
                    }
                )

    return wrapper


class EmptyResults:
    hits = 0
    docs = []

    def __len__(self):
        return 0

    def __getitem__(self, k):
        if isinstance(k, slice):
            return []
        else:
            raise IndexError("It's not here.")


class BaseSearchBackend:
    """
    Abstract search engine base class.
    """

    # Backends should include their own reserved words/characters.
    RESERVED_WORDS = []
    RESERVED_CHARACTERS = []

    def __init__(self, connection_alias, **connection_options):
        self.connection_alias = connection_alias
        self.timeout = connection_options.get("TIMEOUT", 10)
        self.include_spelling = connection_options.get("INCLUDE_SPELLING", False)
        self.batch_size = connection_options.get("BATCH_SIZE", 1000)
        self.silently_fail = connection_options.get("SILENTLY_FAIL", True)
        self.distance_available = connection_options.get("DISTANCE_AVAILABLE", False)

    def update(self, index, iterable, commit=True):
        """
        Updates the backend when given a SearchIndex and a collection of
        documents.

        This method MUST be implemented by each backend, as it will be highly
        specific to each one.
        """
        raise NotImplementedError

    def remove(self, obj_or_string):
        """
        Removes a document/object from the backend. Can be either a model
        instance or the identifier (i.e. ``app_name.model_name.id``) in the
        event the object no longer exists.

        This method MUST be implemented by each backend, as it will be highly
        specific to each one.
        """
        raise NotImplementedError

    def clear(self, models=None, commit=True):
        """
        Clears the backend of all documents/objects for a collection of models.

        This method MUST be implemented by each backend, as it will be highly
        specific to each one.
        """
        raise NotImplementedError

    @log_query
    def search(self, query_string, **kwargs):
        """
        Takes a query to search on and returns dictionary.

        The query should be a string that is appropriate syntax for the backend.

        The returned dictionary should contain the keys 'results' and 'hits'.
        The 'results' value should be an iterable of populated SearchResult
        objects. The 'hits' should be an integer count of the number of matched
        results the search backend found.

        This method MUST be implemented by each backend, as it will be highly
        specific to each one.
        """
        raise NotImplementedError

    def build_search_kwargs(
        self,
        query_string,
        sort_by=None,
        start_offset=0,
        end_offset=None,
        fields="",
        highlight=False,
        facets=None,
        date_facets=None,
        query_facets=None,
        narrow_queries=None,
        spelling_query=None,
        within=None,
        dwithin=None,
        distance_point=None,
        models=None,
        limit_to_registered_models=None,
        result_class=None,
        **extra_kwargs
    ):
        # A convenience method most backends should include in order to make
        # extension easier.
        raise NotImplementedError

    def prep_value(self, value):
        """
        Hook to give the backend a chance to prep an attribute value before
        sending it to the search engine. By default, just force it to unicode.
        """
        return force_str(value)

    def more_like_this(
        self, model_instance, additional_query_string=None, result_class=None
    ):
        """
        Takes a model object and returns results the backend thinks are similar.

        This method MUST be implemented by each backend, as it will be highly
        specific to each one.
        """
        raise NotImplementedError(
            "Subclasses must provide a way to fetch similar record via the 'more_like_this' method if supported by the backend."
        )

    def extract_file_contents(self, file_obj):
        """
        Hook to allow backends which support rich-content types such as PDF,
        Word, etc. extraction to process the provided file object and return
        the contents for indexing

        Returns None if metadata cannot be extracted; otherwise returns a
        dictionary containing at least two keys:

            :contents:
                        Extracted full-text content, if applicable
            :metadata:
                        key:value pairs of text strings
        """

        raise NotImplementedError(
            "Subclasses must provide a way to extract metadata via the 'extract' method if supported by the backend."
        )

    def build_schema(self, fields):
        """
        Takes a dictionary of fields and returns schema information.

        This method MUST be implemented by each backend, as it will be highly
        specific to each one.
        """
        raise NotImplementedError(
            "Subclasses must provide a way to build their schema."
        )

    def build_models_list(self):
        """
        Builds a list of models for searching.

        The ``search`` method should use this and the ``django_ct`` field to
        narrow the results (unless the user indicates not to). This helps ignore
        any results that are not currently handled models and ensures
        consistent caching.
        """
        from haystack import connections

        models = []

        for model in (
            connections[self.connection_alias].get_unified_index().get_indexed_models()
        ):
            models.append(get_model_ct(model))

        return models


# Alias for easy loading within SearchQuery objects.
SearchBackend = BaseSearchBackend


class SearchNode(tree.Node):
    """
    Manages an individual condition within a query.

    Most often, this will be a lookup to ensure that a certain word or phrase
    appears in the documents being indexed. However, it also supports filtering
    types (such as 'lt', 'gt', 'in' and others) for more complex lookups.

    This object creates a tree, with children being a list of either more
    ``SQ`` objects or the expressions/values themselves.
    """

    AND = "AND"
    OR = "OR"
    default = AND

    # Start compat. Django 1.6 changed how ``tree.Node`` works, so we're going
    # to patch back in the original implementation until time to rewrite this
    # presents itself.
    # See https://github.com/django/django/commit/d3f00bd.

    def __init__(self, children=None, connector=None, negated=False):
        """
        Constructs a new Node. If no connector is given, the default will be
        used.

        Warning: You probably don't want to pass in the 'negated' parameter. It
        is NOT the same as constructing a node and calling negate() on the
        result.
        """
        self.children = children and children[:] or []
        self.connector = connector or self.default
        self.subtree_parents = []
        self.negated = negated

    # We need this because of django.db.models.query_utils.Q. Q. __init__() is
    # problematic, but it is a natural Node subclass in all other respects.
    def _new_instance(cls, children=None, connector=None, negated=False):
        """
        This is called to create a new instance of this class when we need new
        Nodes (or subclasses) in the internal code in this class. Normally, it
        just shadows __init__(). However, subclasses with an __init__ signature
        that is not an extension of Node.__init__ might need to implement this
        method to allow a Node to create a new instance of them (if they have
        any extra setting up to do).
        """
        obj = SearchNode(children, connector, negated)
        obj.__class__ = cls
        return obj

    _new_instance = classmethod(_new_instance)

    def __str__(self):
        if self.negated:
            return "(NOT (%s: %s))" % (
                self.connector,
                ", ".join([str(c) for c in self.children]),
            )
        return "(%s: %s)" % (self.connector, ", ".join([str(c) for c in self.children]))

    def __deepcopy__(self, memodict):
        """
        Utility method used by copy.deepcopy().
        """
        obj = SearchNode(connector=self.connector, negated=self.negated)
        obj.__class__ = self.__class__
        obj.children = copy.deepcopy(self.children, memodict)
        obj.subtree_parents = copy.deepcopy(self.subtree_parents, memodict)
        return obj

    def __len__(self):
        """
        The size of a node if the number of children it has.
        """
        return len(self.children)

    def __bool__(self):
        """
        For truth value testing.
        """
        return bool(self.children)

    def __contains__(self, other):
        """
        Returns True is 'other' is a direct child of this instance.
        """
        return other in self.children

    def add(self, node, conn_type):
        """
        Adds a new node to the tree. If the conn_type is the same as the root's
        current connector type, the node is added to the first level.
        Otherwise, the whole tree is pushed down one level and a new root
        connector is created, connecting the existing tree and the new node.
        """
        if node in self.children and conn_type == self.connector:
            return
        if len(self.children) < 2:
            self.connector = conn_type
        if self.connector == conn_type:
            if isinstance(node, SearchNode) and (
                node.connector == conn_type or len(node) == 1
            ):
                self.children.extend(node.children)
            else:
                self.children.append(node)
        else:
            obj = self._new_instance(self.children, self.connector, self.negated)
            self.connector = conn_type
            self.children = [obj, node]

    def negate(self):
        """
        Negate the sense of the root connector. This reorganises the children
        so that the current node has a single child: a negated node containing
        all the previous children. This slightly odd construction makes adding
        new children behave more intuitively.

        Interpreting the meaning of this negate is up to client code. This
        method is useful for implementing "not" arrangements.
        """
        self.children = [
            self._new_instance(self.children, self.connector, not self.negated)
        ]
        self.connector = self.default

    def start_subtree(self, conn_type):
        """
        Sets up internal state so that new nodes are added to a subtree of the
        current node. The conn_type specifies how the sub-tree is joined to the
        existing children.
        """
        if len(self.children) == 1:
            self.connector = conn_type
        elif self.connector != conn_type:
            self.children = [
                self._new_instance(self.children, self.connector, self.negated)
            ]
            self.connector = conn_type
            self.negated = False

        self.subtree_parents.append(
            self.__class__(self.children, self.connector, self.negated)
        )
        self.connector = self.default
        self.negated = False
        self.children = []

    def end_subtree(self):
        """
        Closes off the most recently unmatched start_subtree() call.

        This puts the current state into a node of the parent tree and returns
        the current instances state to be the parent.
        """
        obj = self.subtree_parents.pop()
        node = self.__class__(self.children, self.connector)
        self.connector = obj.connector
        self.negated = obj.negated
        self.children = obj.children
        self.children.append(node)

    # End compat.

    def __repr__(self):
        return "<SQ: %s %s>" % (
            self.connector,
            self.as_query_string(self._repr_query_fragment_callback),
        )

    def _repr_query_fragment_callback(self, field, filter_type, value):
        return "%s%s%s=%s" % (field, FILTER_SEPARATOR, filter_type, force_str(value))

    def as_query_string(self, query_fragment_callback):
        """
        Produces a portion of the search query from the current SQ and its
        children.
        """
        result = []

        for child in self.children:
            if hasattr(child, "as_query_string"):
                result.append(child.as_query_string(query_fragment_callback))
            else:
                expression, value = child
                field, filter_type = self.split_expression(expression)
                result.append(query_fragment_callback(field, filter_type, value))

        conn = " %s " % self.connector
        query_string = conn.join(result)

        if query_string:
            if self.negated:
                query_string = "NOT (%s)" % query_string
            elif len(self.children) != 1:
                query_string = "(%s)" % query_string

        return query_string

    def split_expression(self, expression):
        """Parses an expression and determines the field and filter type."""
        parts = expression.split(FILTER_SEPARATOR)
        field = parts[0]
        if len(parts) == 1 or parts[-1] not in VALID_FILTERS:
            filter_type = "content"
        else:
            filter_type = parts.pop()

        return (field, filter_type)


class SQ(Q, SearchNode):
    """
    Manages an individual condition within a query.

    Most often, this will be a lookup to ensure that a certain word or phrase
    appears in the documents being indexed. However, it also supports filtering
    types (such as 'lt', 'gt', 'in' and others) for more complex lookups.
    """

    pass


class BaseSearchQuery:
    """
    A base class for handling the query itself.

    This class acts as an intermediary between the ``SearchQuerySet`` and the
    ``SearchBackend`` itself.

    The ``SearchQuery`` object maintains a tree of ``SQ`` objects. Each ``SQ``
    object supports what field it looks up against, what kind of lookup (i.e.
    the __'s), what value it's looking for, if it's a AND/OR/NOT and tracks
    any children it may have. The ``SearchQuery.build_query`` method starts with
    the root of the tree, building part of the final query at each node until
    the full final query is ready for the ``SearchBackend``.

    Backends should extend this class and provide implementations for
    ``build_query_fragment``, ``clean`` and ``run``. See the ``solr`` backend for an example
    implementation.
    """

    def __init__(self, using=DEFAULT_ALIAS):
        self.query_filter = SearchNode()
        self.order_by = []
        self.models = set()
        self.boost = {}
        self.start_offset = 0
        self.end_offset = None
        self.highlight = False
        self.facets = {}
        self.date_facets = {}
        self.query_facets = []
        self.narrow_queries = set()
        #: If defined, fields should be a list of field names - no other values
        #: will be retrieved so the caller must be careful to include django_ct
        #: and django_id when using code which expects those to be included in
        #: the results
        self.fields = []
        # Geospatial-related information
        self.within = {}
        self.dwithin = {}
        self.distance_point = {}
        # Internal.
        self._raw_query = None
        self._raw_query_params = {}
        self._more_like_this = False
        self._mlt_instance = None
        self._results = None
        self._hit_count = None
        self._facet_counts = None
        self._stats = None
        self._spelling_suggestion = SPELLING_SUGGESTION_HAS_NOT_RUN
        self.spelling_query = None
        self.result_class = SearchResult
        self.stats = {}
        from haystack import connections

        self._using = using
        self.backend = connections[self._using].get_backend()

    def __str__(self):
        return self.build_query()

    def __getstate__(self):
        """For pickling."""
        obj_dict = self.__dict__.copy()
        del obj_dict["backend"]
        return obj_dict

    def __setstate__(self, obj_dict):
        """For unpickling."""
        from haystack import connections

        self.__dict__.update(obj_dict)
        self.backend = connections[self._using].get_backend()

    def has_run(self):
        """Indicates if any query has been been run."""
        return None not in (self._results, self._hit_count)

    def build_params(self, spelling_query=None):
        """Generates a list of params to use when searching."""
        kwargs = {"start_offset": self.start_offset}

        if self.order_by:
            kwargs["sort_by"] = self.order_by

        if self.end_offset is not None:
            kwargs["end_offset"] = self.end_offset

        if self.highlight:
            kwargs["highlight"] = self.highlight

        if self.facets:
            kwargs["facets"] = self.facets

        if self.date_facets:
            kwargs["date_facets"] = self.date_facets

        if self.query_facets:
            kwargs["query_facets"] = self.query_facets

        if self.narrow_queries:
            kwargs["narrow_queries"] = self.narrow_queries

        if spelling_query:
            kwargs["spelling_query"] = spelling_query
        elif self.spelling_query:
            kwargs["spelling_query"] = self.spelling_query

        if self.boost:
            kwargs["boost"] = self.boost

        if self.within:
            kwargs["within"] = self.within

        if self.dwithin:
            kwargs["dwithin"] = self.dwithin

        if self.distance_point:
            kwargs["distance_point"] = self.distance_point

        if self.result_class:
            kwargs["result_class"] = self.result_class

        if self.fields:
            kwargs["fields"] = self.fields

        if self.models:
            kwargs["models"] = self.models

        return kwargs

    def run(self, spelling_query=None, **kwargs):
        """Builds and executes the query. Returns a list of search results."""
        final_query = self.build_query()
        search_kwargs = self.build_params(spelling_query=spelling_query)

        if kwargs:
            search_kwargs.update(kwargs)

        results = self.backend.search(final_query, **search_kwargs)
        self._results = results.get("results", [])
        self._hit_count = results.get("hits", 0)
        self._facet_counts = self.post_process_facets(results)
        self._spelling_suggestion = results.get("spelling_suggestion", None)

    def run_mlt(self, **kwargs):
        """
        Executes the More Like This. Returns a list of search results similar
        to the provided document (and optionally query).
        """
        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."
            )

        search_kwargs = {"result_class": self.result_class}

        if self.models:
            search_kwargs["models"] = self.models

        if kwargs:
            search_kwargs.update(kwargs)

        additional_query_string = self.build_query()
        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)

    def run_raw(self, **kwargs):
        """Executes a raw query. Returns a list of search results."""
        search_kwargs = self.build_params()
        search_kwargs.update(self._raw_query_params)

        if kwargs:
            search_kwargs.update(kwargs)

        results = self.backend.search(self._raw_query, **search_kwargs)
        self._results = results.get("results", [])
        self._hit_count = results.get("hits", 0)
        self._facet_counts = results.get("facets", {})
        self._spelling_suggestion = results.get("spelling_suggestion", None)

    def get_count(self):
        """
        Returns the number of results the backend found for the query.

        If the query has not been run, this will execute the query and store
        the results.
        """
        if self._hit_count is None:
            # Limit the slice to 1 so we get a count without consuming
            # everything.
            if not self.end_offset:
                self.end_offset = 1

            if self._more_like_this:
                # Special case for MLT.
                self.run_mlt()
            elif self._raw_query:
                # Special case for raw queries.
                self.run_raw()
            else:
                self.run()

        return self._hit_count

    def get_results(self, **kwargs):
        """
        Returns the results received from the backend.

        If the query has not been run, this will execute the query and store
        the results.
        """
        if self._results is None:
            if self._more_like_this:
                # Special case for MLT.
                self.run_mlt(**kwargs)
            elif self._raw_query:
                # Special case for raw queries.
                self.run_raw(**kwargs)
            else:
                self.run(**kwargs)

        return self._results

    def get_facet_counts(self):
        """
        Returns the facet counts received from the backend.

        If the query has not been run, this will execute the query and store
        the results.
        """
        if self._facet_counts is None:
            self.run()

        return self._facet_counts

    def get_stats(self):
        """
        Returns the stats received from the backend.

        If the query has not been run, this will execute the query and store
        the results
        """
        if self._stats is None:
            self.run()
        return self._stats

    def set_spelling_query(self, spelling_query):
        self.spelling_query = spelling_query

    def get_spelling_suggestion(self, preferred_query=None):
        """
        Returns the spelling suggestion received from the backend.

        If the query has not been run, this will execute the query and store
        the results.
        """
        if self._spelling_suggestion is SPELLING_SUGGESTION_HAS_NOT_RUN:
            self.run(spelling_query=preferred_query)

        return self._spelling_suggestion

    def boost_fragment(self, boost_word, boost_value):
        """Generates query fragment for boosting a single word/value pair."""
        return "%s^%s" % (boost_word, boost_value)

    def matching_all_fragment(self):
        """Generates the query that matches all documents."""
        return "*"

    def build_query(self):
        """
        Interprets the collected query metadata and builds the final query to
        be sent to the backend.
        """
        final_query = self.query_filter.as_query_string(self.build_query_fragment)

        if not final_query:
            # Match all.
            final_query = self.matching_all_fragment()

        if self.boost:
            boost_list = []

            for boost_word, boost_value in self.boost.items():
                boost_list.append(self.boost_fragment(boost_word, boost_value))

            final_query = "%s %s" % (final_query, " ".join(boost_list))

        return final_query

    def combine(self, rhs, connector=SQ.AND):
        if connector == SQ.AND:
            self.add_filter(rhs.query_filter)
        elif connector == SQ.OR:
            self.add_filter(rhs.query_filter, use_or=True)

    # Methods for backends to implement.

    def build_query_fragment(self, field, filter_type, value):
        """
        Generates a query fragment from a field, filter type and a value.

        Must be implemented in backends as this will be highly backend specific.
        """
        raise NotImplementedError(
            "Subclasses must provide a way to generate query fragments via the 'build_query_fragment' method."
        )

    # Standard methods to alter the query.

    def clean(self, query_fragment):
        """
        Provides a mechanism for sanitizing user input before presenting the
        value to the backend.

        A basic (override-able) implementation is provided.
        """
        if not isinstance(query_fragment, str):
            return query_fragment

        words = query_fragment.split()
        cleaned_words = []

        for word in words:
            if word in self.backend.RESERVED_WORDS:
                word = word.replace(word, word.lower())

            for char in self.backend.RESERVED_CHARACTERS:
                word = word.replace(char, "\\%s" % char)

            cleaned_words.append(word)

        return " ".join(cleaned_words)

    def build_not_query(self, query_string):
        if " " in query_string:
            query_string = "(%s)" % query_string

        return "NOT %s" % query_string

    def build_exact_query(self, query_string):
        return '"%s"' % query_string

    def add_filter(self, query_filter, use_or=False):
        """
        Adds a SQ to the current query.
        """
        if use_or:
            connector = SQ.OR
        else:
            connector = SQ.AND

        if (
            self.query_filter
            and query_filter.connector != connector
            and len(query_filter) > 1
        ):
            self.query_filter.start_subtree(connector)
            subtree = True
        else:
            subtree = False

        for child in query_filter.children:
            if isinstance(child, tree.Node):
                self.query_filter.start_subtree(connector)
                self.add_filter(child)
                self.query_filter.end_subtree()
            else:
                expression, value = child
                self.query_filter.add((expression, value), connector)

            connector = query_filter.connector

        if query_filter.negated:
            self.query_filter.negate()

        if subtree:
            self.query_filter.end_subtree()

    def add_order_by(self, field):
        """Orders the search result by a field."""
        self.order_by.append(field)

    def clear_order_by(self):
        """
        Clears out all ordering that has been already added, reverting the
        query to relevancy.
        """
        self.order_by = []

    def add_model(self, model):
        """
        Restricts the query requiring matches in the given model.

        This builds upon previous additions, so you can limit to multiple models
        by chaining this method several times.
        """
        if not isinstance(model, ModelBase):
            raise AttributeError(
                "The model being added to the query must derive from Model."
            )

        self.models.add(model)

    def set_limits(self, low=None, high=None):
        """Restricts the query by altering either the start, end or both offsets."""
        if low is not None:
            self.start_offset = int(low)

        if high is not None:
            self.end_offset = int(high)

    def clear_limits(self):
        """Clears any existing limits."""
        self.start_offset, self.end_offset = 0, None

    def add_boost(self, term, boost_value):
        """Adds a boosted term and the amount to boost it to the query."""
        self.boost[term] = boost_value

    def raw_search(self, query_string, **kwargs):
        """
        Runs a raw query (no parsing) against the backend.

        This method causes the SearchQuery to ignore the standard query
        generating facilities, running only what was provided instead.

        Note that any kwargs passed along will override anything provided
        to the rest of the ``SearchQuerySet``.
        """
        self._raw_query = query_string
        self._raw_query_params = kwargs

    def more_like_this(self, model_instance):
        """
        Allows backends with support for "More Like This" to return results
        similar to the provided instance.
        """
        self._more_like_this = True
        self._mlt_instance = model_instance

    def add_stats_query(self, stats_field, stats_facets):
        """Adds stats and stats_facets queries for the Solr backend."""
        self.stats[stats_field] = stats_facets

    def add_highlight(self, **kwargs):
        """Adds highlighting to the search results."""
        self.highlight = kwargs or True

    def add_within(self, field, point_1, point_2):
        """Adds bounding box parameters to search query."""
        from haystack.utils.geo import ensure_point

        self.within = {
            "field": field,
            "point_1": ensure_point(point_1),
            "point_2": ensure_point(point_2),
        }

    def add_dwithin(self, field, point, distance):
        """Adds radius-based parameters to search query."""
        from haystack.utils.geo import ensure_distance, ensure_point

        self.dwithin = {
            "field": field,
            "point": ensure_point(point),
            "distance": ensure_distance(distance),
        }

    def add_distance(self, field, point):
        """
        Denotes that results should include distance measurements from the
        point passed in.
        """
        from haystack.utils.geo import ensure_point

        self.distance_point = {"field": field, "point": ensure_point(point)}

    def add_field_facet(self, field, **options):
        """Adds a regular facet on a field."""
        from haystack import connections

        field_name = (
            connections[self._using].get_unified_index().get_facet_fieldname(field)
        )
        self.facets[field_name] = options.copy()

    def add_date_facet(self, field, start_date, end_date, gap_by, gap_amount=1):
        """Adds a date-based facet on a field."""
        from haystack import connections

        if gap_by not in VALID_GAPS:
            raise FacetingError(
                "The gap_by ('%s') must be one of the following: %s."
                % (gap_by, ", ".join(VALID_GAPS))
            )

        details = {
            "start_date": start_date,
            "end_date": end_date,
            "gap_by": gap_by,
            "gap_amount": gap_amount,
        }
        self.date_facets[
            connections[self._using].get_unified_index().get_facet_fieldname(field)
        ] = details

    def add_query_facet(self, field, query):
        """Adds a query facet on a field."""
        from haystack import connections

        self.query_facets.append(
            (
                connections[self._using].get_unified_index().get_facet_fieldname(field),
                query,
            )
        )

    def add_narrow_query(self, query):
        """
        Narrows a search to a subset of all documents per the query.

        Generally used in conjunction with faceting.
        """
        self.narrow_queries.add(query)

    def set_result_class(self, klass):
        """
        Sets the result class to use for results.

        Overrides any previous usages. If ``None`` is provided, Haystack will
        revert back to the default ``SearchResult`` object.
        """
        if klass is None:
            klass = SearchResult

        self.result_class = klass

    def post_process_facets(self, results):
        # Handle renaming the facet fields. Undecorate and all that.
        from haystack import connections

        revised_facets = {}
        field_data = connections[self._using].get_unified_index().all_searchfields()

        for facet_type, field_details in results.get("facets", {}).items():
            temp_facets = {}

            for field, field_facets in field_details.items():
                fieldname = field
                if field in field_data and hasattr(
                    field_data[field], "get_facet_for_name"
                ):
                    fieldname = field_data[field].get_facet_for_name()

                temp_facets[fieldname] = field_facets

            revised_facets[facet_type] = temp_facets

        return revised_facets

    def using(self, using=None):
        """
        Allows for overriding which connection should be used. This
        disables the use of routers when performing the query.

        If ``None`` is provided, it has no effect on what backend is used.
        """
        return self._clone(using=using)

    def _reset(self):
        """
        Resets the instance's internal state to appear as though no query has
        been run before. Only need to tweak a few variables we check.
        """
        self._results = None
        self._hit_count = None
        self._facet_counts = None
        self._spelling_suggestion = SPELLING_SUGGESTION_HAS_NOT_RUN

    def _clone(self, klass=None, using=None):
        if using is None:
            using = self._using
        else:
            from haystack import connections

            klass = connections[using].query

        if klass is None:
            klass = self.__class__

        clone = klass(using=using)
        clone.query_filter = deepcopy(self.query_filter)
        clone.order_by = self.order_by[:]
        clone.models = self.models.copy()
        clone.boost = self.boost.copy()
        clone.highlight = self.highlight
        clone.stats = self.stats.copy()
        clone.facets = self.facets.copy()
        clone.date_facets = self.date_facets.copy()
        clone.query_facets = self.query_facets[:]
        clone.narrow_queries = self.narrow_queries.copy()
        clone.start_offset = self.start_offset
        clone.end_offset = self.end_offset
        clone.result_class = self.result_class
        clone.within = self.within.copy()
        clone.dwithin = self.dwithin.copy()
        clone.distance_point = self.distance_point.copy()
        clone._raw_query = self._raw_query
        clone._raw_query_params = self._raw_query_params
        clone.spelling_query = self.spelling_query
        clone._more_like_this = self._more_like_this
        clone._mlt_instance = self._mlt_instance

        return clone


class BaseEngine:
    backend = BaseSearchBackend
    query = BaseSearchQuery
    unified_index = UnifiedIndex

    def __init__(self, using=None):
        if using is None:
            using = DEFAULT_ALIAS

        self.using = using
        self.options = settings.HAYSTACK_CONNECTIONS.get(self.using, {})
        self.queries = []
        self._index = None
        self._backend = None

    def get_backend(self):
        if self._backend is None:
            self._backend = self.backend(self.using, **self.options)
        return self._backend

    def reset_sessions(self):
        """Reset any transient connections, file handles, etc."""
        self._backend = None

    def get_query(self):
        return self.query(using=self.using)

    def reset_queries(self):
        del self.queries[:]

    def get_unified_index(self):
        if self._index is None:
            self._index = self.unified_index(self.options.get("EXCLUDED_INDEXES", []))

        return self._index