Elasticsearch 将每个文档的 id 更新为文档中另一个字段的值

Elasticsearch update id of each document to a value of another field in the document

在 elasticsearch 中,如何用文档中另一个字段的值替换每个文档的 id?

我认为您不能更改索引中现有文档的 ID,但可以使用映射中的 path parameter 重新索引它们。这是一个简单的例子。

我建立了一个简单的索引,使用映射中 _id 定义中的 path 参数,并添加了一些文档:

PUT /test_index
{
    "mappings": {
        "doc":{
            "_id": {
                "path": "number"
            },
            "properties": {
                "text_field": {
                    "type": "string"
                },
                "number": {
                    "type": "integer"
                }
            }
        }
    }
}

POST /test_index/doc/_bulk
{"index":{}}
{"text_field": "Apple TV","number":3}
{"index":{}}
{"text_field": "Apple iPhone","number":2}
{"index":{}}
{"text_field": "Apple MacBook","number":1}

然后如果我搜索,我可以看到 id 是按照我的要求设置的:

POST /test_index/_search
...
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 3,
      "max_score": 1,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
               "text_field": "Apple MacBook",
               "number": 1
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "2",
            "_score": 1,
            "_source": {
               "text_field": "Apple iPhone",
               "number": 2
            }
         },
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "3",
            "_score": 1,
            "_source": {
               "text_field": "Apple TV",
               "number": 3
            }
         }
      ]
   }
}

这是我使用的代码:

http://sense.qbox.io/gist/933bd839b2d524889e483f50c59c37ffaab2270a

您可以按照此处 id-mapping

中所述为 _id 定义一个映射来完成此操作