# Cannot execute scripts using \[aggs\] context

**URL:** <https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604>\
**Category:** Elasticsearch\
**Created:** [August 30, 2018, 3:51am UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604 "2018-08-30T03:51:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kennymarx0](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@kennymarx0](https://discuss.elastic.co/u/kennymarx0)\
**Post date:** [August 30, 2018, 3:51am UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604/1 "2018-08-30T03:51:03Z")

</div>

i wanna use aggs and script in es 6.3 like below

> GET /trxdetail-sdall/\_search?pretty  
> {  
> "aggs": {  
> "distinct\_OPPNAME": {  
> "terms": {  
> "script":"doc['OPPACC']+' '+doc['OPPNAME']"  
> }  
> }  
> },  
> "query": {  
> "terms": {  
> "OPPACC.keyword": [  
> "629005281011"  
> ]  
> }  
> }  
> }

but the result is

> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "cannot execute scripts using [aggs] context",  
> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "cannot execute scripts using [aggs] context"  
> }

my setting in elasticsearch.yml is

> script.allowed\_types: inline  
> script.allowed\_contexts: search, update

my question is how to set the config for using script in aggs? or there is no ways in using script for aggs?

---

<div class="post-metadata">

**Author:** ![Zhilong\_He](https://avatars.discourse-cdn.com/v4/letter/z/e19adc/32.png) [@Zhilong\_He](https://discuss.elastic.co/u/Zhilong_He)\
**Post date:** [August 30, 2018, 4:55am UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604/2 "2018-08-30T04:55:11Z")

</div>

Try This

```auto
{
  "aggs": {
    "distinct_OPPNAME": {
      "terms": {
        "script": "doc['OPPACC'].value + ' ' + doc['OPPNAME'].value"
      }
    }
  },
  "query": {
    "terms": {
      "OPPACC.keyword": [
        "629005281011"
      ]
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![kennymarx0](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@kennymarx0](https://discuss.elastic.co/u/kennymarx0)\
**Post date:** [August 30, 2018, 6:00am UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604/3 "2018-08-30T06:00:57Z")

</div>

> [@Zhilong\_He](#):
>
> { "aggs": { "distinct\_OPPNAME": { "terms": { "script": "doc['OPPACC'].value + ' ' + doc['OPPNAME'].value" } } }, "query": { "terms": { "OPPACC.keyword": ["629005281011"] } } }

thanks for reply。 i try it but the same result

> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "cannot execute scripts using [aggs] context",  
> "caused\_by": {  
> "type": "illegal\_argument\_exception",  
> "reason": "cannot execute scripts using [aggs] context"  
> }  
> }

i think mybe it is the setting problem in elasticsearch.yml?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 30, 2018, 3:27pm UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604/4 "2018-08-30T15:27:33Z")

</div>

There should be a full stack trace in your logs. Can you add that here?

---

<div class="post-metadata">

**Author:** ![rjernst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rjernst/32/6363_2.png) [@rjernst](https://discuss.elastic.co/u/rjernst)\
**Post date:** [August 30, 2018, 3:29pm UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604/5 "2018-08-30T15:29:32Z")

</div>

Also, you explicitly have only `search` and `update` scripts allowed in your `elasticsearch.yml`. You need to add `aggs` there if you want to do terms aggregations:

```auto
script.allowed_contexts: search, update, aggs

```

---

<div class="post-metadata">

**Author:** ![kennymarx0](https://avatars.discourse-cdn.com/v4/letter/k/8797f3/32.png) [@kennymarx0](https://discuss.elastic.co/u/kennymarx0)\
**Post date:** [September 2, 2018, 11:25am UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604/6 "2018-09-02T11:25:30Z")

</div>

thanks for reply.

my setting is

> script.allowed\_types: inline  
> script.allowed\_contexts: search,update,aggs

and used the script like below, then success! thanks alot .

> GET /trxdetail-sdall/\_search?pretty  
> {  
> "aggs": {  
> "distinct\_OPPNAME": {  
> "terms": {  
> "script": "doc['OPPACC.keyword'].value + ' ' + doc['OPPNAME.keyword'].value"  
> }  
> }  
> },  
> "query": {  
> "terms": {  
> "OPPACC.keyword": [  
> "629005281011"  
> ]  
> }  
> }  
> }

---

<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:** [September 30, 2018, 11:25am UTC](https://discuss.elastic.co/t/cannot-execute-scripts-using-aggs-context/146604/7 "2018-09-30T11:25:33Z")

</div>

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