在elasticsearch中,如何用文档中另一个字段的值替换每个文档的id?
答案 0 :(得分:2)
我认为您不能更改索引中现有文档的ID,但您可以使用映射中的path parameter重新索引它们。这是一个简单的例子。
我使用映射中path
定义中的_id
参数设置了一个简单的索引,并添加了一些文档:
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}
然后,如果我搜索,我可以看到ids是按照我的要求设置的:
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
答案 1 :(得分:0)
您可以通过定义_id
的映射来完成此操作,如id-mapping