# Sort by groovy \_script

**URL:** <https://discuss.elastic.co/t/sort-by-groovy--script/19241>\
**Category:** Elasticsearch\
**Created:** [August 13, 2014, 7:10am UTC](https://discuss.elastic.co/t/sort-by-groovy--script/19241 "2014-08-13T07:10:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fabian\_Kostring](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fabian_kostring/32/1310_2.png) [@Fabian\_Kostring](https://discuss.elastic.co/u/Fabian_Kostring)\
**Post date:** [August 13, 2014, 7:10am UTC](https://discuss.elastic.co/t/sort-by-groovy--script/19241/1 "2014-08-13T07:10:31Z")

</div>

Hey there!

I'd like to sort my results by groovy script but i have problem selecting  
the right field values from a multi valued field.

I try to select only the value at index 2 (or 10343) from a multivalued  
field.

"script": "abs((doc['multi\_field'].values[0] - param1)+(doc['multi\_field'].values[1]

- param2))",

How can i select the value at a specific index. Is there a way to do it  
with a multivalued field or must i index the fields seperatly?

Thanks for helping

--  
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/1361b223-1448-49a9-98c5-eaa7c1b8859a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/1361b223-1448-49a9-98c5-eaa7c1b8859a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Fabian\_Kostring](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/fabian_kostring/32/1310_2.png) [@Fabian\_Kostring](https://discuss.elastic.co/u/Fabian_Kostring)\
**Post date:** [August 13, 2014, 7:25am UTC](https://discuss.elastic.co/t/sort-by-groovy--script/19241/2 "2014-08-13T07:25:47Z")

</div>

Ps:

This part

"script": "abs((doc['multi\_field'].values[0] - param1)+(doc['multi\_field'].values[1]

- param2))",

throws following exception

Query Failed [Failed to execute main query]]; nested:  
GroovyScriptExecutionException[NullPointerException[Cannot invoke method  
minus() on null object]]; }]",

Am Mittwoch, 13. August 2014 09:10:31 UTC+2 schrieb Fabian Köstring:

> Hey there!
> 
> I'd like to sort my results by groovy script but i have problem selecting  
> the right field values from a multi valued field.
> 
> I try to select only the value at index 2 (or 10343) from a multivalued  
> field.
> 
> "script": "abs((doc['multi\_field'].values[0] - param1)+(doc['multi\_field'].values[1]
> 
> - param2))",
> 
> How can i select the value at a specific index. Is there a way to do it  
> with a multivalued field or must i index the fields seperatly?
> 
> Thanks for helping

--  
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/b4c9febc-be70-4503-955f-4645450ffac2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b4c9febc-be70-4503-955f-4645450ffac2%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:09am UTC](https://discuss.elastic.co/t/sort-by-groovy--script/19241/3 "2017-07-06T01:09:03Z")

</div>


