# Elasticsearch Script merge the results of two aggregations

**URL:** <https://discuss.elastic.co/t/elasticsearch-script-merge-the-results-of-two-aggregations/22263>\
**Category:** Elasticsearch\
**Created:** [February 19, 2015, 12:49pm UTC](https://discuss.elastic.co/t/elasticsearch-script-merge-the-results-of-two-aggregations/22263 "2015-02-19T12:49:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![legend\_blue](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@legend\_blue](https://discuss.elastic.co/u/legend_blue)\
**Post date:** [February 19, 2015, 12:49pm UTC](https://discuss.elastic.co/t/elasticsearch-script-merge-the-results-of-two-aggregations/22263/1 "2015-02-19T12:49:12Z")

</div>

I use aggregrations on elasticsearch version 1.3.8. I use aggregation script for awhile today ıt didnt work. Please help ı cant find any solution :

This the mapping:

```
  "mappings": {
     "product": {
        "properties": {
           "brandId": {
              "type": "integer"
           },
           "brandIsActive": {
              "type": "boolean"
           },
           "brandLink": {
              "type": "string",
              "index": "not_analyzed"
           },
           "brandName": {
              "type": "string",
              "index": "not_analyzed"
           }

      }

   }

```

}

this my query:

post alias-test/product/\_search  
{  
"query": {  
"match\_all": {}  
},  
"aggs": {  
"Brand": {  
"terms": {  
"script": "doc['brandName'].value",  
"size": 0  
}  
}  
}  
}

This is the error:

{  
"error": "SearchPhaseExecutionException[Failed to execute phase [query\_fetch], all shards failed; shardFailures {[g][mizu-20150219142655][0]: RemoteTransportException[[Mammomax][inet[/172.31.37.148:9300]][search/phase/query+fetch]]; nested: SearchParseException[[mizu-20150219142655][0]: query[ConstantScore(_:_)],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match\_all":{}},"aggs":{"Brand":{"terms":{"script":"doc['brandName'].value"}}}}]]]; nested: ExpressionScriptCompilationException[Field [brandName] used in expression must be numeric]; }]",  
"status": 400  
}

The other query :

post test/product/\_search  
{  
"query": {  
"match\_all": {}  
},  
"aggs": {  
"Brand": {  
"terms": {  
"script": "doc['brandName'].value+'|'+doc['brandLink'].value",  
"size": 0  
}  
}  
}  
}

the error :

