# How to sort results based on number of collapsed items?

**URL:** https://discuss.elastic.co/t/how-to-sort-results-based-on-number-of-collapsed-items/172673
**Category:** Elasticsearch
**Created:** [March 17, 2019, 11:47am UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-number-of-collapsed-items/172673 "2019-03-17T11:47:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![rgranit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rgranit/32/42186_2.png) [@rgranit](https://discuss.elastic.co/u/rgranit)
#### Post date: [March 17, 2019, 11:47am UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-number-of-collapsed-items/172673/1 "2019-03-17T11:47:30Z")

</div>

Hi everyone,

I'm using a a query with a collapse in order to gather hits in certain documents that belong to a certain person, yet I wish to sort the results based on the number of documents in which the search found a match..

This is my query:

```auto
GET documents/_search
{
  "_source": {
    "includes": [
      "text"
    ]
  },
  "query": {
    "query_string": {
      "fields": [
        "text"
      ],
      "query": "some text"
    }
  },
  "collapse": {
    "field": "person_id",
    "inner_hits": {
      "name": "top_mathing_docs",
      "_source": {
        "includes": [
          "doc_year",
          "text"
        ]
      }
    }
  }

}

```

Any suggestions?

Thanks! 🕶

---

<div class="post-metadata">

### Author: ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)
#### Post date: [March 17, 2019, 6:17pm UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-number-of-collapsed-items/172673/2 "2019-03-17T18:17:11Z")

</div>

Hi,

I am not sure whether I understand your use case properly.

Collapse operation is performed on the result set that matches a certain result set to be returned. You will be needing to look at the aggregation in order to determine the top used `person_id` field inside your index.

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-collapse.html)

---

<div class="post-metadata">

### Author: ![rgranit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rgranit/32/42186_2.png) [@rgranit](https://discuss.elastic.co/u/rgranit)
#### Post date: [March 17, 2019, 8:08pm UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-number-of-collapsed-items/172673/3 "2019-03-17T20:08:08Z")

</div>

What I wish to accomplish is to search all documents for a certain string and next group the documents that have a hit by their author.  
In the response json I get "top\_mathing\_docs" which contains the number of documents found for each person, yet even after reading about aggregation, I'm not sure how to access that number in order to sort the results according to it.

---

<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: [April 14, 2019, 8:08pm UTC](https://discuss.elastic.co/t/how-to-sort-results-based-on-number-of-collapsed-items/172673/4 "2019-04-14T20:08:12Z")

</div>

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