How to sort results based on number of collapsed items?

(Roy Granit) #1

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:

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! :sunglasses:

(Junaid) #2

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

(Roy Granit) #3

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.

(system) closed #4

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