微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有预定义映射和索引文档的Elasticsearch问题

如何解决带有预定义映射和索引文档的Elasticsearch问题

我正在尝试为stackoverflow数据建立索引。首先,我创建具有指定映射和设置的索引。

    @classmethod
    def create_index_with_set_map(cls,name,elasticsearch):
        """
        create index with default mappings and settings(and analyzer).

    Argument:
    name -- The name of the index.
    elasticsearch -- Elasticsearch instance for connection.
        """
     
        mappings = "mappings": {
            "properties": {
                "Body": {
                    "type": "text","analyzer": "whitespace","fields": {
                        "keyword": {
                            "type": "keyword","ignore_above": 256
                        }
                    }
                }}}
       
        settings = {
            "analysis": {
                "analyzer": {
                    "default": {
                        "type": "whitespace"
                    }
                }
            }
        }

        body = {
            "settings": settings,"mappings": mappings

        }
        res = elasticsearch.indices.create(index=name,body=body)
        print(res)

然后我尝试批量索引我的文档:

@classmethod
    def start_index(cls,index_name,index_path,elasticsearch,doc_type):
        """
    This function is using bulk index.

    Argument:
    index_name -- the name of index
    index_path -- the path of xml file to index
    elasticsearch -- Elasticsearch instance for connection
    doc_type -- doc type 

    Returns:
    """

        for lines in Parser.xml_reader(index_path):
            actions = [
                {
                    "_index": index_name,"_type": doc_type,"_id": Parser.post_parser(line)['Id'],"_source":  Parser.post_parser(line)


                }
                for line in lines if Parser.post_parser(line) is not None
            ]

            helpers.bulk(elasticsearch,actions)

给出错误: ('500个文档未能建立索引。',[{'index':{'_index':'sof-question-answer2','_type':'Stackoverflow','_id':1','状态' :400,'错误':{'类型':'illegal_argument_exception','原因':'[Body]的映射器与现有映射冲突:\ n [mapper [Body]具有不同的[analyzer]]'},'数据' :...}

解决方法

似乎sof-question-answer2索引已使用其他分析器创建,可能是默认的standard analyzer

如果您通过kibana运行命令GET sof-question-answer2/_mapping,则会看到Body字段没有whitespace分析器。

我要解决此问题,您将必须删除索引,更新映射并为数据重新索引(如果有)。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。