Create custom analyzer filter for a index in py-elasticsearch-dsl
For create a custom filter we can use token_filter
:
turkish = analysis.token_filter('turkish_lowercase', type="lowercase", language="turkish")
We are creating a new lower_case
filter for turkish
language. Now, we need to create the analyzer:
turkish_lowercase = analyzer('turkish_lowercase', type = "custom", tokenizer="standard", filter=[turkish],)
We put turkish
filter directly in the filter
parameter, nothing more; we can review the result dictionary with the get_definition
function either filter
as analyzer
.
Finally we apply that analyzer in our Document
:
class Document(DocType): title = Text( analyzer=turkish_lowercase, # filter=turkish ) query = Percolator( ) # query is a percolator class Meta: index = 'titles' # index name doc_type = '_doc' def save(self, **kwargs): return super(Document, self).save(**kwargs)
We will get the next result:
{ "titles":{ "aliases":{ }, "mappings":{ "_doc":{ "properties":{ "query":{ "type":"percolator" }, "title":{ "type":"text", "analyzer":"turkish_lowercase" } } } }, "settings":{ "index":{ "number_of_shards":"5", "provided_name":"titles", "analysis":{ "filter":{ "turkish_lowercase":{ "type":"lowercase", "language":"turkish" } }, "analyzer":{ "turkish_lowercase":{ "filter":[ "turkish_lowercase" ], "type":"custom", "tokenizer":"standard" } } }, "number_of_replicas":"1", } } }}