mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-11 16:53:10 +00:00
Fixes Elasticsearch SearchQueryCompiler names.
This commit is contained in:
parent
2b4bb2b718
commit
cf5d7ae0f0
3 changed files with 110 additions and 110 deletions
|
|
@ -15,7 +15,7 @@ class Elasticsearch5Index(Elasticsearch2Index):
|
|||
pass
|
||||
|
||||
|
||||
class Elasticsearch5SearchQuery(Elasticsearch2SearchQueryCompiler):
|
||||
class Elasticsearch5SearchQueryCompiler(Elasticsearch2SearchQueryCompiler):
|
||||
mapping_class = Elasticsearch5Mapping
|
||||
|
||||
def _connect_filters(self, filters, connector, negated):
|
||||
|
|
@ -77,7 +77,7 @@ class Elasticsearch5SearchResults(Elasticsearch2SearchResults):
|
|||
class Elasticsearch5SearchBackend(Elasticsearch2SearchBackend):
|
||||
mapping_class = Elasticsearch5Mapping
|
||||
index_class = Elasticsearch5Index
|
||||
query_compiler_class = Elasticsearch5SearchQuery
|
||||
query_compiler_class = Elasticsearch5SearchQueryCompiler
|
||||
results_class = Elasticsearch5SearchResults
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -45,55 +45,55 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default)
|
||||
)
|
||||
|
||||
query_class = Elasticsearch2SearchBackend.query_class
|
||||
query_compiler_class = Elasticsearch2SearchBackend.query_compiler_class
|
||||
|
||||
def test_simple(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_match_all(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), MATCH_ALL)
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), MATCH_ALL)
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'query': {'match_all': {}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_and_operator(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", operator='and')
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", operator='and')
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials'], 'operator': 'and'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title="Test"), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'term': {'title_filter': 'Test'}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_and_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title="Test", publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title="Test", publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
|
|
@ -102,7 +102,7 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
|
||||
# Make sure field filters are sorted (as they can be in any order which may cause false positives)
|
||||
query = query.get_query()
|
||||
query = query_compiler.get_query()
|
||||
field_filters = query['filtered']['filter']['and'][1]['and']
|
||||
field_filters[:] = sorted(field_filters, key=lambda f: list(f['term'].keys())[0])
|
||||
|
||||
|
|
@ -110,10 +110,10 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
|
||||
def test_or_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(Q(title="Test") | Q(publication_date=datetime.date(2017, 10, 18))), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(Q(title="Test") | Q(publication_date=datetime.date(2017, 10, 18))), "Hello")
|
||||
|
||||
# Make sure field filters are sorted (as they can be in any order which may cause false positives)
|
||||
query = query.get_query()
|
||||
query = query_compiler.get_query()
|
||||
field_filters = query['filtered']['filter']['and'][1]['or']
|
||||
field_filters[:] = sorted(field_filters, key=lambda f: list(f['term'].keys())[0])
|
||||
|
||||
|
|
@ -126,51 +126,51 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
|
||||
def test_negated_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.exclude(publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.exclude(publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'not': {'term': {'publication_date_filter': '2017-10-18'}}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_fields(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", fields=['title'])
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", fields=['title'])
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'query': {'match': {'title': 'Hello'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_fields_with_and_operator(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", fields=['title'], operator='and')
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", fields=['title'], operator='and')
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'query': {'match': {'title': {'query': 'Hello', 'operator': 'and'}}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_multiple_fields(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", fields=['title', 'content'])
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", fields=['title', 'content'])
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'query': {'multi_match': {'fields': ['title', 'content'], 'query': 'Hello'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_multiple_fields_with_and_operator(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.all(), "Hello", fields=['title', 'content'], operator='and'
|
||||
)
|
||||
|
||||
|
|
@ -179,68 +179,68 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'query': {'multi_match': {'fields': ['title', 'content'], 'query': 'Hello', 'operator': 'and'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_exact_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__exact="Test"), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__exact="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'term': {'title_filter': 'Test'}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_none_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title=None), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title=None), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'missing': {'field': 'title_filter'}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_isnull_true_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__isnull=True), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__isnull=True), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'missing': {'field': 'title_filter'}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_isnull_false_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__isnull=False), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__isnull=False), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'exists': {'field': 'title_filter'}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_startswith_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__startswith="Test"), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__startswith="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'filtered': {'filter': {'and': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'prefix': {'title_filter': 'Test'}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_gt_lookup(self):
|
||||
# This also tests conversion of python dates to strings
|
||||
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__gt=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -249,11 +249,11 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'gt': '2014-04-29'}}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_lt_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__lt=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -262,11 +262,11 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'lt': '2014-04-29'}}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_gte_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__gte=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -275,11 +275,11 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'gte': '2014-04-29'}}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_lte_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__lte=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -288,14 +288,14 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'lte': '2014-04-29'}}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_range_lookup(self):
|
||||
start_date = datetime.datetime(2014, 4, 29)
|
||||
end_date = datetime.datetime(2014, 8, 19)
|
||||
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__range=(start_date, end_date)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -304,37 +304,37 @@ class TestElasticsearch2SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'gte': '2014-04-29', 'lte': '2014-08-19'}}}
|
||||
]}, 'query': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_custom_ordering(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.order_by('publication_date'), "Hello", order_by_relevance=False
|
||||
)
|
||||
|
||||
# Check it
|
||||
expected_result = [{'publication_date_filter': 'asc'}]
|
||||
self.assertDictEqual(query.get_sort(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_sort(), expected_result)
|
||||
|
||||
def test_custom_ordering_reversed(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.order_by('-publication_date'), "Hello", order_by_relevance=False
|
||||
)
|
||||
|
||||
# Check it
|
||||
expected_result = [{'publication_date_filter': 'desc'}]
|
||||
self.assertDictEqual(query.get_sort(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_sort(), expected_result)
|
||||
|
||||
def test_custom_ordering_multiple(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.order_by('publication_date', 'number_of_pages'), "Hello", order_by_relevance=False
|
||||
)
|
||||
|
||||
# Check it
|
||||
expected_result = [{'publication_date_filter': 'asc'}, {'number_of_pages_filter': 'asc'}]
|
||||
self.assertDictEqual(query.get_sort(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_sort(), expected_result)
|
||||
|
||||
|
||||
class TestElasticsearch2SearchResults(TestCase):
|
||||
|
|
@ -348,11 +348,11 @@ class TestElasticsearch2SearchResults(TestCase):
|
|||
|
||||
def get_results(self):
|
||||
backend = Elasticsearch2SearchBackend({})
|
||||
query = mock.MagicMock()
|
||||
query.queryset = models.Book.objects.all()
|
||||
query.get_query.return_value = 'QUERY'
|
||||
query.get_sort.return_value = None
|
||||
return backend.results_class(backend, query)
|
||||
query_compiler = mock.MagicMock()
|
||||
query_compiler.queryset = models.Book.objects.all()
|
||||
query_compiler.get_query.return_value = 'QUERY'
|
||||
query_compiler.get_sort.return_value = None
|
||||
return backend.results_class(backend, query_compiler)
|
||||
|
||||
def construct_search_response(self, results):
|
||||
return {
|
||||
|
|
|
|||
|
|
@ -49,55 +49,55 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
json.dumps(a, sort_keys=True, default=default), json.dumps(b, sort_keys=True, default=default)
|
||||
)
|
||||
|
||||
query_class = Elasticsearch5SearchBackend.query_class
|
||||
query_compiler_class = Elasticsearch5SearchBackend.query_compiler_class
|
||||
|
||||
def test_simple(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_match_all(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), MATCH_ALL)
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), MATCH_ALL)
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'must': {'match_all': {}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_and_operator(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", operator='and')
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", operator='and')
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials'], 'operator': 'and'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title="Test"), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'term': {'title_filter': 'Test'}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_and_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title="Test", publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title="Test", publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
|
|
@ -106,7 +106,7 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
|
||||
# Make sure field filters are sorted (as they can be in any order which may cause false positives)
|
||||
query = query.get_query()
|
||||
query = query_compiler.get_query()
|
||||
field_filters = query['bool']['filter'][1]['bool']['must']
|
||||
field_filters[:] = sorted(field_filters, key=lambda f: list(f['term'].keys())[0])
|
||||
|
||||
|
|
@ -114,10 +114,10 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
|
||||
def test_or_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(Q(title="Test") | Q(publication_date=datetime.date(2017, 10, 18))), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(Q(title="Test") | Q(publication_date=datetime.date(2017, 10, 18))), "Hello")
|
||||
|
||||
# Make sure field filters are sorted (as they can be in any order which may cause false positives)
|
||||
query = query.get_query()
|
||||
query = query_compiler.get_query()
|
||||
field_filters = query['bool']['filter'][1]['bool']['should']
|
||||
field_filters[:] = sorted(field_filters, key=lambda f: list(f['term'].keys())[0])
|
||||
|
||||
|
|
@ -130,51 +130,51 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
|
||||
def test_negated_filter(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.exclude(publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.exclude(publication_date=datetime.date(2017, 10, 18)), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'bool': {'mustNot': {'term': {'publication_date_filter': '2017-10-18'}}}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_fields(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", fields=['title'])
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", fields=['title'])
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'must': {'match': {'title': 'Hello'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_fields_with_and_operator(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", fields=['title'], operator='and')
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", fields=['title'], operator='and')
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'must': {'match': {'title': {'query': 'Hello', 'operator': 'and'}}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_multiple_fields(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.all(), "Hello", fields=['title', 'content'])
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.all(), "Hello", fields=['title', 'content'])
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {
|
||||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'must': {'multi_match': {'fields': ['title', 'content'], 'query': 'Hello'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_multiple_fields_with_and_operator(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.all(), "Hello", fields=['title', 'content'], operator='and'
|
||||
)
|
||||
|
||||
|
|
@ -183,68 +183,68 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
'filter': {'match': {'content_type': 'searchtests.Book'}},
|
||||
'must': {'multi_match': {'fields': ['title', 'content'], 'query': 'Hello', 'operator': 'and'}}
|
||||
}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_exact_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__exact="Test"), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__exact="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'term': {'title_filter': 'Test'}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_none_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title=None), "Hello")
|
||||
query = self.query_compiler_class(models.Book.objects.filter(title=None), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'missing': {'field': 'title_filter'}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_isnull_true_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__isnull=True), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__isnull=True), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'missing': {'field': 'title_filter'}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_isnull_false_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__isnull=False), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__isnull=False), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'exists': {'field': 'title_filter'}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_startswith_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(models.Book.objects.filter(title__startswith="Test"), "Hello")
|
||||
query_compiler = self.query_compiler_class(models.Book.objects.filter(title__startswith="Test"), "Hello")
|
||||
|
||||
# Check it
|
||||
expected_result = {'bool': {'filter': [
|
||||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'prefix': {'title_filter': 'Test'}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_gt_lookup(self):
|
||||
# This also tests conversion of python dates to strings
|
||||
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__gt=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -253,11 +253,11 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'gt': '2014-04-29'}}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_lt_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__lt=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -266,11 +266,11 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'lt': '2014-04-29'}}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_gte_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__gte=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -279,11 +279,11 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'gte': '2014-04-29'}}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_lte_lookup(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__lte=datetime.datetime(2014, 4, 29)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -292,14 +292,14 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'lte': '2014-04-29'}}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_range_lookup(self):
|
||||
start_date = datetime.datetime(2014, 4, 29)
|
||||
end_date = datetime.datetime(2014, 8, 19)
|
||||
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.filter(publication_date__range=(start_date, end_date)), "Hello"
|
||||
)
|
||||
|
||||
|
|
@ -308,37 +308,37 @@ class TestElasticsearch5SearchQuery(TestCase):
|
|||
{'match': {'content_type': 'searchtests.Book'}},
|
||||
{'range': {'publication_date_filter': {'gte': '2014-04-29', 'lte': '2014-08-19'}}}
|
||||
], 'must': {'multi_match': {'query': 'Hello', 'fields': ['_all', '_partials']}}}}
|
||||
self.assertDictEqual(query.get_query(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_query(), expected_result)
|
||||
|
||||
def test_custom_ordering(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.order_by('publication_date'), "Hello", order_by_relevance=False
|
||||
)
|
||||
|
||||
# Check it
|
||||
expected_result = [{'publication_date_filter': 'asc'}]
|
||||
self.assertDictEqual(query.get_sort(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_sort(), expected_result)
|
||||
|
||||
def test_custom_ordering_reversed(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.order_by('-publication_date'), "Hello", order_by_relevance=False
|
||||
)
|
||||
|
||||
# Check it
|
||||
expected_result = [{'publication_date_filter': 'desc'}]
|
||||
self.assertDictEqual(query.get_sort(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_sort(), expected_result)
|
||||
|
||||
def test_custom_ordering_multiple(self):
|
||||
# Create a query
|
||||
query = self.query_class(
|
||||
query_compiler = self.query_compiler_class(
|
||||
models.Book.objects.order_by('publication_date', 'number_of_pages'), "Hello", order_by_relevance=False
|
||||
)
|
||||
|
||||
# Check it
|
||||
expected_result = [{'publication_date_filter': 'asc'}, {'number_of_pages_filter': 'asc'}]
|
||||
self.assertDictEqual(query.get_sort(), expected_result)
|
||||
self.assertDictEqual(query_compiler.get_sort(), expected_result)
|
||||
|
||||
|
||||
class TestElasticsearch5SearchResults(TestCase):
|
||||
|
|
@ -352,11 +352,11 @@ class TestElasticsearch5SearchResults(TestCase):
|
|||
|
||||
def get_results(self):
|
||||
backend = Elasticsearch5SearchBackend({})
|
||||
query = mock.MagicMock()
|
||||
query.queryset = models.Book.objects.all()
|
||||
query.get_query.return_value = 'QUERY'
|
||||
query.get_sort.return_value = None
|
||||
return backend.results_class(backend, query)
|
||||
query_compiler = mock.MagicMock()
|
||||
query_compiler.queryset = models.Book.objects.all()
|
||||
query_compiler.get_query.return_value = 'QUERY'
|
||||
query_compiler.get_sort.return_value = None
|
||||
return backend.results_class(backend, query_compiler)
|
||||
|
||||
def construct_search_response(self, results):
|
||||
return {
|
||||
|
|
|
|||
Loading…
Reference in a new issue