# Sorting a nested collection AFTER sort (for display / result purposes only)

**URL:** <https://discuss.elastic.co/t/sorting-a-nested-collection-after-sort-for-display-result-purposes-only/213219>\
**Category:** Elasticsearch\
**Created:** [December 27, 2019, 6:16pm UTC](https://discuss.elastic.co/t/sorting-a-nested-collection-after-sort-for-display-result-purposes-only/213219 "2019-12-27T18:16:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexjb](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alexjb/32/48999_2.png) [@alexjb](https://discuss.elastic.co/u/alexjb)\
**Post date:** [December 27, 2019, 6:16pm UTC](https://discuss.elastic.co/t/sorting-a-nested-collection-after-sort-for-display-result-purposes-only/213219/1 "2019-12-27T18:16:13Z")

</div>

I'm looking to perform some 'post query' processing on my result set.

I have documents like this:

```
{
    "name": "John Smith",
    "levels": [
        {
            "name": "Gold",
            "order": 4.0
        },
        {
            "name": "Bronze",
            "order": 2.0
        },
        {
            "name": "Silver",
            "order": 3.0
        }
    ]
}

```

A person can have one or more 'levels' and those levels have an 'order' stored with them.

The idea is, we want to order _ALL_ results by `levels.order` DESC (so those Platinum and Gold people come before Silver / Bronze) **However, in the results, I want to order the source value ASC (so Bronze is first)**

For example, say there's two documents in the index:

```
[
    {
        "name": "Craig Jones",
        "levels": [
            {
                "name": "Silver",
                "order": 3.0
            },
            {
                "name": "Bronze",
                "order": 2.0
            }
        ]
    },
    {
        "name": "John Smith",
        "levels": [
            {
                "name": "Silver",
                "order": 3.0
            },
            {
                "name": "Gold",
                "order": 4.0
            },
            {
                "name": "Bronze",
                "order": 2.0
            }
        ]
    }
]

```

When I hit the `_search` api with a sort (but no query)

```
{
    "sort": [
        {
            "levels.order": {
                "nested": {
                    "path": "levels"
                },
                "order": "desc"
            }
        }
    ]
}

```

... get my results back (sort of) as expected:

```
[
    {
        "name": "John Smith",
        "levels": [
            {
                "name": "Gold",
                "order": 4.0
            },
            {
                "name": "Silver",
                "order": 3.0
            },
            {
                "name": "Bronze",
                "order": 2.0
            }
        ]
    },
    {
        "name": "Craig Jones",
        "levels": [
            {
                "name": "Silver",
                "order": 3.0
            },
            {
                "name": "Bronze",
                "order": 2.0
            }
        ]
    }
]

```

John Smith, then Craig Jones (because John has Gold) Then, the `levels` collection is sorted.  
However, what I want to do, is return the hits in the same order, but have the nested `levels` collection ordered ASCENDING, so my intended result would be (note the order of `levels is reversed)

```
[
    {
        "name": "John Smith",
        "levels": [
            {
                "name": "Bronze",
                "order": 2.0
            },
            {
                "name": "Silver",
                "order": 3.0
            },
            {
                "name": "Gold",
                "order": 4.0
            },
        ]
    },
    {
        "name": "Craig Jones",
        "levels": [
            {
                "name": "Silver",
                "order": 3.0
            },
            {
                "name": "Bronze",
                "order": 2.0
            }
        ]
    }
]

```

Is this possible to do, or would I need to script this?

Also, possibly related, if I add a query, for example MUST have 'Gold' level, then the ordering of the levels collection isn't respected in the sort:

```
{
    "query": {
        "bool": {
            "filter": [
                {
                    "nested": {
                        "path": "levels",
                        "query": {
                            "bool": {
                                "must": [
                                    {
                                        "term": {
                                            "levels.name.keyword": {
                                                "value": "Gold"
                                            }
                                        }
                                    }
                                ]
                            }
                        }
                    }
                }
            ]
        }
    },
    "size": 10,
    "sort": [
        {
            "levels.order": {
                "nested": {
                    "path": "levels"
                },
                "order": "desc"
            }
        }
    ]
}

```

The resulting levels collection of the first doc is in the order they were inserted:

```
"levels": [
    {
        "name": "Silver",
        "order": 3.0
    },
    {
        "name": "Gold",
        "order": 4.0
    },
    {
        "name": "Bronze",
        "order": 

```

The mapping for this index is below:

```
{
    "index-name": {
        "mappings": {
            "properties": {
                "levels": {
                    "type": "nested",
                    "properties": {
                        "name": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "order": {
                            "type": "double"
                        }
                    }
                },

                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256,
                            "normalizer": "case_insensitive_normalizer"
                        }
                    }
                },

            }
        }
    }
}
```

---

<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:** [January 24, 2020, 6:16pm UTC](https://discuss.elastic.co/t/sorting-a-nested-collection-after-sort-for-display-result-purposes-only/213219/2 "2020-01-24T18:16:15Z")

</div>

Closed on January 24, 2020, 6:16pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
