# Calculated positional field based on \_score

**URL:** <https://discuss.elastic.co/t/calculated-positional-field-based-on-score/274887>\
**Category:** Elasticsearch\
**Created:** [June 3, 2021, 2:03pm UTC](https://discuss.elastic.co/t/calculated-positional-field-based-on-score/274887 "2021-06-03T14:03:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcater](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jcater/32/89782_2.png) [@jcater](https://discuss.elastic.co/u/jcater)\
**Post date:** [June 3, 2021, 2:03pm UTC](https://discuss.elastic.co/t/calculated-positional-field-based-on-score/274887/1 "2021-06-03T14:03:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [June 4, 2021, 10:15pm UTC](https://discuss.elastic.co/t/calculated-positional-field-based-on-score/274887/2 "2021-06-04T22:15:28Z")

</div>

I can't think of any easy way to do that. One thing you can do is to use `1/score` as a proxy for your position, and add to your sort criteria, a sort like this:

```auto
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": "1/_score"
      },
      "order": "desc"
    }
  }

```

---

<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 2, 2021, 10:15pm UTC](https://discuss.elastic.co/t/calculated-positional-field-based-on-score/274887/3 "2021-07-02T22:15:35Z")

</div>

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