inner_hits文档:http://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#parent-child-inner-hits (ElasticSearch 1.50+ 可用)
在使用搜索“has-child”搜索父文档时,一般情况只返回子文档符合条件的父文档。用 Inner-hits 则可以把父子文档同时返回——既返回父文档,也返回匹配has-child条件的子文档,相当于在父子之间join了。
这是一个相当有用的特性,假设我们使用父文档存储邮件内容,子文档存储每个邮件拥有者的信息以及对于此用户这封邮件的状态。搜索某个账户的邮件列表时,我们希望搜索到邮件内容和邮件状态,可以设想假如没有Inner-hits,我们必须得分两次查询,因为邮件内容和邮件状态分别存放在父文档和子文档中。而有了Inner_hits属性后,我们可以使用一次查询完成。
查询某用户的邮件列表:
curl -XGET 'http://localhost:9200/hermes/email/_search/?pretty=true' -d '{ "query": { "has_child": { "type": "email_owner", "query": { "bool": { "must": [ { "term": { "owner": "[email protected]" } }, {"term": {"labelId": "1"} } ]} }, //注意此处 "inner_hits": {} } } }'
返回:
{ "took": 118, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 1, "hits": [ { "_index": "hermes", "_type": "email", "_id": "1", "_score": 1, "_source": { "subject": "广州市十大新闻", "content": "1. 长春市长春药店。2. 一次性交易一百元。", "recepter": "[email protected]", "sender": "[email protected]" }, "inner_hits": { //inner_hits返回了has_child中匹配上的子文档 "email_owner": { "hits": { "total": 1, "max_score": 1.592944, "hits": [ { "_index": "hermes", "_type": "email_owner", "_id": "1", "_score": 1.592944, "_source": { "owner": "[email protected]", "labelId": "1,2,3" } } ] } } } } ] } }
最后十分感谢clintongormley 在github上的回答:https://github.com/elastic/elasticsearch/issues/10450