# Getting an array inside a document sorted on the basis of some precondition

**URL:** <https://discuss.elastic.co/t/getting-an-array-inside-a-document-sorted-on-the-basis-of-some-precondition/9593>\
**Category:** Elasticsearch\
**Created:** [November 7, 2012, 6:36am UTC](https://discuss.elastic.co/t/getting-an-array-inside-a-document-sorted-on-the-basis-of-some-precondition/9593 "2012-11-07T06:36:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![azitabh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/azitabh/32/2647_2.png) [@azitabh](https://discuss.elastic.co/u/azitabh)\
**Post date:** [November 7, 2012, 6:36am UTC](https://discuss.elastic.co/t/getting-an-array-inside-a-document-sorted-on-the-basis-of-some-precondition/9593/1 "2012-11-07T06:36:58Z")

</div>

Let's say this is the document indexed in elasticsearch:  
{:id=\>123,:a=\>10, :b=\>12,  
:c=\>[{:x=\>A,:y=\>102,:z=\>92},{:x=\>B,:y=\>110,:z=\>96},{:x=\>C,:y=\>94,:z=\>106}]}

And I am firing some query to elasticsearch and it's guaranteed that this  
will be the only document in the result set.  
Is there a way to instruct elasticsearch that the array 'c' in the returned  
document should be sorted on the basis of (y+3z).

Since the expression will be different every time, so I can't take care of  
this while indexing.

Thanks  
-Azitabh

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [November 7, 2012, 11:43am UTC](https://discuss.elastic.co/t/getting-an-array-inside-a-document-sorted-on-the-basis-of-some-precondition/9593/2 "2012-11-07T11:43:28Z")

</div>

Hello Azitabh,

If what you want is to sort the elements of the array within each  
document, then I'm not sure how you can do it other than within your  
application. I don't know of any way for changing the content of  
results.

If you want to change the order of documents you get when you search,  
based on the sum of the mentioned values, you might use a Custom Score  
Query[0] or Script-based Sorting[2]. Something like this:

> <https://gist.github.com/radu-gheorghe/4031013>

Although it would be slow for more results, because the script is  
based on \_source. I couldn't get it working with "doc" ☹

[0] [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)  
[1] [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/api/search/sort.html)

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Wed, Nov 7, 2012 at 8:36 AM, azi [azitabh@gmail.com](mailto:azitabh@gmail.com) wrote:

> Let's say this is the document indexed in elasticsearch:  
> {:id=\>123,:a=\>10, :b=\>12,  
> :c=\>[{:x=\>A,:y=\>102,:z=\>92},{:x=\>B,:y=\>110,:z=\>96},{:x=\>C,:y=\>94,:z=\>106}]}
> 
> And I am firing some query to elasticsearch and it's guaranteed that this  
> will be the only document in the result set.  
> Is there a way to instruct elasticsearch that the array 'c' in the returned  
> document should be sorted on the basis of (y+3z).
> 
> Since the expression will be different every time, so I can't take care of  
> this while indexing.
> 
> Thanks  
> -Azitabh
> 
> --

--

---

<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:** [November 7, 2012, 3:15pm UTC](https://discuss.elastic.co/t/getting-an-array-inside-a-document-sorted-on-the-basis-of-some-precondition/9593/3 "2012-11-07T15:15:07Z")

</div>

If this is the only document and its source is stored, you can use  
script\_field to find and extract the element with the highest score.  
Something like this:

{  
"query": {  
"term" : {  
"id": "123"  
}  
},  
"script\_fields": {  
"top\_c": {  
"script": "c=\_source["c"]; top\_c=null;  
top\_score=java.lang.Integer.MIN\_VALUE; for(cur : c) {score=cur["y"]+  
3\*cur["z"]; if(score \> top\_score){ top\_c=cur; top\_score=score}}; top\_c"  
}  
}  
}

On Wednesday, November 7, 2012 1:36:58 AM UTC-5, Azitabh Ajit wrote:

> Let's say this is the document indexed in elasticsearch:  
> {:id=\>123,:a=\>10, :b=\>12,  
> :c=\>[{:x=\>A,:y=\>102,:z=\>92},{:x=\>B,:y=\>110,:z=\>96},{:x=\>C,:y=\>94,:z=\>106}]}
> 
> And I am firing some query to elasticsearch and it's guaranteed that this  
> will be the only document in the result set.  
> Is there a way to instruct elasticsearch that the array 'c' in the  
> returned document should be sorted on the basis of (y+3z).
> 
> Since the expression will be different every time, so I can't take care of  
> this while indexing.
> 
> Thanks  
> -Azitabh

--

---

<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:05am UTC](https://discuss.elastic.co/t/getting-an-array-inside-a-document-sorted-on-the-basis-of-some-precondition/9593/4 "2017-07-06T03:05:43Z")

</div>


