# How to update by query to create incrementing ranking from sorted query in elasticsearch 6.4

**URL:** <https://discuss.elastic.co/t/how-to-update-by-query-to-create-incrementing-ranking-from-sorted-query-in-elasticsearch-6-4/150637>\
**Category:** Elasticsearch\
**Created:** [October 2, 2018, 4:07am UTC](https://discuss.elastic.co/t/how-to-update-by-query-to-create-incrementing-ranking-from-sorted-query-in-elasticsearch-6-4/150637 "2018-10-02T04:07:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![n0pe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/n0pe/32/45191_2.png) [@n0pe](https://discuss.elastic.co/u/n0pe)\
**Post date:** [October 2, 2018, 4:07am UTC](https://discuss.elastic.co/t/how-to-update-by-query-to-create-incrementing-ranking-from-sorted-query-in-elasticsearch-6-4/150637/1 "2018-10-02T04:07:30Z")

</div>

I'm trying to update\_by\_query to rank items in sorted search. Is it possible to get the current "index" of the document in the script?

eg.

create index:

```
PUT test
{
    "mappings" : {
        "person" : {
            "properties" : {
                "score" : { "type" : "integer" },
                "rank" : { "type" : "integer" }
            }
        }
    }
}  

```

add documents:

```
POST /test/person/1
{
  "score": 100,
  "rank": 0
}
POST /test/person/2
{
  "score": 200,
  "rank": 0
}

```

update\_by\_query

```
POST /test/_update_by_query
{
  "script": {
    "source": "ctx._source.rank=3", // how to get document "index" in sort here?
    "lang": "painless"
  },
  "sort": { "score": "desc" }
}

```

results when sorting by ascending rank should be

score: 200, rank: 1  
score: 100, rank: 2

---

<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:** [October 30, 2018, 4:07am UTC](https://discuss.elastic.co/t/how-to-update-by-query-to-create-incrementing-ranking-from-sorted-query-in-elasticsearch-6-4/150637/2 "2018-10-30T04:07:31Z")

</div>

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