1

I have an aggregation query here that is:

Return the number of Records by Type, grouped by Creator in the last 6 months.

The query is as follows:

GET /test/records/_search?search_type=count
{
  "aggs": {
    "timeRange": {
      "filter": {
        "range": {
          "When": {
            "gte": "now-6M",
            "lte": "now"
          }
        }
      },
      "aggs": {
        "groupBy": {
          "terms": {
            "field": "Creator",
            "min_doc_count": 0
          },
          "aggs": {
            "counts": {
              "terms": {
                "field": "Type",
                "min_doc_count": 0
              }
            }
          }
        }
      }
    }
  }
}

And the Results appear as:

{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 261,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "timeRange": {
         "doc_count": 192,
         "groupBy": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "ff94d50a-9ced-4877-85cc-a08a00fd49f4",
                  "doc_count": 175,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 3,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 95
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 31
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 11
                        }
                     ]
                  }
               },
               {
                  "key": "be8ca900-0011-0002-1976-c737a7e00000",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": []
                  }
               },
               {
                  "key": "fae866a8-705e-e111-bd17-d6ec07ced130",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": []
                  }
               }
            ]
         }
      }
   }
}

From the results listed above, it appears that when there is a 0 count in a terms aggregation, it won't bother listing 0s for the terms in levels below it.

My question is: Is it possible to have it show 0 counts for in these scenarios?

Example desired output below:

{
   "took": 11,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 261,
      "max_score": 0,
      "hits": []
   },
   "aggregations": {
      "timeRange": {
         "doc_count": 192,
         "groupBy": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
               {
                  "key": "ff94d50a-9ced-4877-85cc-a08a00fd49f4",
                  "doc_count": 175,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 3,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 95
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 31
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 11
                        }
                     ]
                  }
               },
               {
                  "key": "be8ca900-0011-0002-1976-c737a7e00000",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 0
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 0
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 0
                        }
                     ]
                  }
               },
               {
                  "key": "fae866a8-705e-e111-bd17-d6ec07ced130",
                  "doc_count": 0,
                  "counts": {
                     "doc_count_error_upper_bound": 0,
                     "sum_other_doc_count": 0,
                     "buckets": [
                        {
                           "key": "9f937783-dc28-421c-a937-a0c201643aae",
                           "doc_count": 0
                        },
                        {
                           "key": "36e4b200-e8ca-47f5-b9bb-a09101058595",
                           "doc_count": 0
                        },
                        {
                           "key": "cf421f05-37b1-470e-9ab9-a0bb0100792d",
                           "doc_count": 0
                        }
                     ]
                  }
               }
            ]
         }
      }
   }
}
chronolinq
  • 113
  • 1
  • 11
  • 1
    I guess the problem is that you know that `type` can have possible values of `"9f937783-dc28-421c-a937-a0c201643aae"`, `"36e4b200-e8ca-47f5-b9bb-a09101058595"` and `"cf421f05-37b1-470e-9ab9-a0bb0100792d"` but elastic search does not know that. It will only list the count for types which are found in the result set (that is none for zero hits) – Clemens Klein-Robbenhaar Dec 01 '15 at 12:21
  • That makes sense. Thanks for the response – chronolinq Jan 13 '16 at 21:02

1 Answers1

0

This is answered as a comment to the main post by Clemens Klein-Robbenhaar. In short: I know what values to expect, but Elasticsearch does not. Once it counts that the group has an aggregation total of 0, it doesn't know what to search under that, because there is nothing.

chronolinq
  • 113
  • 1
  • 11