# ElasticSearch Sorting parent documents based on number of child documents satisfying some filter

**URL:** <https://discuss.elastic.co/t/elasticsearch-sorting-parent-documents-based-on-number-of-child-documents-satisfying-some-filter/157773>\
**Category:** Elasticsearch\
**Created:** [November 21, 2018, 7:47pm UTC](https://discuss.elastic.co/t/elasticsearch-sorting-parent-documents-based-on-number-of-child-documents-satisfying-some-filter/157773 "2018-11-21T19:47:36Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ashish\_Kumar2](https://avatars.discourse-cdn.com/v4/letter/a/50afbb/32.png) [@Ashish\_Kumar2](https://discuss.elastic.co/u/Ashish_Kumar2)\
**Post date:** [November 21, 2018, 7:47pm UTC](https://discuss.elastic.co/t/elasticsearch-sorting-parent-documents-based-on-number-of-child-documents-satisfying-some-filter/157773/1 "2018-11-21T19:47:37Z")

</div>

**Problem statement** I am having nested documents where I have to apply some filters on nested document and want to sort the parent documents based on the number of nested documents satisfying that filter.

**//Sample problem**

We needed to apply filter and sort in single query, in this order specifically:

1. Filter =\> Blogs having comments from city 'Pune'
2. Sort =\> based on No. of comments coming from 'Pune' city

Sample Data:

```auto
[{
  "title": "Investment secrets",
  "body": "What they don't tell you ...",
  "tags": ["shares", "equities"],
  "comments": [
    {
      "id": 12313,
      "city": "Pune",
      "name": "Mary Brown 1",
      "comment": "Lies, lies, lies ",
      "date": "2018-10-18"
    },
    {
      "id": 12314,
      "city": "Pune",
      "name": "Mary Brown 1",
      "comment": "Lies, lies, lies ",
      "date": "2018-10-20"
    }
  ]
},
{
  "title": "Investment secrets",
  "body": "What they don't tell you ...",
  "tags": ["shares", "equities"],
  "comments": [
    {
      "id": 12315,
      "city": "Pune",
      "name": "Mary Brown ",
      "comment": "Lies, lies, lies ",
      "date": "2018-10-18"
    },
    {
      "id": 12316,
      "city": "Bangalore",
      "name": "Mary Brown ",
      "comment": "Lies, lies, lies ",
      "date": "2018-10-20"
    }
  ]
}]

```

Solutions tried so far:

1. using scripts field

```auto
{  
"query":{  
    "nested":{  
        "path":"comments",
        "inner_hits":{  

        },
        "query":{  
            "term":{  
                "comments.city":{  
                    "value":"Pune"
                }
            }
        }
    }
},
"sort":{  
    "_script":{  
        "script":"params._source.inner_hits.comments.hits.total",
        "type":"number",
        "order":"desc"
    }
}}  

```

Problem with this solution =\> inner\_hits is not accessible here

1. We also tried score\_mode: sum, but it seems it works more correctly with fields already having numeric values, else it would sort but we can't determine it's relevance. And our current data structure doesn't have any numeric field other than ID .(so can't use score\_mode as sum here obviously).

2. we thought about using custom function\_score, but haven't tried it, as it might be complex to decide sorting priority if there are lot of filters.

**So, we were thinking of solving this problem, without using 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:** [December 19, 2018, 7:47pm UTC](https://discuss.elastic.co/t/elasticsearch-sorting-parent-documents-based-on-number-of-child-documents-satisfying-some-filter/157773/2 "2018-12-19T19:47:37Z")

</div>

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