Elasticsearch - get count of child docs, even if c

2019-07-12 08:53发布

问题:

Objective: execute 1 search against documents of Parent type, and include in the results the count of children for each parent document.

(Elasticsearch v5)

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

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?

回答1:

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.