在elasticsearch中,如何将每个文档的id替换为文档中另一个字段的值?
我认为您无法更改索引中现有文档的ID,但是可以使用映射中的path参数来重新索引它们。这是一个简单的例子。
我使用映射中定义中的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}
然后,如果我进行搜索,我可以看到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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句