diff --git a/apps/contrib/elasticsearch.py b/apps/contrib/elasticsearch.py index 9420684a..37e390ef 100644 --- a/apps/contrib/elasticsearch.py +++ b/apps/contrib/elasticsearch.py @@ -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() } @@ -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 diff --git a/apps/contrib/translations.py b/apps/contrib/translations.py index 2fa43742..6e8c3809 100644 --- a/apps/contrib/translations.py +++ b/apps/contrib/translations.py @@ -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 """ diff --git a/changelog/_0002.md b/changelog/_0002.md new file mode 100644 index 00000000..89565551 --- /dev/null +++ b/changelog/_0002.md @@ -0,0 +1,3 @@ +### Fixed + +- exclude search results from the untranslated title field