# Cannot access doc field value in ElasticSearch aggregation script

**URL:** <https://discuss.elastic.co/t/cannot-access-doc-field-value-in-elasticsearch-aggregation-script/17756>\
**Category:** Elasticsearch\
**Created:** [May 27, 2014, 4:12pm UTC](https://discuss.elastic.co/t/cannot-access-doc-field-value-in-elasticsearch-aggregation-script/17756 "2014-05-27T16:12:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benjamin\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_smith/32/1513_2.png) [@Benjamin\_Smith](https://discuss.elastic.co/u/Benjamin_Smith)\
**Post date:** [May 27, 2014, 4:12pm UTC](https://discuss.elastic.co/t/cannot-access-doc-field-value-in-elasticsearch-aggregation-script/17756/1 "2014-05-27T16:12:12Z")

</div>

Anyone have some insight re: the following aggregation? I cannot access any  
properties of the document in my script.

With the following query:

{  
"query": {  
"filtered": {  
"filter": [],  
"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"fields": [  
"title"  
],  
"query": "test"  
}  
}  
]  
}  
}  
}  
},  
"aggs": {  
"my\_field": {  
"terms": {  
"field": "my\_field.name.raw",  
"script": "doc['title'].value"  
}  
}  
}  
}

I get the following error:

QueryPhaseExecutionException[[index\_name][2]: query[title:test],from[0],size  
[10]: Query Failed [Failed to execute main query]]; nested:  
PropertyAccessException[[Error: doc['title'].value: null] [Near : {... doc[  
'title'].value ....}] ^ [Line: 1, Column: 1]]; nested:  
InvocationTargetException; nested: ArrayIndexOutOfBoundsException;

I also try using \_source.title, doc.get('title').value, and other  
combinations of the doc['field'] syntax - none are working for me. Is there  
a special nuance for using script within an aggregation? Am I missing  
something obvious?

This fails on both ES 1.1.1 and 1.2.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/5177724b-0055-464b-8439-e8119a44be1f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5177724b-0055-464b-8439-e8119a44be1f%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Benjamin\_Smith](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/benjamin_smith/32/1513_2.png) [@Benjamin\_Smith](https://discuss.elastic.co/u/Benjamin_Smith)\
**Post date:** [May 27, 2014, 4:46pm UTC](https://discuss.elastic.co/t/cannot-access-doc-field-value-in-elasticsearch-aggregation-script/17756/2 "2014-05-27T16:46:14Z")

</div>

Looks like I can answer my own question. From the docs[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html#\_values\_source](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html#_values_source)  
:

When both field and script settings are configured for the aggregation, the

> script will be treated as a value script. While normal scripts are  
> evaluated on a document level (i.e. the script has access to all the data  
> associated with the document), value scripts are evaluated on the value  
> level. In this mode, the values are extracted from the configured field and  
> the script is used to apply a "transformation" over these value/s.

I was specifying a "field" param, meaning I was dealing with a value script.

On Tuesday, May 27, 2014 12:12:12 PM UTC-4, Benjamin Smith wrote:

> Anyone have some insight re: the following aggregation? I cannot access  
> any properties of the document in my script.
> 
> With the following query:
> 
> {  
> "query": {  
> "filtered": {  
> "filter": ,  
> "query": {  
> "bool": {  
> "must": [  
> {  
> "query\_string": {  
> "fields": [  
> "title"  
> ],  
> "query": "test"  
> }  
> }  
> ]  
> }  
> }  
> }  
> },  
> "aggs": {  
> "my\_field": {  
> "terms": {  
> "field": "my\_field.name.raw",  
> "script": "doc['title'].value"  
> }  
> }  
> }  
> }
> 
> I get the following error:
> 
> QueryPhaseExecutionException[[index\_name][2]: query[title:test],from[0],  
> size[10]: Query Failed [Failed to execute main query]]; nested:  
> PropertyAccessException[[Error: doc['title'].value: null] [Near : {... doc  
> ['title'].value ....}] ^ [Line: 1, Column: 1]]; nested:  
> InvocationTargetException; nested: ArrayIndexOutOfBoundsException;
> 
> I also try using \_source.title, doc.get('title').value, and other  
> combinations of the doc['field'] syntax - none are working for me. Is there  
> a special nuance for using script within an aggregation? Am I missing  
> something obvious?
> 
> This fails on both ES 1.1.1 and 1.2.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/06f76720-fcf5-4536-ad18-54483807a1e7%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/06f76720-fcf5-4536-ad18-54483807a1e7%40googlegroups.com).  
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, 1:26am UTC](https://discuss.elastic.co/t/cannot-access-doc-field-value-in-elasticsearch-aggregation-script/17756/3 "2017-07-06T01:26:36Z")

</div>


