Q:

Ratio with elasticsearch

I have a list of customers with this structure: 
{
     "name" : "Toya Romano",
     "hungry" : false,
     "date" : 1420090500020
}

I would like to get the ratio of people who are hungry. How can I do it with an ElasticSearch query? I am running ES 2.3. 

A:

Rather a hacky approach because of this issue, but this should work:
{
  "size": 0,
  "aggs": {
    "whatever": {
      "filters": {
        "filters": [{}]
      },
      "aggs": {
        "all_people": {
          "filter": {}
        },
        "hungry_count": {
          "filter": {
            "term": {
              "hungry": true
            }
          }
        },
        "hungry_ratio": {
          "bucket_script": {
            "buckets_path": {
              "total_hungry": "hungry_count._count",
              "all": "all_people._count"
            },
            "script": "total_hungry/all"
          }
        }
      }
    }
  }
}

With the result like this:
     "buckets": [
        {
           "doc_count": 5,
           "all_people": {
              "doc_count": 5
           },
           "hungry_count": {
              "doc_count": 3
           },
           "hungry_ratio": {
              "value": 0.6
           }
        }
     ]

