# Order results by value in one of the array entries

**URL:** <https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124>\
**Category:** Elasticsearch\
**Created:** [January 7, 2014, 10:45am UTC](https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124 "2014-01-07T10:45:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johan\_E](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Johan\_E](https://discuss.elastic.co/u/Johan_E)\
**Post date:** [January 7, 2014, 10:45am UTC](https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124/1 "2014-01-07T10:45:41Z")

</div>

Hi,

I'm trying to order the result of a query by a specified entry in a array.

Here is a sample entry

{  
"product\_name": "product alfa",  
"product\_id": "4a86c92ccd26111d7ba0eada7da6a75af",  
"description": "This is a sample product",  
"image\_id": "product\_a.jpg",  
"inventory": [  
{  
"warehouse": "warehouse\_a",  
"stock": 99  
},  
{  
"warehouse": "warehouse\_b",  
"stock": 19  
},  
{  
"warehouse": "warehouse\_c",  
"stock": 99  
}  
]  
}

If there were more "products" containing alfa, I would (for example) want  
to sort they by the stock of a warehouse.

I'm currently using a query like:

POST \_search  
{  
"query": {  
"match": {  
"product\_name":{  
"query":"alfa",  
"type" : "phrase"  
}  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"availability.warehouse": "warehouse\_a"  
}  
}  
]  
}  
}  
}

I would like the results sorted by stock (for warehouse\_a only) descending.

Any ideas?

--  
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/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [January 8, 2014, 4:35am UTC](https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124/2 "2014-01-08T04:35:50Z")

</div>

Hi Johan,

You try to use script based sorting.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Or the function score query.

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

I hope this helps.

Regards,

* * *

