【问题标题】:Elasticsearch problem with pre-defined mapping and then index docs带有预定义映射然后索引文档的 Elasticsearch 问题
【发布时间】:2021-02-16 02:53:40
【问题描述】:

我正在尝试索引 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]]'}, 'data' : ...}

【问题讨论】:

  • 请向我们展示您正在索引的文档

标签: python elasticsearch indexing analyzer pyelasticsearch


【解决方案1】:

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

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

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

【讨论】:

  • 我在指定默认映射和设置的索引之前创建索引。
猜你喜欢
  • 1970-01-01
  • 2020-02-07
  • 2021-09-19
  • 1970-01-01
  • 2014-03-06
  • 2014-04-15
  • 2021-05-16
  • 2020-01-16
  • 1970-01-01
相关资源
最近更新 更多