Skip to content

Commit

Permalink
apps/contrib: fix elasticsearch showing results from the untranslated…
Browse files Browse the repository at this point in the history
… title field
  • Loading branch information
goapunk authored and m4ra committed Nov 7, 2023
1 parent b25d4ba commit 2059419
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
20 changes: 17 additions & 3 deletions apps/contrib/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

# Code below taken and modified from wagtails elasticsearch backend
class ElasticsearchResults(Elasticsearch8SearchResults):

def _get_es_body(self, for_count=False):
"""Override super method to add the highlight config."""
body = {
'query': self.query_compiler.get_query()
}
Expand All @@ -25,20 +27,32 @@ def _get_es_body(self, for_count=False):
return body

def _get_results_from_hits(self, hits):
pks = [hit['fields']['pk'][0] for hit in hits]
scores = {str(hit['fields']['pk'][0]): hit['_score'] for hit in hits}
"""Override the super method to add the highlights from elasticsearch.
Most of the code is copied directly from the super method. Also adds
checks to exclude pages which are draft or password-protected.
"""
pks = []
scores = {}
highlights = {}

results = {str(pk): None for pk in pks}
for hit in hits:
hs = hit['highlight']
hs.pop('content_type')
if '_all_text_boost_2_0' in hs:
# ignore hits from the _all_text field as it doesn't work with
# our translations
hs.pop('_all_text_boost_2_0')
for highlight in hs.values():
# we only need one highlight
if hit['fields']['pk'][0] not in highlights:
highlights[str(hit['fields']['pk'][0])] = highlight[0]
# only use search results which have a valid hit
pks.append(hit['fields']['pk'][0])
scores[str(hit['fields']['pk'][0])] = hit['_score']
break

results = {str(pk): None for pk in pks}
for obj in self.query_compiler.queryset.filter(pk__in=pks):
if not obj.live or obj.get_view_restrictions().exists():
continue
Expand Down
2 changes: 0 additions & 2 deletions apps/contrib/translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ def __get__(self, instance, owner):
def get_search_fields(fields: List[str]) -> List[str]:
"""Create a list of fields to search in the current language of the user.
Adds _edgengrams as otherwise autocomplete() won't work.
Returns:
List of fields with the correct language code set
"""
Expand Down
3 changes: 3 additions & 0 deletions changelog/_0002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### Fixed

- exclude search results from the untranslated title field

0 comments on commit 2059419

Please sign in to comment.