From 10d852d39d9df912db8109ed626ea186d7e90c31 Mon Sep 17 00:00:00 2001 From: Jacob Rief Date: Mon, 4 May 2020 16:40:31 +0200 Subject: [PATCH] handle analyzers from Document model --- shop/search/analyzers.py | 35 ++++++++++++++++++----------------- shop/search/documents.py | 8 +++++--- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/shop/search/analyzers.py b/shop/search/analyzers.py index aa4b84974..5c57e1d66 100644 --- a/shop/search/analyzers.py +++ b/shop/search/analyzers.py @@ -1,19 +1,20 @@ from elasticsearch_dsl.analysis import analyzer, token_filter -html_strip = analyzer('html_strip', - tokenizer='standard', - filter=['lowercase', 'stop', 'snowball'], - char_filter=['html_strip'], -) - -german_analyzer = analyzer('german_analyzer', - type='custom', - tokenizer='standard', - filter=[ - 'lowercase', - token_filter('asciifolding', type='asciifolding', preserve_original=False), - token_filter('german_stop', type='stop', language='german'), - token_filter('german_stemmer', type='snowball', language='german'), - ], - char_filter=['html_strip'], -) +body_analyzers = { + 'default': analyzer('default_analyzer', + tokenizer='standard', + filter=['lowercase', 'stop', 'snowball'], + char_filter=['html_strip'], + ), + 'de': analyzer('german_analyzer', + type='custom', + tokenizer='standard', + filter=[ + 'lowercase', + token_filter('asciifolding', type='asciifolding', preserve_original=False), + token_filter('german_stop', type='stop', language='german'), + token_filter('german_stemmer', type='snowball', language='german'), + ], + char_filter=['html_strip'], + ), +} diff --git a/shop/search/documents.py b/shop/search/documents.py index 63776378f..01b67dfa6 100644 --- a/shop/search/documents.py +++ b/shop/search/documents.py @@ -7,6 +7,7 @@ from django_elasticsearch_dsl import fields, Document, Index from shop.models.product import ProductModel +from shop.search.analyzers import body_analyzers class _ProductDocument(Document): @@ -71,18 +72,19 @@ class ProductDocument: """ Factory for building an elasticsearch-dsl Document class. This class """ - def __new__(cls, language=None, settings=None, analyzer=None): + def __new__(cls, language=None, settings=None): if language: index_name = 'products-{}'.format(language.lower()) doc_name = 'ProductDocument{}'.format(language.title()) + analyzer = body_analyzers.get(language, body_analyzers['default']) else: index_name = 'products' doc_name = 'ProductDocument' + analyzer = body_analyzers['default'] products_index = Index(index_name) if settings: products_index.settings(**settings) - kwargs = {'analyzer': analyzer} if analyzer else {} - attrs = {'_language': language, 'body': fields.TextField(**kwargs)} + attrs = {'_language': language, 'body': fields.TextField(analyzer=analyzer)} doc_class = type(doc_name, (_ProductDocument,), attrs) products_index.document(doc_class) return doc_class