映射:
{
"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的子文档?
答案 0 :(得分:2)
has_parent
在OP中无效。在branch
类型中没有名为parent的字段。
以下是有效查询的示例:
{
"query": {
"has_parent": {
"type": "branch",
"query": {
"ids": {
"values" : ["london"]
}
}
}
}
答案 1 :(得分:0)
感谢上面的keety答案。我为查询添加了java api:
HasParentQueryBuilder hpqb=QueryBuilders
.hasParentQuery("branch",QueryBuilders.idsQuery()
.ids("london"));
如果你有同样的问题,希望它会有所帮助。