# Sort documents based on matched inner nested objects

**URL:** https://discuss.elastic.co/t/sort-documents-based-on-matched-inner-nested-objects/342150
**Category:** Elasticsearch
**Created:** [September 1, 2023, 9:09pm UTC](https://discuss.elastic.co/t/sort-documents-based-on-matched-inner-nested-objects/342150 "2023-09-01T21:09:45Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![akarsh\_cholaveti](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@akarsh\_cholaveti](https://discuss.elastic.co/u/akarsh_cholaveti)
#### Post date: [September 1, 2023, 9:09pm UTC](https://discuss.elastic.co/t/sort-documents-based-on-matched-inner-nested-objects/342150/1 "2023-09-01T21:09:45Z")

</div>

Hello,

I am working on a sort query for my documents in the index. Here are the documents look like:

```auto
{
    "studentId": "123",
    "studentName": "Frodo",
    "year": "2023",
    "Scores": [{
            "subject": "Physics",
            "score": "100"
        },
        {
            "subject": "Chemistry",
            "score": "700"
        }
    ]
}

```

```auto
{
    "studentId": "456",
    "studentName": "Samwise",
    "year": "2023",
    "Scores": [{
            "subject": "Physics",
            "score": "200"
        },
        {
            "subject": "Chemistry",
            "score": "600"
        }
    ]
}

```

```auto
{
    "studentId": "789",
    "studentName": "Merry",
    "year": "2023",
    "Scores": [{
            "subject": "Physics",
            "score": "300"
        },
        {
            "subject": "Chemistry",
            "score": "500"
        }
    ]
}

```

I am trying to get results in descending order that falls in the range. For example, I am running following query

```auto
{
    "size": 10000,
    "query": {
        "bool": {
            "must": [{
                    "term": {
                        "year": "2023"
                    }
                },
                {
                    "range": {
                        "Scores.score": {
                            "lte": 200
                        }
                    }
                }
            ]
        }
    },
    "sort": [{
        "Scores.score": {
            "order": "desc"
        }
    }]
}

```

with the above query, I am getting the 2 docs as expected but I am getting doc with '100' score first instead of '200'. I think when ES is performing the sort, it is picking up 700 first and 600 next and giving the documents in that order. How can I do the query that sorts only on matched inner fields? In this case, the sorting should consider only 200, 100 scores and ignore others. Any help would be appreciated. Thanks!!

---

<div class="post-metadata">

### Author: ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)
#### Post date: [September 1, 2023, 10:06pm UTC](https://discuss.elastic.co/t/sort-documents-based-on-matched-inner-nested-objects/342150/2 "2023-09-01T22:06:30Z")

</div>

Hi @akarsh_cholaveti

Did you try to use [Sort Mode Option](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#_sort_mode_option)?.

---

<div class="post-metadata">

### Author: ![akarsh\_cholaveti](https://avatars.discourse-cdn.com/v4/letter/a/ecc23a/32.png) [@akarsh\_cholaveti](https://discuss.elastic.co/u/akarsh_cholaveti)
#### Post date: [September 1, 2023, 10:54pm UTC](https://discuss.elastic.co/t/sort-documents-based-on-matched-inner-nested-objects/342150/3 "2023-09-01T22:54:59Z")

</div>

@RabBit_BR Thanks for your response. I have tried the sort mode option with `min` mode. It works with this particular example but it doesn't work with different scenario. For example, if I use the score as `800` in the same query with `min` mode, I am getting doc3 (score 300 and 500) as response but I expect doc1(score 100 and 700) since 700 is highest score.

---

<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: [September 29, 2023, 10:55pm UTC](https://discuss.elastic.co/t/sort-documents-based-on-matched-inner-nested-objects/342150/4 "2023-09-29T22:55:27Z")

</div>

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