小编典典

如何在Elasticsearch中搜索具有相同父ID的子文档?

elasticsearch

映射:

    {
      "mappings": {
        "branch": {},
        "employee": {
          "_parent": {
            "type": "branch"
          }
        }
      }
   }

分行文件:

{ "index": { "_id": "london" }}
{ "name": "London Westminster", "city": "London", "country": "UK" }
{ "index": { "_id": "liverpool" }}
{ "name": "Liverpool Central", "city": "Liverpool", "country": "UK" }
{ "index": { "_id": "paris" }}
{ "name": "Champs Élysées", "city": "Paris", "country": "France" }

员工证件:

{ "index": { "_id": 1, "parent": "london" }}
{ "name": "Alice Smith", "dob": "1970-10-24", "hobby": "hiking" }

{ "index": { "_id": 2, "parent": "london" }}
{ "name": "Mark Thomas", "dob": "1982-05-16", "hobby": "diving" }

{ "index": { "_id": 3, "parent": "liverpool" }}
{ "name": "Barry Smith", "dob": "1979-04-01", "hobby": "hiking" }

{ "index": { "_id": 4, "parent": "paris" }}
{ "name": "Adrien Grand", "dob": "1987-05-11", "hobby": "horses" }

我想查找具有父ID的文档london,我尝试了以下查询:

    {
    "query":{
       "has_parent":{
         "type":"branch",
         "query":{
           "term":{
               "_parent":"london"
           }
         } 
       }
     }

}

但是ES没有返回结果。如何在Elasticsearch中搜索具有相同父ID的子文档?


阅读 635

收藏
2020-06-22

共1个答案

小编典典

has_parent在OP。有无效是没有叫父在外地branch类型。

以下是有效查询的示例:

{
  "query": {
    "has_parent": {
      "type": "branch", 
      "query": {
        "ids": {
             "values" : ["london"]
        }
      }
    }
  }
2020-06-22