0

I am new to Elastic Search and am trying to make a query with Metric aggregation for my docs. But when I add the field: min_doc_count=1 for my sum metric aggregation, I get an error: `

{
   "error": {
      "root_cause": [
         {
            "type": "illegal_argument_exception",
            "reason": "[sum] unknown field [min_doc_count], parser not found"
         }
      ],
      "type": "illegal_argument_exception",
      "reason": "[sum] unknown field [min_doc_count], parser not found"
   },
   "status": 400
}

`

What am I missing here?

`

{
    "aggregations" : {
        "myKey" : {
            "sum" : {
                "field" : "field1",
                "min_doc_count": 1
            }
        }
    }
}

`

2 Answers2

1

I'm not sure why/where you have the sum keyword?

The idea of min_doc_count is to make sure buckets returned by a given aggs query contain at least N documents, the example below would only return subject buckets for subjects that appear in 10 or more documents.

GET _search
    {
        "aggs" : {
            "docs_per_subject" : {
                "terms" : {
                    "field" : "subject",
                    "min_doc_count": 10
                }
            }
        }
    }

So with that in mind, yours would refactor to the following... Although when setting min_doc_count to 1, it's not really necessary to keep the parameter at all.

GET _search
    {
        "aggs" : {
            "docs_per_subject" : {
                "terms" : {
                    "field" : "field1",
                    "min_doc_count": 1
                }
            }
        }
    }
greggers
  • 181
  • 1
  • 7
  • I think what I get out of your response is that min_doc_count makes more sense for a Bucket Aggregation scenario, rather than setting for a Metric Aggregation. Whereas it makes sense to me, the basic reason for me trying to do this was to get a NULL value for the Sum Aggregation for NoRecordsHit instead of Zero which does not tell me a lot unless I also look at the totalHitsCount as well. – Milind Jindal Oct 13 '17 at 22:50
  • @MilindJindal if you wish to sum only non-zero values of `field` you can filter those zero-values out in a query section. See my answer below. – Eli Oct 15 '17 at 10:16
  • @greggers: my actual use case is to get a Null Value back from a aggregation query for sum, IF I have no doc hits. I don't want to skip the Zero values. – Milind Jindal Oct 16 '17 at 18:51
  • Can we take a step back as I don't understand your comment? – greggers Oct 16 '17 at 19:51
  • Can you perhaps provide some example data and what you want to get from it? – greggers Oct 16 '17 at 19:57
  • Lets say the filter for my query is too strict. And no document satisfies it. the value for Sum Metric that I get is 0. I want the value returned to be "null" – Milind Jindal Oct 18 '17 at 00:33
0

If you wish to sum only non-zero values of field you can filter those zero-values out in a query section:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "field": {
              "gt": 0
            }
          }
        }
      ]
    }
  },
  "aggregations": {
    "myKey": {
      "sum": {
        "field": "field1"
      }
    }
  }
}

See Bool Query and Range Term

Eli
  • 4,576
  • 1
  • 27
  • 39