mirror of
https://github.com/Hopiu/xapian-haystack.git
synced 2026-05-16 11:03:17 +00:00
Started work in refactor
This commit is contained in:
parent
d6a24ef33c
commit
3334b94349
2 changed files with 64 additions and 57 deletions
|
|
@ -64,17 +64,19 @@ class XapianSearchQueryTestCase(TestCase):
|
|||
def test_build_query_multiple_words_not(self):
|
||||
self.sq.add_filter(~SQ(content='hello'))
|
||||
self.sq.add_filter(~SQ(content='world'))
|
||||
self.assertEqual(self.sq.build_query().get_description(), 'Xapian::Query(((<alldocuments> AND_NOT hello) AND (<alldocuments> AND_NOT world)))')
|
||||
|
||||
def test_build_query_multiple_words_or(self):
|
||||
self.sq.add_filter(SQ(content='hello') | SQ(content='world'))
|
||||
self.assertEqual(self.sq.build_query().get_description(), 'Xapian::Query((hello OR world))')
|
||||
|
||||
def test_build_query_multiple_words_mixed(self):
|
||||
self.sq.add_filter(SQ(content='why') | SQ(content='hello'))
|
||||
self.sq.add_filter(~SQ(content='world'))
|
||||
self.assertEqual(self.sq.build_query().get_description(), 'Xapian::Query(((why OR hello) AND (<alldocuments> AND_NOT world)))')
|
||||
self.assertEqual(self.sq.build_query().get_description(), 'Xapian::Query((NOT hello NOT world))')
|
||||
|
||||
# def test_build_query_multiple_words_or(self):
|
||||
# self.sq.add_filter('content', 'hello', use_or=True)
|
||||
# self.sq.add_filter('content', 'world', use_or=True)
|
||||
# self.assertEqual(self.sq.build_query(), 'hello OR world')
|
||||
#
|
||||
# def test_build_query_multiple_words_mixed(self):
|
||||
# self.sq.add_filter('content', 'why')
|
||||
# self.sq.add_filter('content', 'hello', use_or=True)
|
||||
# self.sq.add_filter('content', 'world', use_not=True)
|
||||
# self.assertEqual(self.sq.build_query(), 'why OR hello NOT world')
|
||||
#
|
||||
# def test_build_query_phrase(self):
|
||||
# self.sq.add_filter('content', 'hello world')
|
||||
# self.assertEqual(self.sq.build_query(), '"hello world"')
|
||||
|
|
|
|||
|
|
@ -936,58 +936,63 @@ class SearchQuery(BaseSearchQuery):
|
|||
def build_query(self):
|
||||
if not self.query_filter:
|
||||
return xapian.Query('')
|
||||
else:
|
||||
return self._query_from_search_node(self.query_filter)
|
||||
|
||||
def _query_from_search_node(self, search_node, is_not=False):
|
||||
query_list = []
|
||||
values = []
|
||||
|
||||
for child in search_node.children:
|
||||
if isinstance(child, SearchNode):
|
||||
query_list.append(
|
||||
xapian.Query(
|
||||
xapian.Query.OP_AND,
|
||||
self._query_from_search_node(
|
||||
child, child.negated
|
||||
)
|
||||
)
|
||||
)
|
||||
for child in self.query_filter.children:
|
||||
if isinstance(child, self.query_filter.__class__):
|
||||
print 'SQ: ', child # TODO: Recursive call down tree...
|
||||
else:
|
||||
expression, value = child
|
||||
if is_not:
|
||||
# DS_TODO: This can almost definitely be improved.
|
||||
query_list.append(xapian.Query(xapian.Query.OP_AND_NOT, '', value))
|
||||
else:
|
||||
query_list.append(xapian.Query(value))
|
||||
field, filter_type = self.query_filter.split_expression(expression)
|
||||
values.append(value)
|
||||
|
||||
if search_node.connector == 'OR':
|
||||
return xapian.Query(xapian.Query.OP_OR, query_list)
|
||||
else:
|
||||
return xapian.Query(xapian.Query.OP_AND, query_list)
|
||||
|
||||
def build_sub_query(self, value):
|
||||
return xapian.Query(value)
|
||||
|
||||
#
|
||||
# if not self.query_filter.children:
|
||||
# return xapian.Query('')
|
||||
# else:
|
||||
# query_list = []
|
||||
#
|
||||
# for child in self.query_filter.children:
|
||||
# if isinstance(child, self.query_filter.__class__):
|
||||
# query_list.append(self.build_query(child))
|
||||
# else:
|
||||
# expression, value = child
|
||||
# field, filter_type = self.query_filter.split_expression(expression)
|
||||
# query_list.append(xapian.Query(value))
|
||||
#
|
||||
# return xapian.Query(xapian.Query.OP_AND, query_list)
|
||||
|
||||
# def build_query_fragment(self, field, filter_type, value):
|
||||
# print 'field: ', field
|
||||
# print 'filter_type: ', filter_type
|
||||
# print 'value: ', value
|
||||
return xapian.Query(xapian.Query.OP_AND, values)
|
||||
|
||||
def run(self, spelling_query=None):
|
||||
"""
|
||||
Builds and executes the query. Returns a list of search results.
|
||||
|
||||
Returns:
|
||||
List of search results
|
||||
"""
|
||||
final_query = self.build_query()
|
||||
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 - self.start_offset
|
||||
|
||||
if self.highlight:
|
||||
kwargs['highlight'] = self.highlight
|
||||
|
||||
if self.facets:
|
||||
kwargs['facets'] = list(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
|
||||
|
||||
if self.boost:
|
||||
kwargs['boost'] = self.boost
|
||||
|
||||
results = self.backend.search(final_query, **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)
|
||||
|
||||
# """
|
||||
# Builds a search query fragment from a field, filter type and value.
|
||||
|
|
|
|||
Loading…
Reference in a new issue