ElasticSearch 使用 Inner_hits 查询Parent-Child(父子)文档 - 1.50新特性
2015-04-08 12:14
441 查看
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属性后,我们可以使用一次查询完成。
查询某用户的邮件列表:
返回:
最后十分感谢clintongormley 在github上的回答:https://github.com/elastic/elasticsearch/issues/10450
(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": "13724100993@189.cn" } }, {"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": "shinyke@163.com", "sender": "13724100993@189.cn" }, "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": "13724100993@189.cn", "labelId": "1,2,3" } } ] } } } } ] } }
最后十分感谢clintongormley 在github上的回答:https://github.com/elastic/elasticsearch/issues/10450
相关文章推荐
- ElasticSearch入门 第五篇:使用C#查询文档
- ElasticSearch 父子文档及多代父子文档(grandchild)解析之爸爸去哪儿了
- ElasticSearch入门 第五篇:使用C#查询文档
- Velocity API文档管理查询工具使用心得
- 嵌套Fragment的使用及遇到The specified child already has a parent. You must call removeView()问题的解决
- csdn 在c++ builder中,使用treeview实现对xml文档增加删除修改和查询等基本操作
- 在使用Hibernate时,因为一个查询需要更多的表连接而要使用SQL来解决性能问题。然而返回的结果集中包含了没有映射的Entity类中的表字段,在这个SQL中还有使用如何将层次关系的父子结点显示为横行
- C# 3.0新特性初步研究 Part6:使用查询表达式 _C#教程
- 使用XMLBean中的XMLCursor实现对XML文档的XPath和XQuery查询和修改
- 查询Oracle使用的特性
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- 【Apache Solr系列之四】Solr客户端SolrJ API使用文档-查询实例
- NHibernate文档翻译 第7章 示例: Parent/Child
- Android使用自定义对话框报错:The specified child already has a parent. You must call removeView() on the...
- 使用Xpath查询xml文档节点数据
- 请使用PL/SQL来按父子层次关系查询出表的所有数据
- C# 3.0新特性初步研究 Part6:使用查询表达式 _C#教程
- C# 3.0新特性初步研究 Part6:使用查询表达式
- 使用dynamic特性处理XML文档
- Fragment使用时出现错误:he specified child already has a parent. You must call removeView() on the child's p