# Score document by same value of the field

**URL:** https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628
**Category:** Elasticsearch
**Created:** [February 13, 2018, 12:13pm UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628 "2018-02-13T12:13:13Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![mstkk](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mstkk](https://discuss.elastic.co/u/mstkk)
#### Post date: [February 13, 2018, 12:13pm UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628/1 "2018-02-13T12:13:13Z")

</div>

Let's say you have 5 products from same users and u you list them  
If a **user\_id** (field) have same value in 5 documents, i want the fifth document to have the least document score  
the score should gradually decrease from the first to the fifth

**expected score**

```
"hits": [
      {
        "_type": "product",
        "_id": "74162",
        "_score": 1,
        "_source": {
          "user_id": 90
        } 
     },
     {
        "_type": "product",
        "_id": "6",
        "_score": 1,
        "_source": {
          "user_id": 35
        } 
     }
     {
        "_type": "product",
        "_id": "2",
        "_score": 0.9,
        "_source": {
          "user_id": 90
        } 
     },
     {
        "_type": "product",
        "_id": "3",
        "_score": 0.8,
        "_source": {
          "user_id": 90
        } 
     },
    {
        "_type": "product",
        "_id": "4",
        "_score": 0.7,
        "_source": {
          "user_id": 90
        } 
     },
    {
        "_type": "product",
        "_id": "5",
        "_score": 0.6,
        "_source": {
          "user_id": 90
        } 
     }

]
```

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 13, 2018, 12:57pm UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628/2 "2018-02-13T12:57:17Z")

</div>

You need to compute at index time a field that adds to your doc its position number and then use that field in a function score query for example.

You can't do that at search time I think as documents are not relatively scored based on other document counts.

---

<div class="post-metadata">

### Author: ![mstkk](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mstkk](https://discuss.elastic.co/u/mstkk)
#### Post date: [February 13, 2018, 1:34pm UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628/3 "2018-02-13T13:34:47Z")

</div>

what about if i want to do it in search time because there's so many filters like city filter or town filter or category filter and i have to calculate all of this and score one by one its too hard to manage

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 13, 2018, 3:02pm UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628/4 "2018-02-13T15:02:19Z")

</div>

I don't think you can. May be others have ideas.

---

<div class="post-metadata">

### Author: ![mstkk](https://avatars.discourse-cdn.com/v4/letter/m/838e76/32.png) [@mstkk](https://discuss.elastic.co/u/mstkk)
#### Post date: [February 13, 2018, 3:12pm UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628/5 "2018-02-13T15:12:59Z")

</div>

what if i scripted field like this

```
 "script_fields" : {
    "test1" : {
        "script" : {
            "lang": "painless",
            "source": "doc['count_of_users_result'].value +1"
        }
    }

```

}  
i just dont know how to count them when scripting how to reach other documents fields of result and count them

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 13, 2018, 3:27pm UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628/6 "2018-02-13T15:27:48Z")

</div>

> i just dont know how to count them when scripting how to reach other documents fields of result and count them

That's the thing. Everything which is happening per document is doable. Everything which is related to a previous one is not doable.

May be by writing your own query as a Java plugin but I'm not sure how all that would work in a distributed way.

---

<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: [April 1, 2018, 12:27am UTC](https://discuss.elastic.co/t/score-document-by-same-value-of-the-field/119628/8 "2018-04-01T00:27:36Z")

</div>

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