Use another document's value for script_score computation

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

          "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

          "function_score": {
                "query": {
                  "multi_match": {
                    "query": "acc",
                    "fields": [
                    "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

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