Elasticsearch-获取子文档的数量,即使数量为零 [英] Elasticsearch - get count of child docs, even if count is zero

查看:314
本文介绍了Elasticsearch-获取子文档的数量,即使数量为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(Elasticsearch v5)

(Elasticsearch v5)

数据模型有2种文档类型:父级和子级。

Data model has 2 document types: Parent and Child.

我发现我可以执行以下查询:

I have found that I can do the following query:

GET /stack/parent_doc/_search/
{
  "query": {
    "has_child": {
      "type": "child_doc",
      "inner_hits": {
        "_source": false,
        "size": 0
      },
      "query": {
        "match_all": {}
      }
    }
  }
}

至少有一个孩子的父母 及其子文件计数,如下所示。这已经很接近了,但是我也想让没有孩子的父母。

and I get back all parents which have at least one child and their counts of child documents, as below. This is pretty close, but I also want to have parents which have no children included.

{
    "took": 4077,
    "timed_out": false,
    "_shards": {
        "total": 20,
        "successful": 20,
        "failed": 0
    },
    "hits": {
        "total": 4974405,
        "max_score": 1,
        "hits": [{
                "_index": "stack",
                "_type": "parent_doc",
                "_id": "f34e4848-fd63-35a3-84d3-82cbc8796473",
                "_score": 1,
                "_source": {
                    "field": "value"
                },
                "inner_hits": {
                    "child_doc": {
                        "hits": {
                            "total": 1,
                            "max_score": 0,
                            "hits": []
                        }
                    }
                }
            },
            {
                "_index": "stack",
                "_type": "parent_doc",
                "_id": "f34e1ece-2274-35f6-af37-37138825db20",
                "_score": 1,
                "_source": {
                    "field": "value"
                },
                "inner_hits": {
                    "child_doc": {
                        "hits": {
                            "total": 5,
                            "max_score": 0,
                            "hits": []
                        }
                    }
                }
            }
        ]
    }
}

如果我删除了查询的 match_all 部分,则ES似乎会忽略 has_child 子句完全返回所有父文档,无论它们是否有子(这就是我想要的)b因为没有 inner_hits ,所以我不计算在内。

If I remove the match_all part of the query, then ES seems to ignore the has_child clause entirely, returning all Parent documents regardless of whether or not they have children (which is what I want) but without the inner_hits, so I don't get the count.

  "query": {
    "match_all": {}
  }

是否可以在单个查询中执行此操作?

Is there a way to do this in a single query?

推荐答案

您需要使用 bool / should 包括当前查询以及另一个对其求反的查询:

You need to use a bool/should including your current query plus another one that negates it:

POST /stack/_search/
{
  "query": {
    "bool": {
      "should": [
        {
          "has_child": {
            "type": "child_doc",
            "inner_hits": {
              "_source": false,
              "size": 0
            },
            "query": {
              "match_all": {}
            }
          }
        },
        {
          "bool": {
            "must_not": {
              "has_child": {
                "type": "child_doc",
                "query": {
                  "match_all": {}
                }
              }
            }
          }
        }
      ]
    }
  }
}

现在,您将获得所有父母,无论他们是否有孩子,还可以获得每个父母有多少个孩子的信息。

Now you'll get all parents, whether they have children or not, but also get the information of how many children each parent has.

这篇关于Elasticsearch-获取子文档的数量,即使数量为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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