2017-01-09 117 views
0

弹性文档声明可以在查询中使用_parent字段(请参阅https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-parent-field.html)。ElasticSearch _parent查询

但是,我一直无法让它工作。下面是简单的测试:

PUT /company 
{ 
    "mappings": { 
    "branch": {}, 
    "employee": { 
     "_parent": { 
     "type": "branch" 
     } 
    } 
    } 
} 

POST /company/branch/_bulk 
{ "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" } 

PUT /company/employee/1?parent=london 
{ 
    "name": "Alice Smith", 
    "dob": "1970-10-24", 
    "hobby": "hiking" 
} 

验证该员工有_parent场:

GET /company/employee/_search 
{ 
    "query": { 
    "match_all": {} 
    } 
} 

回报

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 1, 
    "max_score": 1, 
    "hits": [ 
     { 
     "_index": "company", 
     "_type": "employee", 
     "_id": "1", 
     "_score": 1, 
     "_routing": "london", 
     "_parent": "london", 
     "_source": { 
      "name": "Alice Smith", 
      "dob": "1970-10-24", 
      "hobby": "hiking" 
     } 
     } 
    ] 
    } 
} 

但以下几点:

GET /company/employee/_search 
{ 
    "query": { 
    "term": { 
     "_parent":"london" 
    } 
    } 
} 

回报:

{ 
    "took": 1, 
    "timed_out": false, 
    "_shards": { 
    "total": 5, 
    "successful": 5, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 0, 
    "max_score": null, 
    "hits": [] 
    } 
} 

使用“has_parent”工作,但为什么不使用_parent工作,如文档中所述。

下面是使用has_parent的作品查询:

GET /company/employee/_search 
{ 
    "query": { 
    "has_parent": { 
     "parent_type":"branch", 
     "query":{ 
     "match_all": {} 
     } 
    } 
    } 
} 

我缺少什么?使用ElasticSearch 5.0.2。

回答

0

这是一个文档错误。根据breaking changes in 5.0_parent字段不再编入索引,因此无法在该字段上运行term查询。你要么需要使用has_parent查询或新parent_id查询来查找子文档:

POST /company/employee/_search 
{ 
    "query": { 
    "parent_id": { 
     "type": "employee", 
     "id": "london" 
    } 
    } 
} 

对于那些谁想要跟着,我已经filed an issue报告这一点,它得到了修复。更新的文档即将可用。

+0

有没有这样的运气? – Val