Skip to content

Commit

Permalink
Revert "Split functionality out for easier extensibility"
Browse files Browse the repository at this point in the history
This reverts commit 83c1dad.
  • Loading branch information
gasman committed Oct 20, 2023
1 parent 44ed9ff commit e1dfe4b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
63 changes: 30 additions & 33 deletions wagtail/search/backends/elasticsearch5.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,22 +315,25 @@ class Elasticsearch5SearchQueryCompiler(BaseSearchQueryCompiler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.mapping = self.mapping_class(self.queryset.model)
self.remapped_fields = self._remap_fields(self.fields)

def _remap_fields(self, fields):
"""Convert field names into index column names"""
if fields is None:
return None

remapped_fields = []
searchable_fields = {f.field_name: f for f in self.get_searchable_fields()}
for field_name in fields:
if field_name in searchable_fields:
field_name = self.mapping.get_field_column_name(
searchable_fields[field_name]
)
remapped_fields.append(field_name)
return remapped_fields

# Convert field names into index column names
if self.fields:
fields = []
searchable_fields = {
f.field_name: f
for f in self.queryset.model.get_searchable_search_fields()
}
for field_name in self.fields:
if field_name in searchable_fields:
field_name = self.mapping.get_field_column_name(
searchable_fields[field_name]
)

fields.append(field_name)

self.remapped_fields = fields
else:
self.remapped_fields = None

def _process_lookup(self, field, lookup, value):
column_name = self.mapping.get_field_column_name(field)
Expand Down Expand Up @@ -403,19 +406,6 @@ def _process_lookup(self, field, lookup, value):
}
}

def _join_and_compile_queries(self, query, fields, boost=1.0):
if len(fields) == 1:
return self._compile_query(query, fields[0], boost)
else:
# Compile a query for each field then combine with disjunction
# max (or operator which takes the max score out of each of the
# field queries)
field_queries = []
for field in fields:
field_queries.append(self._compile_query(query, field, boost))

return {"dis_max": {"queries": field_queries}}

def _connect_filters(self, filters, connector, negated):
if filters:
if len(filters) == 1:
Expand Down Expand Up @@ -555,7 +545,17 @@ def get_inner_query(self):
return self._compile_fuzzy_query(self.query, fields)

else:
return self._join_and_compile_queries(self.query, fields)
if len(fields) == 1:
return self._compile_query(self.query, fields[0])
else:
# Compile a query for each field then combine with disjunction
# max (or operator which takes the max score out of each of the
# field queries)
field_queries = []
for field in fields:
field_queries.append(self._compile_query(self.query, field))

return {"dis_max": {"queries": field_queries}}

def get_content_type_filter(self):
# Query content_type using a "match" query. See comment in
Expand Down Expand Up @@ -596,9 +596,6 @@ def get_query(self):
else:
return inner_query

def get_searchable_fields(self):
return self.queryset.model.get_searchable_search_fields()

def get_sort(self):
# Ordering by relevance is the default in Elasticsearch
if self.order_by_relevance:
Expand Down
12 changes: 11 additions & 1 deletion wagtail/search/backends/elasticsearch6.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,17 @@ def get_inner_query(self):
}

else:
return self._join_and_compile_queries(self.query, fields)
if len(fields) == 1:
return self._compile_query(self.query, fields[0])
else:
# Compile a query for each field then combine with disjunction
# max (or operator which takes the max score out of each of the
# field queries)
field_queries = []
for field in fields:
field_queries.append(self._compile_query(self.query, field))

return {"dis_max": {"queries": field_queries}}


class Elasticsearch6SearchResults(Elasticsearch5SearchResults):
Expand Down

0 comments on commit e1dfe4b

Please sign in to comment.