# Sorting by nested fields

**URL:** <https://discuss.elastic.co/t/sorting-by-nested-fields/20118>\
**Category:** Elasticsearch\
**Created:** [October 7, 2014, 3:35pm UTC](https://discuss.elastic.co/t/sorting-by-nested-fields/20118 "2014-10-07T15:35:37Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Elke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@Elke](https://discuss.elastic.co/u/Elke)\
**Post date:** [October 7, 2014, 3:35pm UTC](https://discuss.elastic.co/t/sorting-by-nested-fields/20118/1 "2014-10-07T15:35:37Z")

</div>

Hi.

I have a question about sorting by nested fields.  
This is just an small example of the hierarchy level (The marketplaces and  
the prices are mapped as nested documents):

{ id: "doc1",  
name : "test",  
marketplaces : [ { "name" : "marketPlace1",  
"prices" : [ { "amount": "20.00" },  
{ "amount": "40.00" }]  
},  
{ "name" : "marketPlace2",  
"prices" : [{ "amount": "21.00" },  
{ "amount": "30.00" }]  
}]}

{ id: "doc2",  
name : "test2",  
marketplaces : [ { "name" : "marketPlace1",  
"prices" : [{ "amount": "30.00" },  
{ "amount": "35.00" }]  
},  
{ "name" : "marketPlace2",  
"prices" : [{ "amount": "1.00" },  
{ "amount": "3.00" }]  
}]}

Now I want to search for all documents with the marketplace name :  
"marketPlace1" and sort the result by the prices.amount of this  
marketplace.  
So I expect that in the example the first document will be in first place  
and the second document will be in second place.

The filter works fine. I won't see the documents without "marketPlace1" in  
the result list.  
But the sort doesn't restrict the same as the filter. So I tried to set a  
nested filter to match the upper hierarchy too.

I tried this sort, but got the value "Infinity" as sort result. :  
"sort" : {  
"marketplaces.prices.amount" : { "order" : "asc" , "nested\_filter" : {  
"term" : {  
"marketplaces.name" : "marketPlace1"  
}}}}

In case I don't use the nested\_filter I get the second document as first  
result, because the sorting doesn't consider the marketPlace name.

Is there a solution for my problem?  
Thank you very much for your help.

Best regards,  
Elke

--  
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/589eae79-0041-4947-879a-f9b7829c3145%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/589eae79-0041-4947-879a-f9b7829c3145%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jonathan\_Foy](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jonathan\_Foy](https://discuss.elastic.co/u/Jonathan_Foy)\
**Post date:** [October 10, 2014, 1:23pm UTC](https://discuss.elastic.co/t/sorting-by-nested-fields/20118/2 "2014-10-10T13:23:50Z")

</div>

I suspect that because you have multiple levels of nesting you need to use  
the nested path  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#\_sorting\_within\_nested\_objects](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sorting_within_nested_objects)  
(it's probably choosing marketplaces.prices, and you would want just  
marketplaces, though I'm not positive).

On Tuesday, October 7, 2014 11:35:37 AM UTC-4, Elke wrote:

> Hi.
> 
> I have a question about sorting by nested fields.  
> This is just an small example of the hierarchy level (The marketplaces and  
> the prices are mapped as nested documents):
> 
> { id: "doc1",  
> name : "test",  
> marketplaces : [ { "name" : "marketPlace1",  
> "prices" : [ { "amount": "20.00" },  
> { "amount": "40.00" }]  
> },  
> { "name" : "marketPlace2",  
> "prices" : [{ "amount": "21.00" },  
> { "amount": "30.00" }]  
> }]}
> 
> { id: "doc2",  
> name : "test2",  
> marketplaces : [ { "name" : "marketPlace1",  
> "prices" : [{ "amount": "30.00" },  
> { "amount": "35.00" }]  
> },  
> { "name" : "marketPlace2",  
> "prices" : [{ "amount": "1.00" },  
> { "amount": "3.00" }]  
> }]}
> 
> Now I want to search for all documents with the marketplace name :  
> "marketPlace1" and sort the result by the prices.amount of this  
> marketplace.  
> So I expect that in the example the first document will be in first place  
> and the second document will be in second place.
> 
> The filter works fine. I won't see the documents without "marketPlace1" in  
> the result list.  
> But the sort doesn't restrict the same as the filter. So I tried to set a  
> nested filter to match the upper hierarchy too.
> 
> I tried this sort, but got the value "Infinity" as sort result. :  
> "sort" : {  
> "marketplaces.prices.amount" : { "order" : "asc" , "nested\_filter" : {  
> "term" : {  
> "marketplaces.name" :  
> "marketPlace1"  
> }}}}
> 
> In case I don't use the nested\_filter I get the second document as first  
> result, because the sorting doesn't consider the marketPlace name.
> 
> Is there a solution for my problem?  
> Thank you very much for your help.
> 
> Best regards,  
> Elke

--  
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/d6a37b10-169d-40a7-9974-dc31be40bf0a%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d6a37b10-169d-40a7-9974-dc31be40bf0a%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Elke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@Elke](https://discuss.elastic.co/u/Elke)\
**Post date:** [October 13, 2014, 7:48am UTC](https://discuss.elastic.co/t/sorting-by-nested-fields/20118/3 "2014-10-13T07:48:36Z")

</div>

Hi.

I tried to set the nested\_path to "marketplaces" and "marketplaces.prices".  
But I still get "Infinity" as sort result.

Best regards,  
Elke

Am Freitag, 10. Oktober 2014 15:23:50 UTC+2 schrieb Jonathan Foy:

> I suspect that because you have multiple levels of nesting you need to use  
> the nested path  
> [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#\_sorting\_within\_nested\_objects](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_sorting_within_nested_objects)  
> (it's probably choosing marketplaces.prices, and you would want just  
> marketplaces, though I'm not positive).
> 
> On Tuesday, October 7, 2014 11:35:37 AM UTC-4, Elke wrote:
> 
> > Hi.
> > 
> > I have a question about sorting by nested fields.  
> > This is just an small example of the hierarchy level (The marketplaces  
> > and the prices are mapped as nested documents):
> > 
> > { id: "doc1",  
> > name : "test",  
> > marketplaces : [ { "name" : "marketPlace1",  
> > "prices" : [ { "amount": "20.00" },  
> > { "amount": "40.00" }]  
> > },  
> > { "name" : "marketPlace2",  
> > "prices" : [{ "amount": "21.00" },  
> > { "amount": "30.00" }]  
> > }]}
> > 
> > { id: "doc2",  
> > name : "test2",  
> > marketplaces : [ { "name" : "marketPlace1",  
> > "prices" : [{ "amount": "30.00" },  
> > { "amount": "35.00" }]  
> > },  
> > { "name" : "marketPlace2",  
> > "prices" : [{ "amount": "1.00" },  
> > { "amount": "3.00" }]  
> > }]}
> > 
> > Now I want to search for all documents with the marketplace name :  
> > "marketPlace1" and sort the result by the prices.amount of this  
> > marketplace.  
> > So I expect that in the example the first document will be in first place  
> > and the second document will be in second place.
> > 
> > The filter works fine. I won't see the documents without "marketPlace1"  
> > in the result list.  
> > But the sort doesn't restrict the same as the filter. So I tried to set a  
> > nested filter to match the upper hierarchy too.
> > 
> > I tried this sort, but got the value "Infinity" as sort result. :  
> > "sort" : {  
> > "marketplaces.prices.amount" : { "order" : "asc" , "nested\_filter" :  
> > {  
> > "term" : {  
> > "marketplaces.name" :  
> > "marketPlace1"  
> > }}}}
> > 
> > In case I don't use the nested\_filter I get the second document as first  
> > result, because the sorting doesn't consider the marketPlace name.
> > 
> > Is there a solution for my problem?  
> > Thank you very much for your help.
> > 
> > Best regards,  
> > Elke

--  
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/57395623-eae8-4c02-b0f6-24f3e913f7ed%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/57395623-eae8-4c02-b0f6-24f3e913f7ed%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Elke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@Elke](https://discuss.elastic.co/u/Elke)\
**Post date:** [October 17, 2014, 3:47pm UTC](https://discuss.elastic.co/t/sorting-by-nested-fields/20118/4 "2014-10-17T15:47:35Z")

</div>

Has somebody another idea? Or it is not possible at all?

--  
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/d1748088-0ceb-409d-9e42-deffa314f0e9%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/d1748088-0ceb-409d-9e42-deffa314f0e9%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Elke](https://avatars.discourse-cdn.com/v4/letter/e/cdc98d/32.png) [@Elke](https://discuss.elastic.co/u/Elke)\
**Post date:** [December 10, 2014, 10:54am UTC](https://discuss.elastic.co/t/sorting-by-nested-fields/20118/5 "2014-12-10T10:54:58Z")

</div>

Is this possible using script sort?

--  
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/5cdb92cb-676d-45dc-b242-ff9865a3129e%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5cdb92cb-676d-45dc-b242-ff9865a3129e%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, 12:44am UTC](https://discuss.elastic.co/t/sorting-by-nested-fields/20118/6 "2017-07-06T00:44:32Z")

</div>


