# Search API - Is there a way to get position of document in search response hits?

**URL:** <https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657>\
**Category:** Elasticsearch\
**Created:** [October 22, 2019, 1:53pm UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657 "2019-10-22T13:53:58Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![yairinot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yairinot/32/55698_2.png) [@yairinot](https://discuss.elastic.co/u/yairinot)\
**Post date:** [October 22, 2019, 1:53pm UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/1 "2019-10-22T13:53:58Z")

</div>

**Elasticsearch 6.5.4**

While this looks a trivial task I did not find an easy way to include document position in search response

**Sample query**

```
{
  "query":{
     "bool":{
        "must":[
           {
              "term":{
                 "organizationId":{
                    "value":21,
                    "boost":1.0
                 }
              }
           }
        ]
     }
  }
}

```

**Desired results**. Note the **position** field:

```
    "hits": {
        "total": 25,
        "max_score": null,
        "hits": [
            {
                "_index": "...",
                "_type": "...",
                "_id": "...",
                "position": 0,
                "_source": {...}
            },
            {
                "_index": "...",
                "_type": "...",
                "_id": "...",
                "position": 1,
                "_source": {...}
            }
        ]
    }
```

---

<div class="post-metadata">

**Author:** ![NashSLX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nashslx/32/47551_2.png) [@NashSLX](https://discuss.elastic.co/u/NashSLX)\
**Post date:** [October 22, 2019, 3:30pm UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/2 "2019-10-22T15:30:25Z")

</div>

I'm not sure this is possible in a simple way. Isn't the information available implicitly by the position in the hits array? Or is the hits array getting scrambled by whatever you are using to query elastic?

---

<div class="post-metadata">

**Author:** ![yairinot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yairinot/32/55698_2.png) [@yairinot](https://discuss.elastic.co/u/yairinot)\
**Post date:** [October 22, 2019, 3:58pm UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/3 "2019-10-22T15:58:56Z")

</div>

What do you mean by

> Isn't the information available implicitly by the position in the hits array?

I'm looking for a way to get the position in elasticsearch query and then use this value in an aggregation statement on the same query.

The goal is to get the position of a specific document (by given criteria) in search results.

```
  "query":{
  ...
  },
  "aggs":{
  "position-aggregation":{
    "terms":{
       "script":{
          "source":"if (<condition on doc>) { return doc.position } else { return null }",
          "lang":"painless"
       },
       "size":1,
       "min_doc_count":1,
       "shard_min_doc_count":0,
       "show_term_doc_count_error":false,
       "order":[
          {
             "_count":"desc"
          },
          {
             "_key":"asc"
          }
       ]
    }
 }

```

---

<div class="post-metadata">

**Author:** ![NashSLX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nashslx/32/47551_2.png) [@NashSLX](https://discuss.elastic.co/u/NashSLX)\
**Post date:** [October 22, 2019, 4:14pm UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/4 "2019-10-22T16:14:31Z")

</div>

Ah, I understand your problem now. I was refering to the fact that the position of the doc is obviously also the position in the hits array, but since you're trying to use that inside the same query that is of no help.

I don't know if what you're planning to do is possible. The best I could come up with would be using the [\_score](https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-fields.html#scripting-score) variable, which would at least preserve the relative position of the documents.

---

<div class="post-metadata">

**Author:** ![yairinot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yairinot/32/55698_2.png) [@yairinot](https://discuss.elastic.co/u/yairinot)\
**Post date:** [October 22, 2019, 6:51pm UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/5 "2019-10-22T18:51:57Z")

</div>

@NashSLX thank you for your response.

Could you please elaborate on how to use the \_score to get document relative position?

---

<div class="post-metadata">

**Author:** ![NashSLX](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nashslx/32/47551_2.png) [@NashSLX](https://discuss.elastic.co/u/NashSLX)\
**Post date:** [October 23, 2019, 7:45am UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/6 "2019-10-23T07:45:13Z")

</div>

Well since the score should be inversely proportional to the position, I was just thinking something like this:

```
"script":{
   "source":"if (<condition on doc>) { return someFunction(_score) } else { return null }",
   "lang":"painless"
},

```

SomeFunction could be something simple like 1/\_score, or maybe something more sophisticated. This should give you the relative positioning of all documents that fullfill your condition. However, you obviously wouldn't have the exact position and would lose all information about the "position distance" between the documents.

In addition, the exact \_score values obviously will not between consistent at all between different searches. If you need any of that, then I'm afraid I'm out of ideas for you.

---

<div class="post-metadata">

**Author:** ![yairinot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yairinot/32/55698_2.png) [@yairinot](https://discuss.elastic.co/u/yairinot)\
**Post date:** [October 23, 2019, 8:11am UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/7 "2019-10-23T08:11:22Z")

</div>

As I'm looking for an exact position I'm afraid using the \_score won't be sufficient.

I tried using `script_fields` to get the position but it did not work as expected - For some reason **the position of last element is reset to initial value** :

```
{
  "query":{
     "bool":{
        "must":[
           {
              "term":{
                 "organizationId":{
                    "value":21,
                    "boost":1.0
                 }
              }
           }
        ],
        "adjust_pure_negative":true,
        "boost":1.0
     }
  },
  "_source" : true,
  "script_fields" : {
    "position": {
        "script": {
            "source":"params.counter++",
            "lang":"painless",
            "params":{
                "counter": 0
            }
        }
    }
  }
}

```

In case I had 4 hits from query I received the following response. `position` of last element is reset to initial `counter` value:

```
"hits": {
    "total": 4,
    "hits": [
        {
            "_index": "...",
            ...,
            "fields": {
                "position": [0]
            }
        },
        {
            "_index": "...",
            ...,
            "fields": {
                "position": [1]
            }
        },
        {
            "_index": "...",
            ...,
            "fields": {
                "position": [2]
            }
        },
        {
            "_index": "...",
            ...,
            "fields": {
                "position": [0]
            }
        },
    ]
}
```

---

<div class="post-metadata">

**Author:** ![yairinot](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/yairinot/32/55698_2.png) [@yairinot](https://discuss.elastic.co/u/yairinot)\
**Post date:** [October 23, 2019, 10:42am UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/8 "2019-10-23T10:42:49Z")

</div>

I don't know why, but changing counter type from int to int array solved the issue:

```
	"script_fields" : {
    "position": {
        "script": {
            "source":"params.counter[0]++",
            "lang":"painless",
            "params":{
                "counter": [0]
            }
    	}
    }
}
```

---

<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:** [November 20, 2019, 10:54am UTC](https://discuss.elastic.co/t/search-api-is-there-a-way-to-get-position-of-document-in-search-response-hits/204657/9 "2019-11-20T10:54:04Z")

</div>

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