简体   繁体   English

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

[英]how to search child documents with the same parent id in Elasticsearch?

mapping: 制图:

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

documents of 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" }

documents of employee: 员工证件:

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

I want to find which documents have parent id london ,I tried the following query: 我想查找哪些文档的父ID为london ,我尝试了以下查询:

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

}

but ES return no results.how to search child documents with the same parent id in Elasticsearch? 但是ES没有返回结果。如何在Elasticsearch中搜索具有相同父ID的子文档?

The has_parent in not valid in the OP .There is no field called parent in the branch type . has_parent在OP中无效。 branch类型中没有称为parent的字段。

Below is an example of valid query : 以下是有效查询的示例:

{
  "query": {
    "has_parent": {
      "type": "branch", 
      "query": {
        "ids": {
             "values" : ["london"]
        }
      }
    }
  }

Thanks for keety's answer above.I add the java api for the query: 感谢上述keety的回答。我为查询添加了Java api:

HasParentQueryBuilder  hpqb=QueryBuilders
                            .hasParentQuery("branch",QueryBuilders.idsQuery()
                            .ids("london"));

Hope it can be helpful if you have the same question. 如果您有相同的问题,希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM