elasticsearch问题,先定义Map,然后索引文档

af7jpaap  于 2021-06-10  发布在  ElasticSearch
关注(0)|答案(1)|浏览(390)

我正在尝试索引stackoverflow数据。首先,我用指定的Map和设置创建一个索引。

@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','u type':'stackoverflow','u id':1','status':400,'error':{'type':'非法参数\u exception','reason':'mapper for[body]与现有Map冲突:\n[mapper[body]具有不同的[analyzer]]','data':…}

nfeuvbwi

nfeuvbwi1#

看起来像 sof-question-answer2 索引已经用不同的分析器创建,可能是用默认的分析器 standard analyzer .
如果你运行命令 GET sof-question-answer2/_mapping 通过kibana你会看到的 Body field没有 whitespace 分析仪。
为了解决这个问题,你必须删除你的索引,更新你的Map,并重新索引你的数据(如果你有)。

相关问题