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


#1

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


(system) #2

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