2016-08-04 34 views
1

映射:分支如何在Elasticsearch中搜索具有相同父ID的子文档?

{ 
     "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返回no results.how在Elasticsearch中搜索具有相同父ID的子文档?

回答

2

has_parent在OP中无效。在branch类型中没有称为父项的字段。

下面是有效的查询的例子:

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

谢谢。它的工作原理。 –

+0

这个问题一直困扰着我的仆人days.Can你帮我吗?@keety http://stackoverflow.com/questions/38886939/add-highlight-does-not-work-with-has-child-query-in -elasticsearch-2-3-3 –

+0

肯定会给它一个镜头 – keety

0

感谢keety的回答above.I添加Java API来查询:

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

希望能有所帮助,如果你有同样的问题。

相关问题