Elasticsearch Java API(八)--搜索有相同父id的子文档

来源:互联网 发布:幂法求矩阵特征值例子 编辑:程序博客网 时间:2024/04/29 20:05

需求:

搜索具有相同父id的所有子文档.

数据:

   mapping:

    

 {      "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的employee:


curl GET company/employee/_search
    "query":{       "has_parent":{         "type":"branch",         "query":{           "term":{               "_parent":"london"           }         }        }     }}

这样搜没有搜到任何结果.各种尝试都没有成功,后来到stackoverflow上提问得到了答案,正确的搜索:



curl GET company/employee/_search
{  "query": {    "has_parent": {      "type": "branch",       "query": {        "ids": {             "values" : ["london"]        }      }    }  }

对应的java api:

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


我在stackoverflow上的提问链接:how to search child documents with the same parent id in Elasticsearch?

2 0