Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping configuration using elasticsearch-dsl DocType

I'm developing a simple nlp tool, and using elasticsearch-dsl as a es tool with django.

I will have two "DocType", Entity and Intent. I have created my own analyzer which is:

turkish_stop = token_filter('turkish_stop', type='stop', stopwords="_turkish_")
turkish_lowercase = token_filter('turkish_lowercase', type='lowercase', language="turkish")
turkish_stemmer = token_filter('turkish_stemmer', type='stemmer', language='turkish')

turkish_analyzer = analyzer('turkish_analyzer', tokenizer='whitespace', filter=['apostrophe', 'asciifolding',
                                                                                turkish_lowercase, turkish_stop,
                                                                                turkish_stemmer])

In each doc, i have a custom mapping, for example;

class Entity(DocType):
    entity_synonyms = String(analyzer=es.turkish_analyzer, include_in_all=True)
    entity_key = String(index='not_analyzed', include_in_all=False)

    class Meta:
        index = es.ELASTICSEARCH_INDEX
        doc_type = es.ELASTICSEARCH_ENTITY_DOCTYPE

According to document http://elasticsearch-dsl.readthedocs.org/en/latest/persistence.html#persistence . Entity.init() will create mapping for this doc. It really creates mapping on my es (only for entity doc! :( ) . However, i could not do same thing with Intent, after Entity.init(). It gives below error:

IllegalOperation: You cannot update analysis configuration on an open index, you need to close index nlp first.

Any idea to solve this ? I really want to use Entity.init() and Intent.init() if it is possible.

like image 839
erhmutlu Avatar asked Mar 14 '23 14:03

erhmutlu


1 Answers

You are trying to define new analyzers for your Intent type on an open index. This is not allowed and hence you are seeing the error.

You have to first close the index, then run

Intent.init()

and reopen the index. You can refer to the documentation for more info.

EDIT 1 You have to use low level python official client to close the index.

from elasticsearch import Elasticsearch

es = Elasticsearch()
es.indices.close(index="nlp")

Even the dsl library uses it to test the mapping since it is created on top of python client.

like image 138
ChintanShah25 Avatar answered Mar 25 '23 04:03

ChintanShah25