簡體   English   中英

Elasticsearch將每個文檔的ID更新為文檔中另一個字段的值

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

在Elasticsearch中,如何將每個文檔的ID替換為文檔中另一個字段的值?

我認為您無法更改索引中現有文檔的ID,但可以在映射中使用path參數為它們重新編制索引。 這是一個簡單的例子。

我使用映射中_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的映射來做到這一點,如此處id-mapping所述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM