# Elasticsearch bucket\_script agg

**URL:** <https://discuss.elastic.co/t/elasticsearch-bucket-script-agg/259138>\
**Category:** Elasticsearch\
**Created:** [December 18, 2020, 8:23pm UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-agg/259138 "2020-12-18T20:23:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alexkl](https://avatars.discourse-cdn.com/v4/letter/a/858c86/32.png) [@Alexkl](https://discuss.elastic.co/u/Alexkl)\
**Post date:** [December 18, 2020, 8:23pm UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-agg/259138/1 "2020-12-18T20:23:03Z")

</div>

Hello,

There is something i don't understand.  
I want to get a daily hit ratio calculated on the hit/miss stored in a particular field on my index.

I tried to reproduce the doc example adding an extra agg.  
When i try to use bucket\_script as an aggregation:

```auto
GET /varnish/_search
{
    "query": {
        "bool": {
            "must": [
                {
                    "range": {
                        "time_req": {
                            "from": "1608021054803",
                            "to": "1608321154803",
                            "include_lower": true,
                            "include_upper": true,
                            "format": "epoch_millis",
                            "boost": 1
                        }
                    }
                },
                {
                    "term": {
                        "status": {
                            "value": "200",
                            "boost": 1
                        }
                    }
                },
                {
                    "term": {
                        "varnish.side": {
                            "value": "c",
                            "boost": 1
                        }
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "vhost": {
                                        "value": "mysite1.myhome.com",
                                        "boost": 1
                                    }
                                }
                            },
                            {
                                "term": {
                                    "vhost": {
                                        "value": "mysite2.myhome.com",
                                        "boost": 1
                                    }
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "boost": 1
                    }
                }
            ],
            "must_not": [
                {
                    "term": {
                        "headers_req.X-shard-relayed-request": {
                            "value": "1",
                            "boost": 1
                        }
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1
        }
    },
    "aggs": {
        "whatevertoaddanextraagglevel": {
            "filters": {
              "filters" : {
               "varnish" : { "match" : { "app_type" : "varnish" }}
              }
            },
            "aggs": {
                "hits": {
                    "filter": {
                        "term": {
                            "headers_resp.X-Cache": {
                                "value": "HIT",
                                "boost": 1
                            }
                        }
                    }
                },
                "miss": {
                    "filter": {
                        "term": {
                            "headers_resp.X-Cache": {
                                "value": "MISS",
                                "boost": 1
                            }
                        }
                    }
                },
                "hitratio": {
                    "bucket_script": {
                        "buckets_path": {
                            "miss": "miss",
                            "hits": "hits"
                        },
                        "script": {
                            "source": "params.miss / (params.hits+ params.miss) * 100",
                            "lang": "painless"
                        },
                        "gap_policy": "skip"
                    }
                }
            }
        }
    }
}

```

I Got this message:

```auto
{
  "error" : {
    "root_cause" : [],
    "type" : "search_phase_execution_exception",
    "reason" : "",
    "phase" : "fetch",
    "grouped" : true,
    "failed_shards" : [],
    "caused_by" : {
      "type" : "aggregation_execution_exception",
      "reason" : "buckets_path must reference either a number value or a single value numeric metric aggregation, got: [InternalFilter] at aggregation [hits]"
    }
  },
  "status" : 500
}

```

Without the bucket\_script aggregation:

```auto
  "aggregations" : {
    "whatevertoaddanextraagglevel" : {
      "buckets" : {
        "varnish" : {
          "doc_count" : 38943610,
          "hits" : {
            "doc_count" : 25153481
          },
          "miss" : {
            "doc_count" : 12456794
          }
        }
      }

```

i tried varnish\>hit, whatevertoaddanextraagglevel\>hit ... it doesn't exists  
Do you have any idea what i am missing ?

Thanks !

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 15, 2021, 8:23pm UTC](https://discuss.elastic.co/t/elasticsearch-bucket-script-agg/259138/2 "2021-01-15T20:23:11Z")

</div>

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