How to search rank of a document in hits of elasticsearch query?


(Shubham Bhagat) #1

I'm searching a list of documents sorted by "sortScore".

query:

GET /myindex/_search
{
  "from": 0,
  "size": 1000,
  "sort": [
      {
        "sortScore":{"order":"desc"}
      }
  ]
}

sample output:

{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 41619,
      "max_score": null,
      "hits": [
         {
            "_index": "myindex",
            "_type": "stock",
            "_id": "D1393201",
            "sortScore":"1.7972717285156"
            },
            "sort": [
               1.7972717285156
            ]
         },
         {
            "_index": "myindex",
            "_type": "stock",
            "_id": "D1421470",
            "sortScore":"1.7966537475586"
            },
            "sort": [
               1.7966537475586
            ]
         },
         {
            "_index": "myindex",
            "_type": "stock",
            "_id": "D1418136",
            "sortScore":"2.796630859375"
            },
            "sort": [
               2.796630859375
            ]
         }
      ]
   }
}

Now to search a particular document in the above ordering, I can do something like this.

query:

GET /myindex/_search
{
  "from": 0,
  "size": 1000,
  "sort": [
      {
        "sortScore":{"order":"desc"}
      }
  ],
  "query": {
      "bool": {
        "must": [
            {
              "terms": {
                "_id": ["D1421470"]
              }
            }
        ]
      }
    }
}

This will output a particular document of given ID.

Is there any way I can retrieve the rank(in sorted hits) of this particular document? Which should be 2 for above query.


(system) #2

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