"missing" agg weird results

I get some odd looking results from this query:

POST my-index/_search?pretty
{
  "size": 0,
  "aggs": {
    "a": {
      "terms": {
        "field": "foo.a"
      },
      "aggs": {
        "missing_b": {
          "missing": {
            "field": "foo.b"
          }
        },
        "having_b": {
          "filter": {
            "exists": {
              "field": "foo.b"
            }
          }
        }
      }
    }
  }
}

I would have thought that the missing_b and having_b aggregation instances
would have been complementary, but these are the results that I get:

{
  "took" : 25,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 330621654,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "a" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "a2",
          "doc_count" : 327720,
          "having_b" : {
            "doc_count" : 136435
          },
          "missing_b" : {
            "doc_count" : 327720
          }
        },
        {
          "key" : "a1",
          "doc_count" : 102243,
          "having_b" : {
            "doc_count" : 52350
          },
          "missing_b" : {
            "doc_count" : 102243
          }
        },
        {
          "key" : "a0",
          "doc_count" : 13839,
          "having_b" : {
            "doc_count" : 0
          },
          "missing_b" : {
            "doc_count" : 13839
          }
        },
        {
          "key" : "a3",
          "doc_count" : 8108,
          "having_b" : {
            "doc_count" : 2787
          },
          "missing_b" : {
            "doc_count" : 8108
          }
        },
        {
          "key" : "a4",
          "doc_count" : 787,
          "having_b" : {
            "doc_count" : 0
          },
          "missing_b" : {
            "doc_count" : 787
          }
        }
      ]
    }
  }
}

Implementing missing agg myself using filter agg:

POST my-index/_search?pretty
{
  "size": 0,
  "aggs": {
    "a": {
      "terms": {
        "field": "foo.a"
      },
      "aggs": {
        "missing_b": {
          "filter": {
            "bool": {
              "must_not": {
                "exists": {
                  "field": "foo.b"
                }
              }
            }
          }
        },
        "having_b": {
          "filter": {
            "exists": {
              "field": "foo.b"
            }
          }
        }
      }
    }
  }
}

Seems to give me the expected result:

{
  "took" : 67590,
  "timed_out" : false,
  "_shards" : {
    "total" : 10,
    "successful" : 10,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 330621654,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "a" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "a2",
          "doc_count" : 327720,
          "having_b" : {
            "doc_count" : 136435
          },
          "missing_b" : {
            "doc_count" : 191285
          }
        },
        {
          "key" : "a1",
          "doc_count" : 102243,
          "having_b" : {
            "doc_count" : 52350
          },
          "missing_b" : {
            "doc_count" : 49893
          }
        },
        {
          "key" : "a0",
          "doc_count" : 13839,
          "having_b" : {
            "doc_count" : 0
          },
          "missing_b" : {
            "doc_count" : 13839
          }
        },
        {
          "key" : "a3",
          "doc_count" : 8108,
          "having_b" : {
            "doc_count" : 2787
          },
          "missing_b" : {
            "doc_count" : 5321
          }
        },
        {
          "key" : "a4",
          "doc_count" : 787,
          "having_b" : {
            "doc_count" : 0
          },
          "missing_b" : {
            "doc_count" : 787
          }
        }
      ]
    }
  }
}

Should this be reported as a bug instead?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.