Skip to content

Commit

Permalink
use language specific analyzer for body
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed May 4, 2020
1 parent 7300cf0 commit 2fcde39
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 227 deletions.
19 changes: 19 additions & 0 deletions shop/search/analyzers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
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'],
)
35 changes: 16 additions & 19 deletions shop/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,26 @@
from django.utils import translation

from django_elasticsearch_dsl import fields, Document, Index
from elasticsearch_dsl import analyzer

from shop.models.product import ProductModel


html_strip = analyzer(
'html_strip',
tokenizer='standard',
filter=['lowercase', 'stop', 'snowball'],
char_filter=['html_strip'],
)


class _ProductDocument(Document):
product_code = fields.KeywordField(
multi=True,
boost=5,
boost=3,
)

product_name = fields.TextField(
boost=3,
boost=2,
)

product_type = fields.TextField()

body = fields.TextField(
analyzer=html_strip,
)

class Django:
model = ProductModel
fields = ['id']
ignore_signals = True # performed by ProductModel.save()

def __str__(self):
return "{} {}: {}".format(self.product_type, self.id, self.product_name)
Expand All @@ -46,6 +34,9 @@ def get_queryset(self):
return queryset.filter(active=True)

def prepare_product_code(self, instance):
"""
Create a list of textual representation for product codes.
"""
has_valid_product_code = lambda obj: isinstance(getattr(obj, 'product_code', None), str)
variants = instance.get_product_variants()
product_codes = [v.product_code for v in variants if has_valid_product_code(v)]
Expand All @@ -69,11 +60,17 @@ def prepare_body(self, instance):
return body

def update(self, thing, refresh=None, action='index', parallel=False, **kwargs):
with translation.override(self._language):
if self._language:
with translation.override(self._language):
super().update(thing, refresh=None, action='index', parallel=False, **kwargs)
else:
super().update(thing, refresh=None, action='index', parallel=False, **kwargs)


class ProductDocument:
"""
Factory for building an elasticsearch-dsl Document class. This class
"""
def __new__(cls, language=None, settings=None, analyzer=None):
if language:
index_name = 'products-{}'.format(language.lower())
Expand All @@ -84,8 +81,8 @@ def __new__(cls, language=None, settings=None, analyzer=None):
products_index = Index(index_name)
if settings:
products_index.settings(**settings)
if analyzer:
products_index.analyzer(analyzer)
doc_class = type(doc_name, (_ProductDocument,), {'_language': language})
kwargs = {'analyzer': analyzer} if analyzer else {}
attrs = {'_language': language, 'body': fields.TextField(**kwargs)}
doc_class = type(doc_name, (_ProductDocument,), attrs)
products_index.document(doc_class)
return doc_class
3 changes: 2 additions & 1 deletion shop/search/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def get_document(self, language):

class ProductSearchViewMixin(SearchViewMixin):
"""
Mixin class to be added to the ProductListView so that autocompletion works.
Mixin class to be added to the ProductListView to restrict that list to entities matching
the query string.
"""
search_fields = ['product_name', 'product_code']

Expand Down
19 changes: 0 additions & 19 deletions shop/search/routers.py

This file was deleted.

32 changes: 0 additions & 32 deletions shop/search/serializers.py

This file was deleted.

156 changes: 0 additions & 156 deletions shop/search/views.py

This file was deleted.

0 comments on commit 2fcde39

Please sign in to comment.