# How to run a script\_field query?

**URL:** <https://discuss.elastic.co/t/how-to-run-a-script-field-query/13802>\
**Category:** Elasticsearch\
**Created:** [September 30, 2013, 10:11am UTC](https://discuss.elastic.co/t/how-to-run-a-script-field-query/13802 "2013-09-30T10:11:09Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_4](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@Eric\_4](https://discuss.elastic.co/u/Eric_4)\
**Post date:** [September 30, 2013, 10:11am UTC](https://discuss.elastic.co/t/how-to-run-a-script-field-query/13802/1 "2013-09-30T10:11:09Z")

</div>

I have following query in a file called query.json

{  
"script\_fields": {  
"correctYear": {  
"script": "\_source.year – 1800"  
}  
},  
"query": {  
"term": {  
"title": "crime"  
}  
}  
}

I then execute the query in command-line as below.

curl -XGET 'localhost:9200/library/book/\_search?pretty=true' -d @query.json

But I get an error.

SearchPhaseExecutionException: Error: unknown class or illegal statement:  
org.elasticsearch.common.mvel2.ParserContext@7a0bd150

The data type of "year" field is long.

Thank you.

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [September 30, 2013, 11:56am UTC](https://discuss.elastic.co/t/how-to-run-a-script-field-query/13802/2 "2013-09-30T11:56:06Z")

</div>

Hi Eric,

The minus operator is `-` and not `–`. The following script should work:  
{  
"script\_fields": {  
"correctYear": {  
"script": "\_source.year - 1800"  
}  
}  
}

Martijn

On 30 September 2013 12:11, Eric [gayanath@gmail.com](mailto:gayanath@gmail.com) wrote:

> I have following query in a file called query.json
> 
> {  
> "script\_fields": {  
> "correctYear": {  
> "script": "\_source.year – 1800"  
> }  
> },  
> "query": {  
> "term": {  
> "title": "crime"  
> }  
> }  
> }
> 
> I then execute the query in command-line as below.
> 
> curl -XGET 'localhost:9200/library/book/\_search?pretty=true' -d @query.json
> 
> But I get an error.
> 
> SearchPhaseExecutionException: Error: unknown class or illegal statement:  
> org.elasticsearch.common.mvel2.ParserContext@7a0bd150
> 
> The data type of "year" field is long.
> 
> Thank you.
> 
> --  
> 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).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
Met vriendelijke groet,

Martijn van Groningen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Eric\_4](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@Eric\_4](https://discuss.elastic.co/u/Eric_4)\
**Post date:** [October 1, 2013, 4:09am UTC](https://discuss.elastic.co/t/how-to-run-a-script-field-query/13802/3 "2013-10-01T04:09:37Z")

</div>

Hi Martijn,

Thanks a lot! This resolved the issue.

I copied the example from a PDF file. That should be the cause for this  
character.

On Monday, September 30, 2013 7:56:06 PM UTC+8, Martijn v Groningen wrote:

> Hi Eric,
> 
> The minus operator is `-` and not `–`. The following script should work:  
> {  
> "script\_fields": {  
> "correctYear": {  
> "script": "\_source.year - 1800"  
> }  
> }  
> }
> 
> Martijn
> 
> On 30 September 2013 12:11, Eric \<[gaya...@gmail.com](mailto:gaya...@gmail.com) \<javascript:\>\> wrote:
> 
> > I have following query in a file called query.json
> > 
> > {  
> > "script\_fields": {  
> > "correctYear": {  
> > "script": "\_source.year – 1800"  
> > }  
> > },  
> > "query": {  
> > "term": {  
> > "title": "crime"  
> > }  
> > }  
> > }
> > 
> > I then execute the query in command-line as below.
> > 
> > curl -XGET 'localhost:9200/library/book/\_search?pretty=true' -d  
> > @query.json
> > 
> > But I get an error.
> > 
> > SearchPhaseExecutionException: Error: unknown class or illegal statement:  
> > org.elasticsearch.common.mvel2.ParserContext@7a0bd150
> > 
> > The data type of "year" field is long.
> > 
> > Thank you.
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:14am UTC](https://discuss.elastic.co/t/how-to-run-a-script-field-query/13802/4 "2017-07-06T02:14:10Z")

</div>

Closed on July 6, 2017, 2:14am UTC


