# Sort nested documents in search result

**URL:** <https://discuss.elastic.co/t/sort-nested-documents-in-search-result/17984>\
**Category:** Elasticsearch\
**Created:** [June 8, 2014, 10:12pm UTC](https://discuss.elastic.co/t/sort-nested-documents-in-search-result/17984 "2014-06-08T22:12:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zdenek\_Pizl](https://avatars.discourse-cdn.com/v4/letter/z/b19c9b/32.png) [@Zdenek\_Pizl](https://discuss.elastic.co/u/Zdenek_Pizl)\
**Post date:** [June 8, 2014, 10:12pm UTC](https://discuss.elastic.co/t/sort-nested-documents-in-search-result/17984/1 "2014-06-08T22:12:11Z")

</div>

Hallo,

let's say I have following structure of document with nested (multiple)  
documents :

{  
"host": "server-001",  
"plugins": [  
{  
"plugin\_name": "function-c",  
"plugin": {"function-c": "some C content" }  
},  
{  
"plugin\_name": "function-a",  
"plugin": {"function-a": "some A content" }  
},  
{  
"plugin\_name": "function-b",  
"plugin": {"function-b": "some B content" }  
}  
]  
}

I would like to get result of search with sorted nested plugins' objects,  
that is JSON with plugin\_name item in 'function-a, function-b, function-c'  
sequence of nested documents. Is it even possible to achieve? If it is, how  
should the query and/or mapping look like?

I've tried i.e. '{ "query": { "match": { "host": "server-001" }}, "sort": {  
"plugins.plugin\_name" : "asc" }}}' but it does not sort the nested block at  
all.

Thank you, regards Z. Pizl.

--  
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/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 9, 2014, 8:02am UTC](https://discuss.elastic.co/t/sort-nested-documents-in-search-result/17984/2 "2014-06-09T08:02:30Z")

</div>

Have a look at nested sort in docs: [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#\_nested\_sorting\_example](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-sort.html#_nested_sorting_example)

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 9 juin 2014 à 00:12, Zdenek Pizl [zdenek.pizl@gmail.com](mailto:zdenek.pizl@gmail.com) a écrit :

Hallo,

let's say I have following structure of document with nested (multiple) documents :

{  
"host": "server-001",  
"plugins": [  
{  
"plugin\_name": "function-c",  
"plugin": {"function-c": "some C content" }  
},  
{  
"plugin\_name": "function-a",  
"plugin": {"function-a": "some A content" }  
},  
{  
"plugin\_name": "function-b",  
"plugin": {"function-b": "some B content" }  
}  
]  
}

I would like to get result of search with sorted nested plugins' objects, that is JSON with plugin\_name item in 'function-a, function-b, function-c' sequence of nested documents. Is it even possible to achieve? If it is, how should the query and/or mapping look like?

I've tried i.e. '{ "query": { "match": { "host": "server-001" }}, "sort": { "plugins.plugin\_name" : "asc" }}}' but it does not sort the nested block at all.

Thank you, regards Z. Pizl.

--  
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/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/F200833A-E80F-4EBA-B4D9-C83D75B42790%40pilato.fr](https://groups.google.com/d/msgid/elasticsearch/F200833A-E80F-4EBA-B4D9-C83D75B42790%40pilato.fr).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Zdenek\_Pizl](https://avatars.discourse-cdn.com/v4/letter/z/b19c9b/32.png) [@Zdenek\_Pizl](https://discuss.elastic.co/u/Zdenek_Pizl)\
**Post date:** [June 9, 2014, 12:22pm UTC](https://discuss.elastic.co/t/sort-nested-documents-in-search-result/17984/3 "2014-06-09T12:22:17Z")

</div>

Hi David,

the example is known to me and probably it is not what I am looking for or  
I am doing something dumb. I'll try to explain it in deeper details.

Let's say we have 2 documents within index /sort-test/ with following  
mapping:

_Creating an index and its mapping_:

curl -XPUT [http://localhost:9200/sort-test](http://localhost:9200/sort-test)  
curl -XPUT '[http://localhost:9200/sort-test/node/\_mapping](http://localhost:9200/sort-test/node/_mapping)' -d ' { "node": {  
"properties" : { "plugins" : { "type" : "nested"}, "host": { "index":  
"not\_analyzed", "type": "string" }}}}'

Documents:

curl -XPOST [http://localhost:9200/sort-test/node/](http://localhost:9200/sort-test/node/) -d '{

"host": "server-002",  
"plugins": [  
{ "plugin\_name": "function-c", "plugin": {"function-c": "some C  
content" }},  
{ "plugin\_name": "function-a", "plugin": {"function-a": "some A  
content" }},  
{ "plugin\_name": "function-b", "plugin": {"function-b": "some B  
content" }}  
]  
}'

curl -XPOST [http://localhost:9200/sort-test/node/](http://localhost:9200/sort-test/node/) -d '{

"host": "server-001",  
"plugins": [  
{ "plugin\_name": "function-x", "plugin": {"function-x": "some X  
content" }},  
{ "plugin\_name": "function-d", "plugin": {"function-d": "some D  
content" }}  
]  
}'

And now I would like to get a document of host=server-001 in following form:

'{

"host": "server-001",  
"plugins": [

- { "plugin\_name": "function-d", "plugin": {"function-d": "some D  
content" }}\*
- { "plugin\_name": "function-x", "plugin": {"function-x": "some X  
content" }}, \*  
]  
}'

^^^ Note there are plugins's nested items sorted in ascending order  
according to plugin\_name. THAT is the result I need to achieve. I've tried  
following:

curl -XPOST [http://localhost:9200/sort-test/\_search?pretty=1](http://localhost:9200/sort-test/_search?pretty=1) -d'{ "query":  
{ "match": { "host": "server-001" }}, "sort": { "plugins.plugin\_name" :  
"asc" }}}'  
or  
curl -XPOST [http://localhost:9200/sort-test/\_search?pretty=1](http://localhost:9200/sort-test/_search?pretty=1) -d'{ "query":  
{ "match": { "host": "server-001" }}, "sort": { "plugins.plugin\_name" : {  
"order": "asc", "nested\_path": "plugins" } }}}'

with no luck.

The result is still the same, without sorted plugins items:

{  
"took" : 3,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : null,  
"hits" : [ {  
"\_index" : "sort-test",  
"\_type" : "node",  
"\_id" : "Zg41zMjwRYy77OU9vJ7YHQ",  
"\_score" : null, "\_source" : {  
"host": "server-001",  
"plugins": [  
{ "plugin\_name": "function-x", "plugin": {"function-x": "some X  
content" }},  
{ "plugin\_name": "function-d", "plugin": {"function-d": "some D  
content" }}  
]  
},  
"sort" : ["d"]  
} ]  
}  
}

Any idea how to achieve this? Thank you in advance, regard .zp.

--

> 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/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/09751eee-fd17-416a-98ba-f025ee381482%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/9d1e204e-6d2e-4d85-8c7b-bb7c7d608aed%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/9d1e204e-6d2e-4d85-8c7b-bb7c7d608aed%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, 1:23am UTC](https://discuss.elastic.co/t/sort-nested-documents-in-search-result/17984/4 "2017-07-06T01:23:48Z")

</div>


