# Sort on field with array value

**URL:** <https://discuss.elastic.co/t/sort-on-field-with-array-value/6284>\
**Category:** Elasticsearch\
**Created:** [January 4, 2012, 11:26am UTC](https://discuss.elastic.co/t/sort-on-field-with-array-value/6284 "2012-01-04T11:26:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Runar\_Myklebust\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/runar_myklebust_2/32/2047_2.png) [@Runar\_Myklebust\_2](https://discuss.elastic.co/u/Runar_Myklebust_2)\
**Post date:** [January 4, 2012, 11:26am UTC](https://discuss.elastic.co/t/sort-on-field-with-array-value/6284/1 "2012-01-04T11:26:21Z")

</div>

Hi all.

In out data model, the uses could define fields with mutliple values, in  
this example data\_myrelated.  
The values are stored in an array, like this:

{  
"took" : 100,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 1,  
"max\_score" : 1.0,  
"hits" : [ {  
"\_index" : "cms",  
"\_type" : "content",  
"\_id" : "101",  
"\_score" : 1.0,  
"fields" : {  
"categorykey" : "9.0",  
"orderby\_contenttype" : "article",  
"categorykey\_numeric" : 9.0,  
"status" : "2",  
"status\_numeric" : 2.0,  
"contenttype" : "article",  
"orderby\_data\_myrelated" : [ "  
\u0001@\u0004\u0000\u0000\u0000\u0000\u0000\u0000\u0000",  
" \u0001@\u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000" ],  
"data\_myrelated\_numeric" : [3.0, 9.0]  
}  
} ]  
}  
}

(The orderby\_data\_myrelated is stored like this to enable correct sorting  
of number values in fields where we also could get text)

* * *

{  
"from" : 0,  
"size" : 10,  
"query" : {  
"match\_all" : {  
}  
},  
"sort" : [ {  
"orderby\_data\_myrelated" : {  
"order" : "asc"  
}  
} ]  
}

This query now yields no results, is this expected behaviour?

## greetings

Runar Myklebust

---

<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:** [January 4, 2012, 3:30pm UTC](https://discuss.elastic.co/t/sort-on-field-with-array-value/6284/2 "2012-01-04T15:30:39Z")

</div>

You can't sort on a multi valued field, you should get a failure in your  
search...

On Wed, Jan 4, 2012 at 1:26 PM, Runar Myklebust [runar@myklebust.me](mailto:runar@myklebust.me) wrote:

> Hi all.
> 
> In out data model, the uses could define fields with mutliple values, in  
> this example data\_myrelated.  
> The values are stored in an array, like this:
> 
> {  
> "took" : 100,  
> "timed\_out" : false,  
> "\_shards" : {  
> "total" : 5,  
> "successful" : 5,  
> "failed" : 0  
> },  
> "hits" : {  
> "total" : 1,  
> "max\_score" : 1.0,  
> "hits" : [ {  
> "\_index" : "cms",  
> "\_type" : "content",  
> "\_id" : "101",  
> "\_score" : 1.0,  
> "fields" : {  
> "categorykey" : "9.0",  
> "orderby\_contenttype" : "article",  
> "categorykey\_numeric" : 9.0,  
> "status" : "2",  
> "status\_numeric" : 2.0,  
> "contenttype" : "article",  
> "orderby\_data\_myrelated" : [ " \u0001@\u0004\u0000\u0000\u0000\u0000\u0000\u0000\u0000",  
> " \u0001@\u0011\u0000\u0000\u0000\u0000\u0000\u0000\u0000" ],  
> "data\_myrelated\_numeric" : [3.0, 9.0]  
> }  
> } ]  
> }  
> }
> 
> (The orderby\_data\_myrelated is stored like this to enable correct sorting  
> of number values in fields where we also could get text)
> 
> * * *
> 
> {  
> "from" : 0,  
> "size" : 10,  
> "query" : {  
> "match\_all" : {  
> }  
> },  
> "sort" : [ {  
> "orderby\_data\_myrelated" : {  
> "order" : "asc"  
> }  
> } ]  
> }
> 
> This query now yields no results, is this expected behaviour?
> 
> ## greetings
> 
> Runar Myklebust

---

<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:43am UTC](https://discuss.elastic.co/t/sort-on-field-with-array-value/6284/3 "2017-07-06T03:43:49Z")

</div>


