更新 Elasticsearch 中现有字段的映射索引参数

Update mapping index parameter of existing field in Elasticsearch

我有映射

{
  "test" : {
    "mappings" : {
      "properties" : {
        "description" : {
          "type" : "text"
        },
        "location" : {
          "type" : "keyword",
          "index" : false
        },
        "title" : {
          "type" : "text"
        }
      }
    }
  }
}

并且我想将 location 字段的 index 参数更新为 true

我正在尝试

PUT /test/_mapping
{
  
    "properties": {
        "location": { 
            "type": "keyword",
            "index": true
        }
    }
  
}

我得到

{"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"Mapper for [location] conflicts with existing mapping:\n[mapper [location] has different [index] values]"}],"type":"illegal_argument_exception","reason":"Mapper for [location] conflicts with existing mapping:\n[mapper [location] has different [index] values]"},"status":400}

如何更新index参数?

您要实现的目标称为重大更改或冲突更改,这是不可能的,错误消息中提到了相同的内容。

想想 index 参数的作用以及它的破坏性变化的原因,来自 index docs

The index option controls whether field values are indexed. It accepts true or false and defaults to true. Fields that are not indexed are not queryable.

之前的索引值为 false,因此您现有的文档没有索引值且不可查询,现在您更改为 true,这没有意义,因为您的早期文档将没有索引值,这就是它称为重大更改的原因。

您必须使用新索引值创建一个新索引,您可以为此使用 reindex API