Skip to content

Commit

Permalink
handle analyzers from Document model
Browse files Browse the repository at this point in the history
  • Loading branch information
jrief committed May 4, 2020
1 parent 1552f23 commit 10d852d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
35 changes: 18 additions & 17 deletions shop/search/analyzers.py
Original file line number Diff line number Diff line change
@@ -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'],
),
}
8 changes: 5 additions & 3 deletions shop/search/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

0 comments on commit 10d852d

Please sign in to comment.