【问题标题】:Mapping configuration using elasticsearch-dsl DocType使用 elasticsearch-dsl DocType 映射配置
【发布时间】:2016-04-24 06:14:54
【问题描述】:

我正在开发一个简单的 nlp 工具,并使用 elasticsearch-dsl 作为 django 的 es 工具。

我将有两个“DocType”,Entity 和 Intent。我创建了自己的分析器:

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])

例如,在每个文档中,我都有一个自定义映射;

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

根据文档http://elasticsearch-dsl.readthedocs.org/en/latest/persistence.html#persistence 。 Entity.init() 将为此文档创建映射。它确实在我的 es 上创建了映射(仅适用于实体文档!:()。但是,在 Entity.init() 之后,我无法对 Intent 做同样的事情。 它给出了以下错误:

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

有什么办法解决这个问题吗?如果可能的话,我真的很想使用 Entity.init() 和 Intent.init()。

【问题讨论】:

    标签: django elasticsearch django-rest-framework elasticsearch-dsl


    【解决方案1】:

    您正在尝试在打开的 index 上为您的 Intent 类型定义新的 analyzers。这是不允许的,因此您会看到错误。

    你必须先close索引,然后运行

    Intent.init()
    

    并重新打开索引。您可以参考documentation了解更多信息。

    EDIT 1 您必须使用低级 python official client 来关闭索引。

    from elasticsearch import Elasticsearch
    
    es = Elasticsearch()
    es.indices.close(index="nlp")
    

    即使dsl library 使用它来映射test,因为它是在 python 客户端之上创建的。

    【讨论】:

    • 如何使用 elasticsearch-dsl 关闭它?我没找到方法,我已经考虑过了。
    • 很高兴我能提供帮助,请考虑将答案标记为已接受,因为它也会帮助未来的读者
    • @erhmutlu 使用es = connections.get_connection() 获取实例并用它关闭索引。
    猜你喜欢
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2016-09-25
    • 2019-04-17
    • 1970-01-01
    • 2016-02-18
    • 2016-02-21
    相关资源
    最近更新 更多