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:
remapped_fields = [Field(self.mapping.all_field_name)]
models = get_indexed_models()
unique_boosts = set()
for model in models:
if not issubclass(model, self.queryset.model):
continue
for field in model.get_searchable_search_fields():
if field.boost:
unique_boosts.add(float(field.boost))
models = get_indexed_models()
unique_boosts = set()
for model in models:
if not issubclass(model, self.queryset.model):
continue
for field in model.get_searchable_search_fields():
if field.boost:
unique_boosts.add(float(field.boost))
remapped_fields.extend(
[
Field(self.mapping.get_boost_field_name(boost), boost)
for boost in unique_boosts
]
)
remapped_fields.extend(
[
Field(self.mapping.get_boost_field_name(boost), boost)
for boost in unique_boosts
]
)
return remapped_fields
def _compile_fuzzy_query(self, query, fields):
if len(fields) == 1:
return {
"match": {
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",
}
match_query = {
"query": query.query_string,
"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):
match_query = {"query": query.query_string}

Wyświetl plik

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

Wyświetl plik

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