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

**URL:** <https://discuss.elastic.co/t/how-to-search-rank-of-a-document-in-hits-of-elasticsearch-query/135939>\
**Category:** Elasticsearch\
**Created:** [June 14, 2018, 1:51pm UTC](https://discuss.elastic.co/t/how-to-search-rank-of-a-document-in-hits-of-elasticsearch-query/135939 "2018-06-14T13:51:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhagat95](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@bhagat95](https://discuss.elastic.co/u/bhagat95)\
**Post date:** [June 14, 2018, 1:51pm UTC](https://discuss.elastic.co/t/how-to-search-rank-of-a-document-in-hits-of-elasticsearch-query/135939/1 "2018-06-14T13:51:01Z")

</div>

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.

---

<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:** [July 12, 2018, 1:51pm UTC](https://discuss.elastic.co/t/how-to-search-rank-of-a-document-in-hits-of-elasticsearch-query/135939/2 "2018-07-12T13:51:09Z")

</div>

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