[英]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.