Skip to content

Commit

Permalink
Fixed #129 -- auto_query now works as expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecarleitao committed Jun 13, 2014
1 parent fc0a9f7 commit a59e696
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions xapian_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from haystack import connections
from haystack.backends import BaseEngine, BaseSearchBackend, BaseSearchQuery, SearchNode, log_query
from haystack.constants import ID, DJANGO_ID, DJANGO_CT
from haystack.constants import ID, DJANGO_ID, DJANGO_CT, DEFAULT_OPERATOR
from haystack.exceptions import HaystackError, MissingDependency
from haystack.inputs import AutoQuery
from haystack.models import SearchResult
Expand Down Expand Up @@ -49,6 +49,13 @@
xapian.QueryParser.FLAG_PURE_NOT
)

# Mapping from `HAYSTACK_DEFAULT_OPERATOR` to Xapian operators
XAPIAN_OPTS = {'AND': xapian.Query.OP_AND,
'OR': xapian.Query.OP_OR,
'PHRASE': xapian.Query.OP_PHRASE,
'NEAR': xapian.Query.OP_NEAR
}

# number of documents checked by default when building facets
# this must be improved to be relative to the total number of docs.
DEFAULT_CHECK_AT_LEAST = 1000
Expand Down Expand Up @@ -750,6 +757,7 @@ def parse_query(self, query_string):
qp.set_database(self._database())
qp.set_stemmer(xapian.Stem(self.language))
qp.set_stemming_strategy(xapian.QueryParser.STEM_SOME)
qp.set_default_op(XAPIAN_OPTS[DEFAULT_OPERATOR])
qp.add_boolean_prefix('django_ct', TERM_PREFIXES['django_ct'])

for field_dict in self.schema:
Expand Down Expand Up @@ -1340,13 +1348,16 @@ def _filter_startswith(self, term, field_name, field_type, is_not):
Assumes term is not a list.
"""
# TODO: if field_type is of type integer, we need to marsh the value.
if field_name:
query_string = '%s:%s*' % (field_name, term)
if field_type == 'text':
if len(term.split()) == 1:
term = '^ %s*' % term
query = self.backend.parse_query(term)
else:
term = '^ %s' % term
query = self._phrase_query(term.split(), field_name, field_type)
else:
query_string = '%s*' % term

query = self.backend.parse_query(query_string)
term = '^%s*' % term
query = self.backend.parse_query(term)

if is_not:
return xapian.Query(xapian.Query.OP_AND_NOT, self._all_query(), query)
Expand Down

0 comments on commit a59e696

Please sign in to comment.