1

我尝试一个小例子

我创建一个映射

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"
            }
         }
      ]
   }
}

为什么,丢失了一对 countryA-branchA 和 countryA-branchB?

ps: 有时间再试试,可能是countryA和countryB冲突

4

2 回答 2

0

在这种情况下,Elasticsearch 会丢失相同_id且相同的_parent记录(记录由_id+标识_parent)。
如果没有_parent(分支),则仅使用_id字段来识别记录。

于 2017-02-28T09:37:28.320 回答
0

您“丢失”文档的原因是因为您提供了多个相同的文档_id。在 Elasticsearch 中,文档 ID 是唯一的,当您使用相同的文档 ID 插入两个文档时,第二次插入将覆盖并更新第一条记录。

如果执行GET /company/branch/branchA,您将能够看到该文档_version大于 1。

要解决这个问题,只需删除_id属性并让 Elasticsearch 自动生成 id,或者为每个文档选择唯一的文档 id。

于 2016-09-26T08:35:49.247 回答