【问题标题】:Elasticsearch - get count of child docs, even if count is zeroElasticsearch - 获取子文档的计数,即使计数为零
【发布时间】:2018-02-14 23:52:16
【问题描述】:

目标:对 Parent 类型的文档执行 1 次搜索,并在结果中包含每个父文档的子文档数。

(弹性搜索 v5)

数据模型有 2 种文档类型:Parent 和 Child。

我发现我可以做以下查询:

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

我会返回所有至少有一个孩子的父母及其孩子文档的数量,如下所示。这非常接近,但我也希望有没有孩子的父母。

{
    "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 子句,返回所有 Parent 文档,无论他们是否有孩子(这是我想要的)但没有 @ 987654325@,所以我不明白。

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

有没有办法在单个查询中做到这一点?

【问题讨论】:

    标签: elasticsearch elasticsearch-5


    【解决方案1】:

    您需要使用bool/should,包括您当前的查询以及另一个否定它的查询:

    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": {}
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
    

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

    【讨论】:

    • 有没有办法根据子长度(降序)对结果进行排序?
    • @PrijeshMeppayil 随时针对您的特定用例提出新问题。
    猜你喜欢
    • 1970-01-01
    • 2023-03-04
    • 2021-03-19
    • 1970-01-01
    • 2015-11-14
    • 2021-11-20
    • 2021-03-04
    • 1970-01-01
    • 2017-11-20
    相关资源
    最近更新 更多