# Mapping and sorting on array elements

**URL:** <https://discuss.elastic.co/t/mapping-and-sorting-on-array-elements/3375>\
**Category:** Elasticsearch\
**Created:** [September 28, 2010, 9:49am UTC](https://discuss.elastic.co/t/mapping-and-sorting-on-array-elements/3375 "2010-09-28T09:49:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michel\_Valdrighi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/michel_valdrighi/32/3315_2.png) [@Michel\_Valdrighi](https://discuss.elastic.co/u/Michel_Valdrighi)\
**Post date:** [September 28, 2010, 9:49am UTC](https://discuss.elastic.co/t/mapping-and-sorting-on-array-elements/3375/1 "2010-09-28T09:49:56Z")

</div>

Hello,

There is a sorting problem that I'm struggling with.

Let's say there are players that compete on an arbitrary number of tracks,  
and who have a ranking on those tracks.  
I want to query the results by track\_id and get a list of the players that  
competed on it, sorted by ranking.

The data that is mapped this way:

{  
"results": {  
"\_source" : {"enabled": false},  
"properties": {  
"player": {"type": "string", "analyzer": "standard",  
"omit\_term\_freq\_and\_positions": "true", "omit\_norms": "true"},

```
        "tracks": {
            "type": "object",
            "properties": {
                "track_id" : {"type" : "integer"},
                "ranking" : {"type" : "integer"}
            }
        }
    }
}

```

}

What I'm doing now is querying for "tracks.track\_id": 42, and sorting with  
"sort":"tracks.ranking".  
The problem is that if a player competed on more than one track,  
ElasticSearch would pick its best value for "tracks.ranking" and sort using  
that value. Obviously this brings wrong results.

How can I make it sort on the "tracks.ranking" value that corresponds to the  
given "tracks.track\_id" ?  
Is there a better mapping for this need, or can this be solved with a  
custom\_score script?

Thanks in advance for any help on this problem.

--  
Michel Valdrighi

---

<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:** [September 28, 2010, 11:57am UTC](https://discuss.elastic.co/t/mapping-and-sorting-on-array-elements/3375/2 "2010-09-28T11:57:47Z")

</div>

Hi,

There isn't really a way to do it currently, even with custom\_score  
script. I have an idea on how to solve such "parent child" relationship, but  
need some time to iron it out... .

-shay.banon

On Tue, Sep 28, 2010 at 11:49 AM, Michel Valdrighi [michelv@gmail.com](mailto:michelv@gmail.com)wrote:

> Hello,
> 
> There is a sorting problem that I'm struggling with.
> 
> Let's say there are players that compete on an arbitrary number of tracks,  
> and who have a ranking on those tracks.  
> I want to query the results by track\_id and get a list of the players that  
> competed on it, sorted by ranking.
> 
> The data that is mapped this way:
> 
> {  
> "results": {  
> "\_source" : {"enabled": false},  
> "properties": {  
> "player": {"type": "string", "analyzer": "standard",  
> "omit\_term\_freq\_and\_positions": "true", "omit\_norms": "true"},
> 
> ```
> "tracks": {
> "type": "object",
> "properties": {
> "track_id" : {"type" : "integer"},
> "ranking" : {"type" : "integer"}
> }
> }
> }
> }
> 
> ```
> 
> }
> 
> What I'm doing now is querying for "tracks.track\_id": 42, and sorting with  
> "sort":"tracks.ranking".  
> The problem is that if a player competed on more than one track,  
> Elasticsearch would pick its best value for "tracks.ranking" and sort using  
> that value. Obviously this brings wrong results.
> 
> How can I make it sort on the "tracks.ranking" value that corresponds to  
> the given "tracks.track\_id" ?  
> Is there a better mapping for this need, or can this be solved with a  
> custom\_score script?
> 
> Thanks in advance for any help on this problem.
> 
> --  
> Michel Valdrighi

---

<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, 4:18am UTC](https://discuss.elastic.co/t/mapping-and-sorting-on-array-elements/3375/3 "2017-07-06T04:18:50Z")

</div>


