# How to access document fields from script?

**URL:** <https://discuss.elastic.co/t/how-to-access-document-fields-from-script/6332>\
**Category:** Elasticsearch\
**Created:** [January 9, 2012, 10:37pm UTC](https://discuss.elastic.co/t/how-to-access-document-fields-from-script/6332 "2012-01-09T22:37:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harish\_Rawat](https://avatars.discourse-cdn.com/v4/letter/h/53a042/32.png) [@Harish\_Rawat](https://discuss.elastic.co/u/Harish_Rawat)\
**Post date:** [January 9, 2012, 10:37pm UTC](https://discuss.elastic.co/t/how-to-access-document-fields-from-script/6332/1 "2012-01-09T22:37:38Z")

</div>

Hi

I am trying to access document fields in the script based sorting

curl -XPOST '[http://localhost:9200/users/user/\_search](http://localhost:9200/users/user/_search)' -d '{  
"query" : { "term" : { "group" : "dpe" }},  
"sort" : { "\_script" : { "script" : "doc['first\_name'].stringValue",  
"type" : "string", "order" : "asc"}}  
}'

and I get following exception

nested: ElasticSearchIllegalArgumentException[No field found for  
[org.elasticsearch.index.field.data.strings.StringDocFieldData@2ad6d4be]]

I get this error if I try to accessing document fields using  
doc['field\_name'].stringValue. I am able to access values by using  
doc.field\_name syntax though.

Thanks  
Harish

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 10, 2012, 9:02am UTC](https://discuss.elastic.co/t/how-to-access-document-fields-from-script/6332/2 "2012-01-10T09:02:50Z")

</div>

Its because of curl and the use of ' (which is used after -d as well).

On Tue, Jan 10, 2012 at 12:37 AM, Harish Rawat [harish@groupon.com](mailto:harish@groupon.com) wrote:

> Hi
> 
> I am trying to access document fields in the script based sorting
> 
> curl -XPOST '[http://localhost:9200/users/user/\_search](http://localhost:9200/users/user/_search)' -d '{  
> "query" : { "term" : { "group" : "dpe" }},  
> "sort" : { "\_script" : { "script" : "doc['first\_name'].stringValue",  
> "type" : "string", "order" : "asc"}}  
> }'
> 
> and I get following exception
> 
> nested: ElasticSearchIllegalArgumentException[No field found for  
> [org.elasticsearch.index.field.data.strings.StringDocFieldData@2ad6d4be]]
> 
> I get this error if I try to accessing document fields using  
> doc['field\_name'].stringValue. I am able to access values by using  
> doc.field\_name syntax though.
> 
> Thanks  
> Harish

---

<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, 3:43am UTC](https://discuss.elastic.co/t/how-to-access-document-fields-from-script/6332/3 "2017-07-06T03:43:14Z")

</div>


