# Sorting and scripting

**URL:** <https://discuss.elastic.co/t/sorting-and-scripting/10457>\
**Category:** Elasticsearch\
**Created:** [January 22, 2013, 6:13pm UTC](https://discuss.elastic.co/t/sorting-and-scripting/10457 "2013-01-22T18:13:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea\_crotti](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@andrea\_crotti](https://discuss.elastic.co/u/andrea_crotti)\
**Post date:** [January 22, 2013, 6:13pm UTC](https://discuss.elastic.co/t/sorting-and-scripting/10457/1 "2013-01-22T18:13:38Z")

</div>

I have quite a few doubts about sorting and scripting.

I installed the Python plugin for ES, and as far as I understood I can  
run arbitraty Python code there, right?

I have some documents which contain list of ideas, and for example I  
want to sort on the length of this array.

So I write the sort dictionary in this way:

script = {  
'script': "len(doc['ideas'])",  
'lang': 'python',  
'order': 'asc',  
}  
sort = {'sort': {'\_script': script}}

One surprising thing is that even if I put something that makes no  
sense in "script" (ln(doc['ideas']) for example) elasticsearch doesn't  
complain at all...

Is that normal?

I also couldn't find much documentation/examples about how to use the  
scripting, do you know some other resources?

Thanks,  
Andrea

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [February 1, 2013, 3:08pm UTC](https://discuss.elastic.co/t/sorting-and-scripting/10457/2 "2013-02-01T15:08:18Z")

</div>

I suspect that sort order is not applied correctly. Otherwise, it would  
have complained about absence of "type" parameter and invalid argument for  
len. Try this:

curl "localhost:9200/your-index/your-type/\_search" -d '{  
"sort": [  
{  
"\_script": {  
"script" : "len(doc["ideas"].values)",  
"type" : "number",  
"lang": "python"  
}  
}  
]  
}'

to see if it works with your data from command line.

On Tuesday, January 22, 2013 1:13:38 PM UTC-5, andrea crotti wrote:

> I have quite a few doubts about sorting and scripting.
> 
> I installed the Python plugin for ES, and as far as I understood I can  
> run arbitraty Python code there, right?
> 
> I have some documents which contain list of ideas, and for example I  
> want to sort on the length of this array.
> 
> So I write the sort dictionary in this way:
> 
> script = {  
> 'script': "len(doc['ideas'])",  
> 'lang': 'python',  
> 'order': 'asc',  
> }  
> sort = {'sort': {'\_script': script}}
> 
> One surprising thing is that even if I put something that makes no  
> sense in "script" (ln(doc['ideas']) for example) elasticsearch doesn't  
> complain at all...
> 
> Is that normal?
> 
> I also couldn't find much documentation/examples about how to use the  
> scripting, do you know some other resources?
> 
> Thanks,  
> Andrea

--  
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:53am UTC](https://discuss.elastic.co/t/sorting-and-scripting/10457/3 "2017-07-06T02:53:32Z")

</div>


