Assigning an arbitrary number to a document, based on sort order

Hi all,

I'm struggling to get my head around a certain use-case that I'm trying to
code up at the moment.

Essentially, I am trying to assign an auto incrementing value to a document
based on it's sort order within Elasticsearch. I'm doing this as I have a
rankings system where ranks are generated dynamically, based upon the
search criteria given by the user, and so these ranks cannot be
predetermined (they HAVE to be calculated by Elasticsearch at runtime).

I have worked out how to calculate and sort the ranks for each document,
using the "avg" sort mode option when sorting. The part I'm having
difficulty with is assigning an arbitrary number to each document to
signify it's position in the resultset, based on rank. I need this to be
returned in the resultset for each document so that I'm able to display
this rank, regardless of how the final result set is sorted.

For example, given the following documents....
{
name: "Dave",
data: [1, 2, 3, 4]
}

{
name: "John",
data: [4, 5, 6, 7]
}

{
name: "Alex",
data: [10, 11, 12, 13]
}

After running the query and ordering by "name asc", I want to get the
result set: (assuming that a lower rank number is "better")

[
{
name: 'Alex',
data: [10, 11, 12, 13],
rank: 3
},
{
name: 'Dave',
data: [1, 2, 3, 4],
rank: 1
},
{
name: 'John',
data: [4, 5, 6, 7],
rank: 2
}
]

So my question is.... what kind of search request would I need to make in
order to generate this "rank" number?

Thanks for reading! :slight_smile:

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.