post test/product/\_search  
{  
"query": {  
"match\_all": {}  
},  
"aggs": {  
"Brand": {  
"terms": {  
"script": "doc['brandName'].value+'|'+doc['brandLink'].value",  
"size": 0  
}  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![legend\_blue](https://avatars.discourse-cdn.com/v4/letter/l/919ad9/32.png) [@legend\_blue](https://discuss.elastic.co/u/legend_blue)\
**Post date:** [February 19, 2015, 1:21pm UTC](https://discuss.elastic.co/t/elasticsearch-script-merge-the-results-of-two-aggregations/22263/2 "2015-02-19T13:21:32Z")

</div>

I second error :

{  
"error": "SearchPhaseExecutionException[Failed to execute phase  
[query\_fetch], all shards failed; shardFailures {[g][test][0]:  
RemoteTransportException[[Mammomax][inet[/192.168.1.8:9300]][search/phase/query+fetch]];  
nested: SearchParseException[[test][0]:  
query[ConstantScore(_:_)],from[-1],size[-1]: Parse Failure [Failed to  
parse source [{"query":{"match\_all":{}},"aggs":{"Brand":{"terms":{"script":"doc['brandName'].value+'|'+doc['brandLink'].value","size":0}}}}]]];  
nested: ExpressionScriptCompilationException[Failed to parse  
expression: doc['brandName'].value+'|'+doc['brandLink'].value];  
nested: ParseException[unexpected character ''' at position (23).];  
nested: NoViableAltException; }]",  
"status": 400  
}

2015-02-19 14:49 GMT+02:00 ali balci [balci.alii@gmail.com](mailto:balci.alii@gmail.com):

> I use aggregrations on elasticsearch version 1.3.8. I use aggregation script for awhile today ıt didnt work. Please help ı cant find any solution :
> 
> This the mapping:
> 
> ```
> "mappings": {
> "product": {
> "properties": {
> "brandId": {
> "type": "integer"
> },
> "brandIsActive": {
> "type": "boolean"
> },
> "brandLink": {
> "type": "string",
> "index": "not_analyzed"
> },
> "brandName": {
> "type": "string",
> "index": "not_analyzed"
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> this my query:
> 
> post alias-test/product/\_search  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "aggs": {  
> "Brand": {  
> "terms": {  
> "script": "doc['brandName'].value",  
> "size": 0  
> }  
> }  
> }  
> }
> 
> This is the error:
> 
> {  
> "error": "SearchPhaseExecutionException[Failed to execute phase [query\_fetch], all shards failed; shardFailures {[g][mizu-20150219142655][0]: RemoteTransportException[[Mammomax][inet[/172.31.37.148:9300]][search/phase/query+fetch]]; nested: SearchParseException[[mizu-20150219142655][0]: query[ConstantScore(_:_)],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"match\_all":{}},"aggs":{"Brand":{"terms":{"script":"doc['brandName'].value"}}}}]]]; nested: ExpressionScriptCompilationException[Field [brandName] used in expression must be numeric]; }]",  
> "status": 400  
> }
> 
> The other query :
> 
> post test/product/\_search  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "aggs": {  
> "Brand": {  
> "terms": {  
> "script": "doc['brandName'].value+'|'+doc['brandLink'].value",  
> "size": 0  
> }  
> }  
> }  
> }
> 
> the error :
> 
> post test/product/\_search  
> {  
> "query": {  
> "match\_all": {}  
> },  
> "aggs": {  
> "Brand": {  
> "terms": {  
> "script": "doc['brandName'].value+'|'+doc['brandLink'].value",  
> "size": 0  
> }  
> }  
> }  
> }
> 
> --  
> You received this message because you are subscribed to a topic in the  
> Google Groups "elasticsearch" group.  
> To unsubscribe from this topic, visit  
> [https://groups.google.com/d/topic/elasticsearch/aeNWfgYNVmA/unsubscribe](https://groups.google.com/d/topic/elasticsearch/aeNWfgYNVmA/unsubscribe).  
> To unsubscribe from this group and all its topics, send an email to  
> [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
Best Regards

ALİ BALCI  
Bilgisayar Mühendisligi  
Tel:0543 699 59 88  
FACEBOOK [http://www.facebook.com/alibalci.mail](http://www.facebook.com/alibalci.mail)  
BLOG [http://balciali.wordpress.com/](http://balciali.wordpress.com/)

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAMLQj%3D%2B1k0DiPwXdtxYNMjcK%2BMeQmzSAdw-9D0nrVpP-mPU4nw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAMLQj%3D%2B1k0DiPwXdtxYNMjcK%2BMeQmzSAdw-9D0nrVpP-mPU4nw%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![masaru](https://avatars.discourse-cdn.com/v4/letter/m/a9a28c/32.png) [@masaru](https://discuss.elastic.co/u/masaru)\
**Post date:** [February 20, 2015, 6:42am UTC](https://discuss.elastic.co/t/elasticsearch-script-merge-the-results-of-two-aggregations/22263/3 "2015-02-20T06:42:41Z")

</div>

Hi,

Looks like you are using lucene expression [1]. See the link for the limitation of lucene expression. Today it only supports numeric values.  
Since terms agg doesn’t have lang property, probably you have “script.default\_lang" set to “expression" in elasticsearch.yml?

FYI, if you put “lang”:”groovy” (and if configuration allows running dynamic groovy script), your query should work.  
But make sure you read release note [2] before turning on dynamic groovy scripting. (you can use groovy script without turning on dynamic scripting [3])

Masaru

[1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/modules-scripting.html#_lucene_expressions_scripts)  
[2] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/blog/elasticsearch-1-4-3-and-1-3-8-released/)  
[3] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/blog/running-groovy-scripts-without-dynamic-scripting/)

On February 19, 2015 at 22:21:41, ali balci ([balci.alii@gmail.com](mailto:balci.alii@gmail.com)) wrote:

> I second error :
> 
> {  
> "error": "SearchPhaseExecutionException[Failed to execute phase  
> [query\_fetch], all shards failed; shardFailures {[g][test][0]:  
> RemoteTransportException[[Mammomax][inet[/192.168.1.8:9300]][search/phase/query+fetch]];  
> nested: SearchParseException[[test][0]:  
> query[ConstantScore(_:_)],from[-1],size[-1]: Parse Failure [Failed to  
> parse source [{"query":{"match\_all":{}},"aggs":{"Brand":{"terms":{"script":"doc['brandName'].value+'|'+doc['brandLink'].value","size":0}}}}]]];  
> nested: ExpressionScriptCompilationException[Failed to parse  
> expression: doc['brandName'].value+'|'+doc['brandLink'].value];  
> nested: ParseException[unexpected character ''' at position (23).];  
> nested: NoViableAltException; }]",  
> "status": 400  
> }
> 
> 2015-02-19 14:49 GMT+02:00 ali balci :
> 
> > I use aggregrations on elasticsearch version 1.3.8. I use aggregation script for awhile  
> > today ıt didnt work. Please help ı cant find any solution :
> > 
> > This the mapping:
> > 
> > "mappings": {  
> > "product": {  
> > "properties": {  
> > "brandId": {  
> > "type": "integer"  
> > },  
> > "brandIsActive": {  
> > "type": "boolean"  
> > },  
> > "brandLink": {  
> > "type": "string",  
> > "index": "not\_analyzed"  
> > },  
> > "brandName": {  
> > "type": "string",  
> > "index": "not\_analyzed"  
> > }
> > 
> > }
> > 
> > }
> > 
> > }
> > 
> > this my query:
> > 
> > post alias-test/product/\_search  
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "aggs": {  
> > "Brand": {  
> > "terms": {  
> > "script": "doc['brandName'].value",  
> > "size": 0  
> > }  
> > }  
> > }  
> > }
> > 
> > This is the error:
> > 
> > {  
> > "error": "SearchPhaseExecutionException[Failed to execute phase [query\_fetch],  
> > all shards failed; shardFailures {[g][mizu-20150219142655][0]: RemoteTransportException[[Mammomax][inet[/172.31.37.148:9300]][search/phase/query+fetch]];  
> > nested: SearchParseException[[mizu-20150219142655][0]: query[ConstantScore(_:_)],from[-1],size[-1]:  
> > Parse Failure [Failed to parse source [{"query":{"match\_all":{}},"aggs":{"Brand":{"terms":{"script":"doc['brandName'].value"}}}}]]];  
> > nested: ExpressionScriptCompilationException[Field [brandName] used in expression  
> > must be numeric]; }]",  
> > "status": 400  
> > }
> > 
> > The other query :
> > 
> > post test/product/\_search  
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "aggs": {  
> > "Brand": {  
> > "terms": {  
> > "script": "doc['brandName'].value+'|'+doc['brandLink'].value",  
> > "size": 0  
> > }  
> > }  
> > }  
> > }
> > 
> > the error :
> > 
> > post test/product/\_search  
> > {  
> > "query": {  
> > "match\_all": {}  
> > },  
> > "aggs": {  
> > "Brand": {  
> > "terms": {  
> > "script": "doc['brandName'].value+'|'+doc['brandLink'].value",  
> > "size": 0  
> > }  
> > }  
> > }  
> > }
> > 
> > --  
> > You received this message because you are subscribed to a topic in the  
> > Google Groups "elasticsearch" group.  
> > To unsubscribe from this topic, visit  
> > [https://groups.google.com/d/topic/elasticsearch/aeNWfgYNVmA/unsubscribe](https://groups.google.com/d/topic/elasticsearch/aeNWfgYNVmA/unsubscribe).  
> > To unsubscribe from this group and all its topics, send an email to  
> > [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1af39cb0-893d-4a7d-b9e1-a061eed48de6%40googlegroups.com)
> > 
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).
> 
> --  
> Best Regards
> 
> ALİ BALCI  
> Bilgisayar Mühendisligi  
> Tel:0543 699 59 88  
> FACEBOOK  
> BLOG
> 
> --  
> You received this message because you are subscribed to the Google Groups "elasticsearch"  
> group.  
> To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAMLQj%3D%2B1k0DiPwXdtxYNMjcK%2BMeQmzSAdw-9D0nrVpP-mPU4nw%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAMLQj%3D%2B1k0DiPwXdtxYNMjcK%2BMeQmzSAdw-9D0nrVpP-mPU4nw%40mail.gmail.com).  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.54e6d79d.3a95f874.10ad%40citra.local](https://groups.google.com/d/msgid/elasticsearch/etPan.54e6d79d.3a95f874.10ad%40citra.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [July 6, 2017, 12:31am UTC](https://discuss.elastic.co/t/elasticsearch-script-merge-the-results-of-two-aggregations/22263/4 "2017-07-06T00:31:18Z")

</div>


