Don't add _all_text fields to remapped_fields if explicit fields are specified

Also fix previously-masked issues with _compile_fuzzy_query - pass field boost, and use field_name as key instead of the Field object
pull/11297/head
Matt Westcott 2023-11-10 00:38:16 +00:00 zatwierdzone przez Matt Westcott
rodzic 1e964c1a22
commit ccf4227af1
3 zmienionych plików z 68 dodań i 91 usunięć

Wyświetl plik

@ -107,41 +107,36 @@ class Elasticsearch6SearchQueryCompiler(Elasticsearch5SearchQueryCompiler):
else: else:
remapped_fields = [Field(self.mapping.all_field_name)] remapped_fields = [Field(self.mapping.all_field_name)]
models = get_indexed_models() models = get_indexed_models()
unique_boosts = set() unique_boosts = set()
for model in models: for model in models:
if not issubclass(model, self.queryset.model): if not issubclass(model, self.queryset.model):
continue continue
for field in model.get_searchable_search_fields(): for field in model.get_searchable_search_fields():
if field.boost: if field.boost:
unique_boosts.add(float(field.boost)) unique_boosts.add(float(field.boost))
remapped_fields.extend( remapped_fields.extend(
[ [
Field(self.mapping.get_boost_field_name(boost), boost) Field(self.mapping.get_boost_field_name(boost), boost)
for boost in unique_boosts for boost in unique_boosts
] ]
) )
return remapped_fields return remapped_fields
def _compile_fuzzy_query(self, query, fields): def _compile_fuzzy_query(self, query, fields):
if len(fields) == 1: match_query = {
return { "query": query.query_string,
"match": { "fuzziness": "AUTO",
fields[0]: {
"query": query.query_string,
"fuzziness": "AUTO",
}
}
}
return {
"multi_match": {
"query": query.query_string,
"fields": [field.field_name_with_boost for field in fields],
"fuzziness": "AUTO",
}
} }
if len(fields) == 1:
if fields[0].boost != 1.0:
match_query["boost"] = fields[0].boost
return {"match": {fields[0].field_name: match_query}}
else:
match_query["fields"] = [field.field_name_with_boost for field in fields]
return {"multi_match": match_query}
def _compile_plaintext_query(self, query, fields, boost=1.0): def _compile_plaintext_query(self, query, fields, boost=1.0):
match_query = {"query": query.query_string} match_query = {"query": query.query_string}

Wyświetl plik

@ -278,12 +278,11 @@ class TestElasticsearch6SearchQuery(TestCase):
"bool": { "bool": {
"filter": {"match": {"content_type": "searchtests.Book"}}, "filter": {"match": {"content_type": "searchtests.Book"}},
"must": { "must": {
"multi_match": { "match": {
"fields": [ "title": {
"title^2.0", "query": "Hello",
"_all_text_boost_2_0^2.0", "boost": 2.0,
], },
"query": "Hello",
}, },
}, },
} }
@ -301,13 +300,12 @@ class TestElasticsearch6SearchQuery(TestCase):
"bool": { "bool": {
"filter": {"match": {"content_type": "searchtests.Book"}}, "filter": {"match": {"content_type": "searchtests.Book"}},
"must": { "must": {
"multi_match": { "match": {
"fields": [ "title": {
"title^2.0", "query": "Hello",
"_all_text_boost_2_0^2.0", "boost": 2.0,
], "operator": "and",
"query": "Hello", },
"operator": "and",
}, },
}, },
} }
@ -329,7 +327,6 @@ class TestElasticsearch6SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"content", "content",
"_all_text_boost_2_0^2.0",
], ],
"query": "Hello", "query": "Hello",
}, },
@ -356,7 +353,6 @@ class TestElasticsearch6SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"content", "content",
"_all_text_boost_2_0^2.0",
], ],
"query": "Hello", "query": "Hello",
"operator": "and", "operator": "and",
@ -723,7 +719,6 @@ class TestElasticsearch6SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"content", "content",
"_all_text_boost_2_0^2.0",
], ],
"query": "Hello world", "query": "Hello world",
"type": "phrase", "type": "phrase",
@ -739,13 +734,11 @@ class TestElasticsearch6SearchQuery(TestCase):
# Check it # Check it
expected_result = { expected_result = {
"multi_match": { "match_phrase": {
"fields": [ "title": {
"title^2.0", "query": "Hello world",
"_all_text_boost_2_0^2.0", "boost": 2.0,
], },
"query": "Hello world",
"type": "phrase",
}, },
} }
self.assertDictEqual(query_compiler.get_inner_query(), expected_result) self.assertDictEqual(query_compiler.get_inner_query(), expected_result)
@ -780,13 +773,12 @@ class TestElasticsearch6SearchQuery(TestCase):
# Check it # Check it
expected_result = { expected_result = {
"multi_match": { "match": {
"fields": [ "title": {
"title^2.0", "query": "Hello world",
"_all_text_boost_2_0^2.0", "fuzziness": "AUTO",
], "boost": 2.0,
"query": "Hello world", },
"fuzziness": "AUTO",
}, },
} }
self.assertDictEqual(query_compiler.get_inner_query(), expected_result) self.assertDictEqual(query_compiler.get_inner_query(), expected_result)
@ -804,7 +796,6 @@ class TestElasticsearch6SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"body", "body",
"_all_text_boost_2_0^2.0",
], ],
"query": "Hello world", "query": "Hello world",
"fuzziness": "AUTO", "fuzziness": "AUTO",