Jun Ohtani  
[johtani@gmail.com](mailto:johtani@gmail.com)  
blog : [http://blog.johtani.info](http://blog.johtani.info)  
twitter : [http://twitter.com/johtani](http://twitter.com/johtani)

2014/01/07 19:45、Johan E [johane73@gmail.com](mailto:johane73@gmail.com) のメール：

> Hi,
> 
> I'm trying to order the result of a query by a specified entry in a array.
> 
> Here is a sample entry
> 
> {  
> "product\_name": "product alfa",  
> "product\_id": "4a86c92ccd26111d7ba0eada7da6a75af",  
> "description": "This is a sample product",  
> "image\_id": "product\_a.jpg",  
> "inventory": [  
> {  
> "warehouse": "warehouse\_a",  
> "stock": 99  
> },  
> {  
> "warehouse": "warehouse\_b",  
> "stock": 19  
> },  
> {  
> "warehouse": "warehouse\_c",  
> "stock": 99  
> }  
> ]  
> }
> 
> If there were more "products" containing alfa, I would (for example) want to sort they by the stock of a warehouse.
> 
> I'm currently using a query like:
> 
> POST \_search  
> {  
> "query": {  
> "match": {  
> "product\_name":{  
> "query":"alfa",  
> "type" : "phrase"  
> }  
> }  
> },  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "term": {  
> "availability.warehouse": "warehouse\_a"  
> }  
> }  
> ]  
> }  
> }  
> }
> 
> I would like the results sorted by stock (for warehouse\_a only) descending.
> 
> Any ideas?
> 
> --  
> 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/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Johan\_E](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Johan\_E](https://discuss.elastic.co/u/Johan_E)\
**Post date:** [January 8, 2014, 11:11am UTC](https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124/3 "2014-01-08T11:11:52Z")

</div>

Hi Jun,

Thanks for your reply.

Im not sure how I can get that to work. In my project I need to only  
boost/order by the stock of "warehouse\_a", how do I use only the value for  
that entry in the array?

Thanks  
Johan

On Wednesday, January 8, 2014 4:35:50 AM UTC, Jun Ohtani wrote:

> Hi Johan,
> 
> You try to use script based sorting.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_script_based_sorting)
> 
> Or the function score query.
> 
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_script_score)
> 
> I hope this helps.
> 
> Regards,
> 
> * * *
> 
> Jun Ohtani  
> [joh...@gmail.com](mailto:joh...@gmail.com) \<javascript:\>  
> blog : [http://blog.johtani.info](http://blog.johtani.info)  
> twitter : [http://twitter.com/johtani](http://twitter.com/johtani)
> 
> 2014/01/07 19:45、Johan E \<[joha...@gmail.com](mailto:joha...@gmail.com) \<javascript:\>\> のメール：
> 
> > Hi,
> > 
> > I'm trying to order the result of a query by a specified entry in a  
> > array.
> > 
> > Here is a sample entry
> > 
> > {  
> > "product\_name": "product alfa",  
> > "product\_id": "4a86c92ccd26111d7ba0eada7da6a75af",  
> > "description": "This is a sample product",  
> > "image\_id": "product\_a.jpg",  
> > "inventory": [  
> > {  
> > "warehouse": "warehouse\_a",  
> > "stock": 99  
> > },  
> > {  
> > "warehouse": "warehouse\_b",  
> > "stock": 19  
> > },  
> > {  
> > "warehouse": "warehouse\_c",  
> > "stock": 99  
> > }  
> > ]  
> > }
> > 
> > If there were more "products" containing alfa, I would (for example)  
> > want to sort they by the stock of a warehouse.
> > 
> > I'm currently using a query like:
> > 
> > POST \_search  
> > {  
> > "query": {  
> > "match": {  
> > "product\_name":{  
> > "query":"alfa",  
> > "type" : "phrase"  
> > }  
> > }  
> > },  
> > "filter": {  
> > "bool": {  
> > "must": [  
> > {  
> > "term": {  
> > "availability.warehouse": "warehouse\_a"  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > I would like the results sorted by stock (for warehouse\_a only)  
> > descending.
> > 
> > Any ideas?
> > 
> > --  
> > 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:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com).
> 
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/0eb410ef-0117-4004-84f0-713b5b02616f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0eb410ef-0117-4004-84f0-713b5b02616f%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Johan\_E](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@Johan\_E](https://discuss.elastic.co/u/Johan_E)\
**Post date:** [January 8, 2014, 12:52pm UTC](https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124/4 "2014-01-08T12:52:58Z")

</div>

I ended up changing the format of the json, with warehouse stock in  
separate entries in an array. This way I can check for it and get the stock  
at the same time.

--  
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/6c2103ee-7786-4ec0-b981-10aabb365fb9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/6c2103ee-7786-4ec0-b981-10aabb365fb9%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![johtani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/johtani/32/44956_2.png) [@johtani](https://discuss.elastic.co/u/johtani)\
**Post date:** [January 9, 2014, 4:56am UTC](https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124/5 "2014-01-09T04:56:52Z")

</div>

Hi Johan,

Sorry for the late reply.

script base sort sample like this.

--sample--  
POST /script\_order\_sample/\_search  
{  
"query": {  
"match": {  
"product\_name":{  
"query":"alfa",  
"type" : "phrase"  
}  
}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"term": {  
"inventory.warehouse": "warehouse\_a"  
}  
}  
]  
}  
},  
"sort": {  
"\_script" : {  
"script" : "foreach( stock\_info : \_source.inventory){ if (stock\_info.warehouse == 'warehouse\_a') {return stock\_info.stock;}}",  
"type" : "number",  
"order" : "desc"  
}  
}  
}

—

I have another plan for this issue.  
I think that “inventory” array is flatten like this.  
Ant your query only specify “sort” to “warehourse\_a”.

{  
"product\_name": "product alfa",  
"product\_id": "4a86c92ccd26111d7ba0eada7da6a75af",  
"description": "This is a sample product",  
"image\_id": "product\_a.jpg",  
"warehouse\_a”: 99,  
“warehouse\_b": 19,  
“warehouse\_c”: 99  
}

I hope it helps your issue.  
Regards,

* * *

Jun Ohtani  
[johtani@gmail.com](mailto:johtani@gmail.com)  
blog : [http://blog.johtani.info](http://blog.johtani.info)  
twitter : [http://twitter.com/johtani](http://twitter.com/johtani)

2014/01/08 20:11、Johan E [johane73@gmail.com](mailto:johane73@gmail.com) のメール：

> Hi Jun,
> 
> Thanks for your reply.
> 
> Im not sure how I can get that to work. In my project I need to only boost/order by the stock of "warehouse\_a", how do I use only the value for that entry in the array?
> 
> Thanks  
> Johan
> 
> On Wednesday, January 8, 2014 4:35:50 AM UTC, Jun Ohtani wrote:  
> Hi Johan,
> 
> You try to use script based sorting.  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_script_based_sorting)
> 
> Or the function score query.  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_script_score)
> 
> I hope this helps.
> 
> Regards,
> 
> * * *
> 
> Jun Ohtani  
> [joh...@gmail.com](mailto:joh...@gmail.com)  
> blog : [http://blog.johtani.info](http://blog.johtani.info)  
> twitter : [http://twitter.com/johtani](http://twitter.com/johtani)
> 
> 2014/01/07 19:45、Johan E [joha...@gmail.com](mailto:joha...@gmail.com) のメール：
> 
> > Hi,
> > 
> > I'm trying to order the result of a query by a specified entry in a array.
> > 
> > Here is a sample entry
> > 
> > {  
> > "product\_name": "product alfa",  
> > "product\_id": "4a86c92ccd26111d7ba0eada7da6a75af",  
> > "description": "This is a sample product",  
> > "image\_id": "product\_a.jpg",  
> > "inventory": [  
> > {  
> > "warehouse": "warehouse\_a",  
> > "stock": 99  
> > },  
> > {  
> > "warehouse": "warehouse\_b",  
> > "stock": 19  
> > },  
> > {  
> > "warehouse": "warehouse\_c",  
> > "stock": 99  
> > }  
> > ]  
> > }
> > 
> > If there were more "products" containing alfa, I would (for example) want to sort they by the stock of a warehouse.
> > 
> > I'm currently using a query like:
> > 
> > POST \_search  
> > {  
> > "query": {  
> > "match": {  
> > "product\_name":{  
> > "query":"alfa",  
> > "type" : "phrase"  
> > }  
> > }  
> > },  
> > "filter": {  
> > "bool": {  
> > "must": [  
> > {  
> > "term": {  
> > "availability.warehouse": "warehouse\_a"  
> > }  
> > }  
> > ]  
> > }  
> > }  
> > }
> > 
> > I would like the results sorted by stock (for warehouse\_a only) descending.
> > 
> > Any ideas?
> > 
> > --  
> > 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).  
> > To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/01a7baad-40e3-40b3-8104-66910762b004%40googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).
> 
> --  
> 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 [toelasticsearch+unsubscribe@googlegroups.com](mailto:toelasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/0eb410ef-0117-4004-84f0-713b5b02616f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/0eb410ef-0117-4004-84f0-713b5b02616f%40googlegroups.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, 1:57am UTC](https://discuss.elastic.co/t/order-results-by-value-in-one-of-the-array-entries/15124/6 "2017-07-06T01:57:51Z")

</div>


