Calculating percent

Hello,
I want to calculte the percent of a given type on my index: Ok_Count/total , I wrote this query
But it doesn't work as i excepted , could you help me to correct it

{
  
  "aggs": {
    "total":{
      "filter": {
        "bool": {
            "should": [{
              "term": {"FieldPresentOnAllDocument": "F"}
              }]
          }
        }
      },
    "OK": {
      "filter": {
        "bool": {
          "must": {
            "bool": {
              "should": [
                {
                  "bool": {
                    "must": {
                      "range": {
                        "MyField": {
                          "gte": 100,
                          "lte": 399
                        }
                      }
                    }
                  }
                },
                {
                  "bool": {
                    "should": [
                      {
                        "term": {"MyField": "0"}
                      },
                      {
                        "term": {"MyField": "5"}
                      },
                      {
                        "term": {"MyField": "6"}
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }                
  },
  "size":0
}

result is :

{
  "took" : 1101,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 10000,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "Ok" : {
      "doc_count" : 5342374
    },
    "total" : {
      "MyField" : 0
    }
  }
}

Though I'm not sure your overall picture, you can match all document with "match all query".

The following might not be what you need, you may also be able calculate the perecent itself by adding bucket script aggregation in a single query.

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