Wyświetl plik

@ -290,12 +290,11 @@ class TestElasticsearch7SearchQuery(TestCase):
"bool": { "bool": {
"filter": {"match": {"content_type": "searchtests.Book"}}, "filter": {"match": {"content_type": "searchtests.Book"}},
"must": { "must": {
"multi_match": { "match": {
"fields": [ "title": {
"title^2.0", "query": "Hello",
"_all_text_boost_2_0^2.0", "boost": 2.0,
], },
"query": "Hello",
}, },
}, },
} }
@ -313,13 +312,12 @@ class TestElasticsearch7SearchQuery(TestCase):
"bool": { "bool": {
"filter": {"match": {"content_type": "searchtests.Book"}}, "filter": {"match": {"content_type": "searchtests.Book"}},
"must": { "must": {
"multi_match": { "match": {
"fields": [ "title": {
"title^2.0", "query": "Hello",
"_all_text_boost_2_0^2.0", "boost": 2.0,
], "operator": "and",
"query": "Hello", }
"operator": "and",
}, },
}, },
} }
@ -341,7 +339,6 @@ class TestElasticsearch7SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"content", "content",
"_all_text_boost_2_0^2.0",
], ],
"query": "Hello", "query": "Hello",
} }
@ -368,7 +365,6 @@ class TestElasticsearch7SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"content", "content",
"_all_text_boost_2_0^2.0",
], ],
"query": "Hello", "query": "Hello",
"operator": "and", "operator": "and",
@ -736,7 +732,6 @@ class TestElasticsearch7SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"content", "content",
"_all_text_boost_2_0^2.0",
], ],
"type": "phrase", "type": "phrase",
} }
@ -751,13 +746,11 @@ class TestElasticsearch7SearchQuery(TestCase):
# Check it # Check it
expected_result = { expected_result = {
"multi_match": { "match_phrase": {
"fields": [ "title": {
"title^2.0", "query": "Hello world",
"_all_text_boost_2_0^2.0", "boost": 2.0,
], },
"query": "Hello world",
"type": "phrase",
}, },
} }
self.assertDictEqual(query_compiler.get_inner_query(), expected_result) self.assertDictEqual(query_compiler.get_inner_query(), expected_result)
@ -792,13 +785,12 @@ class TestElasticsearch7SearchQuery(TestCase):
# Check it # Check it
expected_result = { expected_result = {
"multi_match": { "match": {
"fields": [ "title": {
"title^2.0", "query": "Hello world",
"_all_text_boost_2_0^2.0", "fuzziness": "AUTO",
], "boost": 2.0,
"query": "Hello world", },
"fuzziness": "AUTO",
} }
} }
self.assertDictEqual(query_compiler.get_inner_query(), expected_result) self.assertDictEqual(query_compiler.get_inner_query(), expected_result)
@ -816,7 +808,6 @@ class TestElasticsearch7SearchQuery(TestCase):
"fields": [ "fields": [
"title^2.0", "title^2.0",
"body", "body",
"_all_text_boost_2_0^2.0",
], ],
"query": "Hello world", "query": "Hello world",
"fuzziness": "AUTO", "fuzziness": "AUTO",