# Sort on a deeply nested field?

**URL:** <https://discuss.elastic.co/t/sort-on-a-deeply-nested-field/5909>\
**Category:** Elasticsearch\
**Created:** [November 18, 2011, 7:40am UTC](https://discuss.elastic.co/t/sort-on-a-deeply-nested-field/5909 "2011-11-18T07:40:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [November 18, 2011, 7:40am UTC](https://discuss.elastic.co/t/sort-on-a-deeply-nested-field/5909/1 "2011-11-18T07:40:50Z")

</div>

Hi folks. Can you sort on a deeply nested field? I ask because this query:

curl -X GET 'localhost:9200/test/product/\_search?pretty=true' -d '{  
"sort" : [{ "items.properties.color.orig": {"order": "asc"} }]  
}'

fails with this:

QueryPhaseExecutionException[[test][2]:  
query[ConstantScore(_:_)],from[0],size[10],sort[\<custom:"items.properties.color.orig":  
org.elasticsearch.index.field.data.strings.StringFieldDataType$1@f590c6\>]:  
Query Failed [Failed to execute main query]]; nested: IOException[Can't  
sort on string types with more than one value per doc, or more than one  
token per field];

However, as far as I can tell, the mapping does in fact contain  
"items.properties.color.orig", which is an unanalyzed string:

> <https://gist.github.com/nickhoffman/31e5846f104fecccd304>
>
> There are more than three files. show original

Any idea what I'm doing wrong?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [November 20, 2011, 8:21am UTC](https://discuss.elastic.co/t/sort-on-a-deeply-nested-field/5909/2 "2011-11-20T08:21:45Z")

</div>

Yes, you can sort on "deeply nested field" (and you don't need to add  
`properties` to get there, i.e. it can be obj1.obj2.field3), but you can't  
sort on a field with multiple values.

On Fri, Nov 18, 2011 at 9:40 AM, Nick Hoffman [nick@deadorange.com](mailto:nick@deadorange.com) wrote:

> Hi folks. Can you sort on a deeply nested field? I ask because this query:
> 
> curl -X GET 'localhost:9200/test/product/\_search?pretty=true' -d '{  
> "sort" : [{ "items.properties.color.orig": {"order": "asc"} }]  
> }'
> 
> fails with this:
> 
> QueryPhaseExecutionException[[test][2]:  
> query[ConstantScore(_:_)],from[0],size[10],sort[\<custom:"items.properties.color.orig":  
> org.elasticsearch.index.field.data.strings.StringFieldDataType$1@f590c6\>]:  
> Query Failed [Failed to execute main query]]; nested: IOException[Can't  
> sort on string types with more than one value per doc, or more than one  
> token per field];
> 
> However, as far as I can tell, the mapping does in fact contain  
> "items.properties.color.orig", which is an unanalyzed string:  
> [Why does sorting fail here? · GitHub](https://gist.github.com/31e5846f104fecccd304)
> 
> Any idea what I'm doing wrong?

---

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [November 20, 2011, 9:58pm UTC](https://discuss.elastic.co/t/sort-on-a-deeply-nested-field/5909/3 "2011-11-20T21:58:50Z")

</div>

The "properties" is a keyword that's used in my app and that we decided to  
use in ES, too.

Any idea why the error in that gist is occurring? I've been wracking brain,  
but can't see anything wrong with the mapping, document, or query.

> <https://gist.github.com/nickhoffman/31e5846f104fecccd304>
>
> There are more than three files. show original

---

<div class="post-metadata">

**Author:** ![Nick\_Hoffman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nick_hoffman/32/1872_2.png) [@Nick\_Hoffman](https://discuss.elastic.co/u/Nick_Hoffman)\
**Post date:** [November 21, 2011, 7:53pm UTC](https://discuss.elastic.co/t/sort-on-a-deeply-nested-field/5909/4 "2011-11-21T19:53:46Z")

</div>

On IRC, kimchy identified what the problem is. The document that I've  
indexed:

{  
"name" : "Snoprinsessa Barbie",  
"items" : [  
{ "name" : "Faux fur-trimmed parka", "properties" : {"color" : "White"}  
},  
{ "name" : "Stud Diamond Ring", "properties" : {"color" : "Clear"} }  
]  
}

has an array of strings, which obviously can't be sorted. I didn't realize  
that the "items" field being an array was a problem.

bitsweat\_ suggested I look at parent-child support:

> <https://github.com/elastic/elasticsearch/issues/553>

---

<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:48am UTC](https://discuss.elastic.co/t/sort-on-a-deeply-nested-field/5909/5 "2017-07-06T03:48:01Z")

</div>


