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

**URL:** <https://discuss.elastic.co/t/assigning-an-arbitrary-number-to-a-document-based-on-sort-order/13090>\
**Category:** Elasticsearch\
**Created:** [August 6, 2013, 3:01pm UTC](https://discuss.elastic.co/t/assigning-an-arbitrary-number-to-a-document-based-on-sort-order/13090 "2013-08-06T15:01:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Smith](https://avatars.discourse-cdn.com/v4/letter/d/a3d4f5/32.png) [@David\_Smith](https://discuss.elastic.co/u/David_Smith)\
**Post date:** [August 6, 2013, 3:01pm UTC](https://discuss.elastic.co/t/assigning-an-arbitrary-number-to-a-document-based-on-sort-order/13090/1 "2013-08-06T15:01:41Z")

</div>

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! 🙂

--  
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](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:22am UTC](https://discuss.elastic.co/t/assigning-an-arbitrary-number-to-a-document-based-on-sort-order/13090/2 "2017-07-06T02:22:40Z")

</div>


