# Ordering by an array field when we know which item of the array will be used for ordering

**URL:** <https://discuss.elastic.co/t/ordering-by-an-array-field-when-we-know-which-item-of-the-array-will-be-used-for-ordering/7079>\
**Category:** Elasticsearch\
**Created:** [March 21, 2012, 8:49am UTC](https://discuss.elastic.co/t/ordering-by-an-array-field-when-we-know-which-item-of-the-array-will-be-used-for-ordering/7079 "2012-03-21T08:49:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Palko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_palko/32/2948_2.png) [@Jan\_Palko](https://discuss.elastic.co/u/Jan_Palko)\
**Post date:** [March 21, 2012, 8:49am UTC](https://discuss.elastic.co/t/ordering-by-an-array-field-when-we-know-which-item-of-the-array-will-be-used-for-ordering/7079/1 "2012-03-21T08:49:10Z")

</div>

Hi,

Here is a sample document:

{  
"descriptions": [  
{  
"locale": "en-gb",  
"description": "Example (English)"  
},  
{  
"locale": "it",  
"description": "Example (Italiano)"  
},  
{  
"locale": "fr",  
"description": "Example (Francais)"  
}  
]  
}

We have a locale filter, so ES returns the documents which have description  
in locale acceptable by user and ordered by his preference e.g. en-gb,en,it  
(similar to HTTP Accept-Language header without quality parameter). This  
works fine and also searching by description dependent on locale.

On frontend we always use just one description per document, the one with  
locale which is the most accurate to user's preference. This is done on  
Java side. But what we need is to allow ordering by the descriptions field,  
which is array in ES. I know that it isn't possible to order by array  
fields as it doesn't make sense. Is there any possibility to filter out  
unnecessary descriptions and order by description when we know which  
description will be used based on user's language preference?

The last possible solution we would like to avoid is to read all matched  
documents and order it on Java side, but we use pagination, and after that  
we would read all documents, order them, and return only those which are on  
the requested page.

Thanks,  
Jan

---

<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:** [March 21, 2012, 3:44pm UTC](https://discuss.elastic.co/t/ordering-by-an-array-field-when-we-know-which-item-of-the-array-will-be-used-for-ordering/7079/2 "2012-03-21T15:44:12Z")

</div>

Wouldn't it simplify things if you changed your sample document into  
something like this:

{  
"descriptions": {  
"en-gb": "Example 1 (English)",  
"it": "Example 1 (Italiano)",  
"fr": "Example 1 (Francais)"  
}  
}

You can still limit by locale by adding filter [descriptions.fr](http://descriptions.fr):\* for  
example or you can store a list of available locales in a separate field.

On Wednesday, March 21, 2012 4:49:10 AM UTC-4, Ján Paľko wrote:

> Hi,
> 
> Here is a sample document:
> 
> {  
> "descriptions": [  
> {  
> "locale": "en-gb",  
> "description": "Example (English)"  
> },  
> {  
> "locale": "it",  
> "description": "Example (Italiano)"  
> },  
> {  
> "locale": "fr",  
> "description": "Example (Francais)"  
> }  
> ]  
> }
> 
> We have a locale filter, so ES returns the documents which have  
> description in locale acceptable by user and ordered by his preference e.g.  
> en-gb,en,it (similar to HTTP Accept-Language header without quality  
> parameter). This works fine and also searching by description dependent on  
> locale.
> 
> On frontend we always use just one description per document, the one with  
> locale which is the most accurate to user's preference. This is done on  
> Java side. But what we need is to allow ordering by the descriptions field,  
> which is array in ES. I know that it isn't possible to order by array  
> fields as it doesn't make sense. Is there any possibility to filter out  
> unnecessary descriptions and order by description when we know which  
> description will be used based on user's language preference?
> 
> The last possible solution we would like to avoid is to read all matched  
> documents and order it on Java side, but we use pagination, and after that  
> we would read all documents, order them, and return only those which are on  
> the requested page.
> 
> Thanks,  
> Jan

---

<div class="post-metadata">

**Author:** ![Jan\_Palko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_palko/32/2948_2.png) [@Jan\_Palko](https://discuss.elastic.co/u/Jan_Palko)\
**Post date:** [March 22, 2012, 12:32pm UTC](https://discuss.elastic.co/t/ordering-by-an-array-field-when-we-know-which-item-of-the-array-will-be-used-for-ordering/7079/3 "2012-03-22T12:32:44Z")

</div>

Thank you for that, but the problem is more complicated. I've resolved it  
by using ScriptSortBuilder where I can take the most relevant description  
according to user's language preference and then use the description for  
ordering.

On Wednesday, March 21, 2012 4:44:12 PM UTC+1, Igor Motov wrote:

> Wouldn't it simplify things if you changed your sample document into  
> something like this:
> 
> {  
> "descriptions": {  
> "en-gb": "Example 1 (English)",  
> "it": "Example 1 (Italiano)",  
> "fr": "Example 1 (Francais)"  
> }  
> }
> 
> You can still limit by locale by adding filter [descriptions.fr](http://descriptions.fr):\* for  
> example or you can store a list of available locales in a separate field.
> 
> On Wednesday, March 21, 2012 4:49:10 AM UTC-4, Ján Paľko wrote:
> 
> > Hi,
> > 
> > Here is a sample document:
> > 
> > {  
> > "descriptions": [  
> > {  
> > "locale": "en-gb",  
> > "description": "Example (English)"  
> > },  
> > {  
> > "locale": "it",  
> > "description": "Example (Italiano)"  
> > },  
> > {  
> > "locale": "fr",  
> > "description": "Example (Francais)"  
> > }  
> > ]  
> > }
> > 
> > We have a locale filter, so ES returns the documents which have  
> > description in locale acceptable by user and ordered by his preference e.g.  
> > en-gb,en,it (similar to HTTP Accept-Language header without quality  
> > parameter). This works fine and also searching by description dependent on  
> > locale.
> > 
> > On frontend we always use just one description per document, the one with  
> > locale which is the most accurate to user's preference. This is done on  
> > Java side. But what we need is to allow ordering by the descriptions field,  
> > which is array in ES. I know that it isn't possible to order by array  
> > fields as it doesn't make sense. Is there any possibility to filter out  
> > unnecessary descriptions and order by description when we know which  
> > description will be used based on user's language preference?
> > 
> > The last possible solution we would like to avoid is to read all matched  
> > documents and order it on Java side, but we use pagination, and after that  
> > we would read all documents, order them, and return only those which are on  
> > the requested page.
> > 
> > Thanks,  
> > Jan

---

<div class="post-metadata">

**Author:** ![Jan\_Palko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jan_palko/32/2948_2.png) [@Jan\_Palko](https://discuss.elastic.co/u/Jan_Palko)\
**Post date:** [March 22, 2012, 2:09pm UTC](https://discuss.elastic.co/t/ordering-by-an-array-field-when-we-know-which-item-of-the-array-will-be-used-for-ordering/7079/4 "2012-03-22T14:09:54Z")

</div>

For those who are interested in a solution, here it's the sort part from  
Java API:

"sort" : [ {  
"\_script" : {  
"script" : "foreach (locale : locales) { foreach (desc :  
\_source.descriptions) { if (desc.locale.startsWith(locale)) { return  
desc.description; } }}",  
"type" : "string",  
"params" : {  
"locales" : ["en-us", "en", "sk-sk", "it"]  
}  
}  
} ]

On Thursday, March 22, 2012 1:32:44 PM UTC+1, Ján Paľko wrote:

> Thank you for that, but the problem is more complicated. I've resolved it  
> by using ScriptSortBuilder where I can take the most relevant description  
> according to user's language preference and then use the description for  
> ordering.
> 
> On Wednesday, March 21, 2012 4:44:12 PM UTC+1, Igor Motov wrote:
> 
> > Wouldn't it simplify things if you changed your sample document into  
> > something like this:
> > 
> > {  
> > "descriptions": {  
> > "en-gb": "Example 1 (English)",  
> > "it": "Example 1 (Italiano)",  
> > "fr": "Example 1 (Francais)"  
> > }  
> > }
> > 
> > You can still limit by locale by adding filter [descriptions.fr](http://descriptions.fr):\* for  
> > example or you can store a list of available locales in a separate field.
> > 
> > On Wednesday, March 21, 2012 4:49:10 AM UTC-4, Ján Paľko wrote:
> > 
> > > Hi,
> > > 
> > > Here is a sample document:
> > > 
> > > {  
> > > "descriptions": [  
> > > {  
> > > "locale": "en-gb",  
> > > "description": "Example (English)"  
> > > },  
> > > {  
> > > "locale": "it",  
> > > "description": "Example (Italiano)"  
> > > },  
> > > {  
> > > "locale": "fr",  
> > > "description": "Example (Francais)"  
> > > }  
> > > ]  
> > > }
> > > 
> > > We have a locale filter, so ES returns the documents which have  
> > > description in locale acceptable by user and ordered by his preference e.g.  
> > > en-gb,en,it (similar to HTTP Accept-Language header without quality  
> > > parameter). This works fine and also searching by description dependent on  
> > > locale.
> > > 
> > > On frontend we always use just one description per document, the one  
> > > with locale which is the most accurate to user's preference. This is done  
> > > on Java side. But what we need is to allow ordering by the descriptions  
> > > field, which is array in ES. I know that it isn't possible to order by  
> > > array fields as it doesn't make sense. Is there any possibility to filter  
> > > out unnecessary descriptions and order by description when we know which  
> > > description will be used based on user's language preference?
> > > 
> > > The last possible solution we would like to avoid is to read all matched  
> > > documents and order it on Java side, but we use pagination, and after that  
> > > we would read all documents, order them, and return only those which are on  
> > > the requested page.
> > > 
> > > Thanks,  
> > > Jan

---

<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:35am UTC](https://discuss.elastic.co/t/ordering-by-an-array-field-when-we-know-which-item-of-the-array-will-be-used-for-ordering/7079/5 "2017-07-06T03:35:09Z")

</div>

Closed on July 6, 2017, 3:35am UTC


