# Aggregation not working on Scripted field

**URL:** <https://discuss.elastic.co/t/aggregation-not-working-on-scripted-field/99430>\
**Category:** Elasticsearch\
**Created:** [September 5, 2017, 1:06pm UTC](https://discuss.elastic.co/t/aggregation-not-working-on-scripted-field/99430 "2017-09-05T13:06:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaktigupta200](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@shaktigupta200](https://discuss.elastic.co/u/shaktigupta200)\
**Post date:** [September 5, 2017, 1:06pm UTC](https://discuss.elastic.co/t/aggregation-not-working-on-scripted-field/99430/1 "2017-09-05T13:06:55Z")

</div>

Hi Folks,

I am trying to perform bucket aggregation on scripted field but getting empty result while search result are coming fine but not aggregation result.

```auto
"aggregations": {
    "level": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [

      ]
    }
  } 

```

My query is

```auto
        {
          "size": 10,
          "query": {
            "bool": {
              "must": [
                {
                  "query_string": {
                    "query": "request_url: *.html*",
                    "analyze_wildcard": true
                  }
                },
                {
                  "range": {
                    "@timestamp": {
                      "from": "now-24h",
                      "to": "now"
                    }
                  }
                }
              ],
              "must_not": []
            }
          },
          "script_fields": {
            "my_base_url_scripted": {
              "script": "def path = doc['request_url'].value; if (path != null) { int firstQIndex = path.indexOf('.html'); if (firstQIndex > 0) { firstQIndex = firstQIndex + 5; return path.substring(0,firstQIndex); } } return path;"
            }
          },
        "aggs" : {
             "level" : {
                "terms" : {
                  "field" : "my_base_url_scripted",
                  "size" : 300000
                          }
                        }
          },
          "docvalue_fields": [
            "@timestamp"
          ]
        }

```

Kindly help.

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [September 6, 2017, 4:45pm UTC](https://discuss.elastic.co/t/aggregation-not-working-on-scripted-field/99430/2 "2017-09-06T16:45:09Z")

</div>

Aggregations do not support reusing `script_fields` definitions. You need to put the script directly into your aggregation definition:

```auto
        "aggs" : {
             "level" : {
                "terms" : {
                  "script" : "def path = doc['request_url'].value; if (path != null) { int firstQIndex = path.indexOf('.html'); if (firstQIndex > 0) { firstQIndex = firstQIndex + 5; return path.substring(0,firstQIndex); } } return path;",
                  "size" : 300000
                          }
                        }
          }

```

---

<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:** [October 4, 2017, 4:45pm UTC](https://discuss.elastic.co/t/aggregation-not-working-on-scripted-field/99430/3 "2017-10-04T16:45:14Z")

</div>

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