title: How to sort elasticsearch results based on number of collapsed items?
body: <p>I'm using a a query with collapse in order to gather some documents under 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:</p>
  
  <pre><code>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"
          ]
        }
      }
    }
  
  }
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Thanks</p>