ElasticSearch 使用 Inner_hits 查询Parent-Child(父子)文档 - 1.50新特性


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": "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 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值