# Terms Aggregation With Script Field

**URL:** <https://discuss.elastic.co/t/terms-aggregation-with-script-field/108584>\
**Category:** Elasticsearch\
**Created:** [November 21, 2017, 3:40pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-script-field/108584 "2017-11-21T15:40:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![harshanarayana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harshanarayana/32/10213_2.png) [@harshanarayana](https://discuss.elastic.co/u/harshanarayana)\
**Post date:** [November 21, 2017, 3:40pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-script-field/108584/1 "2017-11-21T15:40:13Z")

</div>

Hi,

I'm using Elasticsearch JAVA DSL library with Version 5.6

I am trying to create an Elasticsearch Query that is similar to the one mentioned below.

```
{  
"aggs": {    
    "aggName": {      
        "terms": {           
            "script": "Math.round((doc['field1'].value - doc['field2'].value)/640000)"     
        }    
    }  
  }
}

```

The way I am creating a Terms aggregate now is as follows.

```
TermsAggregationBuilder subAggregation = AggregationBuilders
                            .terms("aggName")
                            .script(new Script("Math.round((doc['field1'].value - doc['field2'].value)/640000)")
                            .size(0)
                            .order(Terms.Order.term(true));

```

However, the response for this aggregate Builder is as follows.

```
{  
"aggs": {    
    "aggName": {      
        "terms": {           
            "script": {
                "source": "Math.round((doc['field1'].value - doc['field2'].value)/640000)",
                "lang": "painless"
          }
       }    
    }  
  }
}

```

And when the same query is run on Elasticsearch, I end up with the following Error.

```
{
    "error": {
        "root_cause": [
            {
                "type": "parsing_exception",
                "reason": "[terms] failed to parse field [script]",
                "line": 7,
                "col": 21
            }
        ],
        "type": "parsing_exception",
        "reason": "[terms] failed to parse field [script]",
        "line": 7,
        "col": 21,
        "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "[script] unknown field [source], parser not found"
        }
    },
    "status": 400
}

```

Can someone please direct me in the right way to generate the Expected JSON Structure that I want to use for querying the Elasticsearch using the JAVA SDK.

---

<div class="post-metadata">

**Author:** ![harshanarayana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harshanarayana/32/10213_2.png) [@harshanarayana](https://discuss.elastic.co/u/harshanarayana)\
**Post date:** [November 22, 2017, 5:27am UTC](https://discuss.elastic.co/t/terms-aggregation-with-script-field/108584/2 "2017-11-22T05:27:34Z")

</div>

I also tried the following way of creating the Aggregation and it raised the same issue.

```
TermsAggregationBuilder subAggregation = AggregationBuilders
                            .terms("aggName")
                            .script(new Script(ScriptType.INLINE, "painless", "Math.round((doc['field1'].value - doc['field2'].value)/640000)", Collections.emptyMap()))
                            .size(0)
                            .order(Terms.Order.term(true));

```

The String output for the above is generated as follows.

```
{  
"aggs": {    
    "aggName": {      
        "terms": {           
            "script": {
                "source": "Math.round((doc['field1'].value - doc['field2'].value)/640000)",
                "lang": "painless"
          }
       }    
    }  
  }
}
```

---

<div class="post-metadata">

**Author:** ![harshanarayana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/harshanarayana/32/10213_2.png) [@harshanarayana](https://discuss.elastic.co/u/harshanarayana)\
**Post date:** [November 22, 2017, 11:21am UTC](https://discuss.elastic.co/t/terms-aggregation-with-script-field/108584/3 "2017-11-22T11:21:45Z")

</div>

Never mind, got the Issue resolved by moving to ES Version 5.6.4.

Turns out the "inline" script was converted to "source" script in 5.6.X and someone downgraded the ES Node I was using to test into a 5.5.2 Node and all the "source" queries were failing to run.

---

<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:** [December 20, 2017, 11:21am UTC](https://discuss.elastic.co/t/terms-aggregation-with-script-field/108584/4 "2017-12-20T11:21:47Z")

</div>

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