# Sorting parent documents based on the score of a nested child

**URL:** https://discuss.elastic.co/t/sorting-parent-documents-based-on-the-score-of-a-nested-child/138925
**Category:** Elasticsearch
**Created:** [July 6, 2018, 1:31pm UTC](https://discuss.elastic.co/t/sorting-parent-documents-based-on-the-score-of-a-nested-child/138925 "2018-07-06T13:31:58Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@Uli](https://discuss.elastic.co/u/Uli)
#### Post date: [July 6, 2018, 1:31pm UTC](https://discuss.elastic.co/t/sorting-parent-documents-based-on-the-score-of-a-nested-child/138925/1 "2018-07-06T13:31:58Z")

</div>

Hello,

i have the following mapping:

```
{
  "indexname": {
    "mappings": {
      "object": {
        "properties": {
          "languagedata": {
            "type": "nested",
            "properties": {
              "language": {
                "type": "keyword"
              },
              "title": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword"
                  }
                },
                "analyzer": "standard_lowercase_asciifolding_sort"
              }
            }
          },
          "id": {
            "type": "integer"
          }
        }
      }
    }
  }
}

```

Is it possible to sort the parent documents based on the score, when doing a match query on the nested object 'languagedata'?

```
{
  "sort": [
    {
      "_score": {
        "mode": "max",
        "order": "desc",
        "nested_path": "languagedata",
        "nested_filter": {
          "bool": {
            "must": [
              {
                "match": {
                  "languagedata.title": "search_phrase"
                }
              }
            ],
            "filter": [
              {
                "term": {
                  "languagedata.language": "de"
                }
              }
            ]
          }
        }
      }
    }
  ]
}

```

I know that my sort-example will not work, but it is only an example that shows what I want to do.

Thanks for your help  
Uli

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [July 9, 2018, 1:14pm UTC](https://discuss.elastic.co/t/sorting-parent-documents-based-on-the-score-of-a-nested-child/138925/2 "2018-07-09T13:14:16Z")

</div>

The nested query already returns a score based on the query on the nested object. If you do not have any query clauses apart from the nested query, then you can go with just sorting on `_score`, which is the default sort order.

In other words, this should give you the desired sort order:

```auto
GET indexname/_search
{
  "query": {
    "nested": {
      "score_mode": "max", 
      "path": "languagedata",
      "query": {
        "bool": {
            "must": [
              {
                "match": {
                  "languagedata.title": "search_phrase"
                }
              }
            ],
            "filter": [
              {
                "term": {
                  "languagedata.language": "de"
                }
              }
            ]
          }
      }
    }
  }
}

```

---

<div class="post-metadata">

### Author: ![Uli](https://avatars.discourse-cdn.com/v4/letter/u/ccd318/32.png) [@Uli](https://discuss.elastic.co/u/Uli)
#### Post date: [July 16, 2018, 12:01pm UTC](https://discuss.elastic.co/t/sorting-parent-documents-based-on-the-score-of-a-nested-child/138925/3 "2018-07-16T12:01:24Z")

</div>

And what can I do, in case of other additional query clauses?

---

<div class="post-metadata">

### Author: ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)
#### Post date: [July 16, 2018, 12:50pm UTC](https://discuss.elastic.co/t/sorting-parent-documents-based-on-the-score-of-a-nested-child/138925/4 "2018-07-16T12:50:05Z")

</div>

You could wrap your `nested` query in a `bool` query. You would put the `nested` query in the `must` clause, and the other criteria in a `filter` clause. That way, only your `nested` query will contribute to the score, and the other clauses will not.

---

<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: [August 13, 2018, 12:50pm UTC](https://discuss.elastic.co/t/sorting-parent-documents-based-on-the-score-of-a-nested-child/138925/5 "2018-08-13T12:50:06Z")

</div>

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