# Use another document's value for script\_score computation

**URL:** <https://discuss.elastic.co/t/use-another-documents-value-for-script-score-computation/280511>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [August 5, 2021, 8:44am UTC](https://discuss.elastic.co/t/use-another-documents-value-for-script-score-computation/280511 "2021-08-05T08:44:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![SatyaAditya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/satyaaditya/32/75015_2.png) [@SatyaAditya](https://discuss.elastic.co/u/SatyaAditya)\
**Post date:** [August 5, 2021, 8:44am UTC](https://discuss.elastic.co/t/use-another-documents-value-for-script-score-computation/280511/1 "2021-08-05T08:44:39Z")

</div>

Hi,  
Is there a way to use a different document's field value in the script\_Score query?  
My use-case is I have a query which is asked by multiple users and count of the query asked by the user in this way

```auto
{
          "query_popularity_per_user" : {
            "u-123" : 2,
            "u-456" : 1
          },
          "global_popularity" : 3,
           "query" : "current account",
          "doc_type" : "search_history"
        }

```

And my current query looks like this

```auto
 {
          "function_score": {
                "query": {
                  "multi_match": {
                    "query": "acc",
                    "fields": [
                      "query"
                    ],
                    "minimum_should_match": "40%",
                    "boost": 100
                  }
              }
            },
            "script_score": {
              "script": {
                "lang": "painless",
                "source": "int local_popularity = 0;String key='query_popularity_per_user.u-123';if(doc.containsKey(key) && doc[key].size()>0){local_popularity=(int)doc[key].value;}return Math.log(1+ (0.6*local_popularity) +(0.4*doc['global_popularity'].value))"
              }
            }
          }
        }

```

As you can see the field **query\_popularity\_per\_user** grows indefinitely as the new users who ask the same question increases. For this, to scale, I thought of adding the count of query asked by a user in a different document and use that document's field value in script\_score computation when a user with userId "u-123" asks this query.

My question is, inside script\_score I can access current document fields using the **doc** field but how to access another document's field values inside script\_score

---

<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:** [September 2, 2021, 8:45am UTC](https://discuss.elastic.co/t/use-another-documents-value-for-script-score-computation/280511/2 "2021-09-02T08:45:19Z")

</div>

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