I need to add a position/ordinal field to my Elasticsearch search results, relative to a sort by _score, regardless of how the actual results are sorted. I'm using the term "field" loosely... I just need access to some ordinal number.
Currently, I only sort my documents by _score. So, I can just calculate this position/ordinal in the calling code. (i.e., the first result is always position #1, the second is #2, the nth is #n, etc.) This is as straight-forward as you can get:
Position, First, Last, City, State (Score)
1, John, Smith, Memphis, TN (_score: 0.9)
2, Jane, Doe, Atlanta, GA (_score: 0.8)
3, James, Wilson, San Francisco, CA (_score: 0.6)
4, Jeff, Smith, Dallas, TX (_score: 0.5)
5, Kevin, Richards, Austin, TX (_score: 0.4)
However, I now have the need to sort by other fields, but I still need to know the relative position based on sorting by _score. For example, if I sort by City in the above example, I still need the Position calculated as if we're sorting by score:
Position, First, Last, City, State (Score)
2, Jane, Doe, Atlanta, GA (_score: 0.8)
5, Kevin, Richards, Austin, TX (_score: 0.4)
4, Jeff, Smith, Dallas, TX (_score: 0.5)
1, John, Smith, Memphis, TN (_score: 0.9)
3, James, Wilson, San Francisco, CA (_score: 0.6)
To do this without pulling all results from Elasticsearch and doing my own sorting in the calling code (which isn't feasible), I need to shift the calculation of this value to Elasticsearch somehow.
Is there a mechanism to do this in Elasticsearch that I'm overlooking?