0

我正在尝试索引 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', 'status': 400, 'error': {'type': 'illegal_argument_exception', 'reason': 'Mapper for [Body] 与现有映射冲突:\n[mapper [Body] 有不同的 [analyzer]]'}, '数据': ...}

4

1 回答 1

1

看起来sof-question-answer2index 已经用不同的分析器创建了,可能是默认的standard analyzer

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

我为了解决这个问题,您将不得不删除您的索引、更新您的映射并重新索引您的数据(如果有的话)。

于 2020-11-03T12:50:51.680 回答