我尝试了一个小例子
我创建了一个映射
PUT /company
{
"mappings": {
"country": {},
"branch": {
"_parent": {
"type": "country"
}
},
"employee": {
"_parent": {
"type": "branch"
}
}
}
}
并添加一些项目
POST /company/country/_bulk
{"index": {"_id": "countryA"}}
{"name": "0001"}
{"index": {"_id": "countryB"}}
{"name": "0008"}
{"index": {"_id": "countryC"}}
{"name": "0015"}
POST /company/branch/_bulk
{ "index": { "_id": "branchA", "parent": "countryA" }}
{ "name": "0002" }
{ "index": { "_id": "branchB", "parent": "countryA" }}
{ "name": "0005" }
{ "index": { "_id": "branchA", "parent": "countryB" }}
{ "name": "0009" }
{ "index": { "_id": "branchB", "parent": "countryB" }}
{ "name": "0012" }
{ "index": { "_id": "branchA", "parent": "countryC" }}
{ "name": "0016" }
{ "index": { "_id": "branchB", "parent": "countryC" }}
{ "name": "0019" }
但是,然后我运行请求
GET /company/branch/_search
结果只有4项分支
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "company",
"_type": "branch",
"_id": "branchA",
"_score": 1,
"_routing": "countryC",
"_parent": "countryC",
"_source": {
"name": "0016"
}
},
{
"_index": "company",
"_type": "branch",
"_id": "branchB",
"_score": 1,
"_routing": "countryC",
"_parent": "countryC",
"_source": {
"name": "0019"
}
},
{
"_index": "company",
"_type": "branch",
"_id": "branchA",
"_score": 1,
"_routing": "countryB",
"_parent": "countryB",
"_source": {
"name": "0009"
}
},
{
"_index": "company",
"_type": "branch",
"_id": "branchB",
"_score": 1,
"_routing": "countryB",
"_parent": "countryB",
"_source": {
"name": "0012"
}
}
]
}
}
为什么,失去对国家A-branchA& countryA-branchB吗
ps:我有时会再试一次,可能是countryA与countryB冲突
答案 0 :(得分:0)
你失败的原因'文档是因为您为多个文档提供了相同的_id
。在Elasticsearch中,文档ID是唯一的,当您使用相同的文档ID插入两个文档时,第二个插入将覆盖并更新第一个记录。
如果您执行GET /company/branch/branchA
,您将能够看到该文档_version
大于1。
要解决此问题,只需删除_id
属性,让Elasticsearch自动生成ID,或为每个文档选择唯一的文档ID。
答案 1 :(得分:0)
在这种情况下,Elasticsearch会丢失具有相同_id
和相同_parent
的记录(记录由_id
+ _parent
标识)。
如果没有_parent
(分支),则仅使用_id
字段来标识记录。