mirror of
https://github.com/Hopiu/xapian-haystack.git
synced 2026-05-16 02:53:13 +00:00
Four tests passing now. Recursively parsing the search nodes and negated on NOT as required.
This commit is contained in:
parent
048e296d65
commit
5c67f5476a
2 changed files with 30 additions and 17 deletions
|
|
@ -61,10 +61,10 @@ class XapianSearchQueryTestCase(TestCase):
|
|||
self.sq.add_filter(SQ(content='world'))
|
||||
self.assertEqual(self.sq.build_query().get_description(), 'Xapian::Query((hello AND world))')
|
||||
|
||||
# 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((NOT hello NOT world))')
|
||||
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('content', 'hello', use_or=True)
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
|
||||
__author__ = 'David Sauve'
|
||||
__version__ = (1, 0, 0, 'beta')
|
||||
__version__ = (2, 0, 0, 'alpha')
|
||||
|
||||
import datetime
|
||||
import cPickle as pickle
|
||||
|
|
@ -29,7 +29,7 @@ from django.conf import settings
|
|||
from django.core.exceptions import ImproperlyConfigured
|
||||
from django.utils.encoding import smart_unicode, force_unicode
|
||||
|
||||
from haystack.backends import BaseSearchBackend, BaseSearchQuery, log_query
|
||||
from haystack.backends import BaseSearchBackend, BaseSearchQuery, SearchNode, log_query
|
||||
from haystack.exceptions import MissingDependency
|
||||
from haystack.fields import DateField, DateTimeField, IntegerField, FloatField, BooleanField, MultiValueField
|
||||
from haystack.models import SearchResult
|
||||
|
|
@ -936,19 +936,32 @@ class SearchQuery(BaseSearchQuery):
|
|||
|
||||
def build_query(self):
|
||||
if not self.query_filter:
|
||||
query = xapian.Query('')
|
||||
return xapian.Query('')
|
||||
else:
|
||||
query_list = []
|
||||
|
||||
for child in self.query_filter.children:
|
||||
expression, value = child
|
||||
query_list.append(value)
|
||||
|
||||
query = xapian.Query(xapian.Query.OP_AND, query_list)
|
||||
|
||||
return query
|
||||
return self._query_from_search_node(self.query_filter)
|
||||
|
||||
def build_query_fragment(self, field, filter_type, value):
|
||||
def _query_from_search_node(self, search_node, is_not=False):
|
||||
query_list = []
|
||||
|
||||
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)
|
||||
)
|
||||
)
|
||||
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))
|
||||
|
||||
return xapian.Query(xapian.Query.OP_AND, query_list)
|
||||
|
||||
def build_sub_query(self, value):
|
||||
return xapian.Query(value)
|
||||
|
||||
#
|
||||
|
|
|
|||
Loading…
Reference in a new issue