弹性搜索让所有的父母都没有孩子 [英] Elasticsearch get all parents with no children

查看:69
本文介绍了弹性搜索让所有的父母都没有孩子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最初,我一直在尝试列出每个父母和一个最新的孩子。我已经知道如何执行以下查询

Originally I've been trying to get a list of parents and a single most recent child for each one of them. I've figured how to do that with the following query

{"query": 
  {"has_child": 
    {"inner_hits": 
      {"name": "latest", "size": 1, "sort":
        [{"started_at": {"order": "desc"}}]
      }, 
     "type": "child_type", 
     "query": {"match_all": {}}
    }
  }
}

但问题是 - 结果不包括没有孩子的父母。添加 min_children:0 也没有帮助。所以我想我可以查询所有没有孩子的父母,并将这两个单独的 OR 查询结合起来。但是我在构建这样的查询时遇到麻烦。谢谢任何建议。

But the problem is — the results do not include parents with no children. Adding min_children: 0 doesn't help either. So I thought I could make a query for all parents with no children and combine those two in a single OR query. But I'm having trouble building such a query. Would appreciate any suggestions.

推荐答案

这是您的查询:

    {
      "query":{
        "bool":{
          "should":[
            {
              "bool":{
                "must_not":[
                  {
                    "has_child":{
                      "type":"child_type",
                      "query":{
                        "match_all":{}
                      }
                    }
                  }
                ]
              }
            },
            {
              "has_child":{
                "inner_hits":{
                  "name":"latest",
                  "size":1, "sort":[{"started_at": {"order": "desc"}}]
                },
                "type":"child_type",
                "query":{
                  "match_all":{}
                }
              }
            }
          ]
        }
      }
    }

这篇关于弹性搜索让所有的父母都没有孩子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