BO new via ES #3
This commit is contained in:
parent
1907494f15
commit
65a6ac0dd0
|
|
@ -165,19 +165,26 @@ class NewsQuerySet(TranslationQuerysetMixin):
|
|||
from search_indexes.utils import OBJECT_FIELD_PROPERTIES
|
||||
search_value = search_value.lower()
|
||||
search_fields = ('description', 'title', 'subtitle')
|
||||
search_keys = []
|
||||
field_to_boost = {
|
||||
'title': 3.0,
|
||||
'subtitle': 2.0,
|
||||
'description': 1.0,
|
||||
}
|
||||
search_keys = {}
|
||||
for field in search_fields:
|
||||
for locale in OBJECT_FIELD_PROPERTIES.keys():
|
||||
search_keys.append(f'{field}.{locale}')
|
||||
search_keys.update({f'{field}.{locale}': field_to_boost[field]})
|
||||
_query = None
|
||||
for key in search_keys:
|
||||
for key, boost in search_keys.items():
|
||||
if _query is None:
|
||||
_query = elasticsearch_dsl.Q('fuzzy', **{key: {'value': f'{search_value}', 'fuzziness': 'auto:2,5',
|
||||
'prefix_length': 0, 'max_expansions': 100}})
|
||||
_query = elasticsearch_dsl.Q('match', **{key: {'query': search_value, 'fuzziness': 'auto:2,5',
|
||||
'boost': boost}})
|
||||
else:
|
||||
_query |= elasticsearch_dsl.Q('fuzzy', **{key: {'value': f'{search_value}', 'fuzziness': 'auto:2,5',
|
||||
'prefix_length': 0, 'max_expansions': 100}})
|
||||
search = NewsDocument.search().filter(_query)[0:10000].execute()
|
||||
_query |= elasticsearch_dsl.Q('match', **{key: {'query': search_value, 'fuzziness': 'auto:2,5',
|
||||
'boost': boost,
|
||||
}})
|
||||
_query |= elasticsearch_dsl.Q('wildcard', **{key: {'value': f'*{search_value}*', 'boost': boost + 30}})
|
||||
search = NewsDocument.search().query('bool', should=_query)[0:10000].execute()
|
||||
ids = [result.meta.id for result in search]
|
||||
return self.filter(id__in=ids)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user