代码之家  ›  专栏  ›  技术社区  ›  andy

Elasticsearch-获取子文档的计数,即使计数为零

  •  2
  • andy  · 技术社区  · 7 年前

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

    (Elasticsearch v5)

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

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

    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 子句,返回所有父文档,无论它们是否有子文档(这是我想要的),但不包含 inner_hits ,所以我不知道计数。

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

    有没有一种方法可以在单个查询中实现这一点?

    1 回复  |  直到 7 年前
        1
  •  4
  •   Val    7 年前

    您需要使用 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": {}
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    现在,您将获得所有家长,无论他们是否有子女,还将获得每个家长有多少子女的信息。