peterkim95
peterkim95

Reputation: 21

ElasticSearch painless scripts - Way to output variable values besides the final score?

I am using a painless script to implement a custom scoring function while querying the ES index, that's being used as a basis for our recommendation engine. While calculating the final score in the painless script, I use a product of intermediate variables such as recency and uniqueness, calculated within the painless script.

Now, it is trivial to get the final scores of the top documents as they are returned within the query response. However, for detailed analysis, I'm trying to find a way to also get the intermediate variables' values (recency and uniqueness as in the above example). I understand these painless variables only exist within the context of the painless script, which does not have standard REPL setup. So is there really no way to access these painless variables? Has anyone found a workaround to do this? Thanks!

E.g. If I have the following simplified painless script:

def recency = 1/doc['date'].value
def uniqueness = doc['ctr].value

return recency * uniqueness

In the final ES response, I get the scores i.e. recency * uniqueness. However, I also want to know what the intermediate variables are i.e. recency and uniqueness

Upvotes: 1

Views: 9429

Answers (2)

Apurv Sheth
Apurv Sheth

Reputation: 1

There is not direct way of printing it anywhere i suppose. But here is what you can give it a try to check the intermediate output of any variable.

  • create another scripted field which will return only the value of that variable.

For Ex: in your case,

"script_fields": {
"derivedRecency": {
      "script": {
        "lang": "painless",
        "source": """ 
            return doc['recency'].value;
            
          """
      }
    }
}

Upvotes: 0

Aritra Nayak
Aritra Nayak

Reputation: 61

You can try using a modular approach with multiple scripted fields like:

  1. recency -- get the recency field
  2. uniqueness -- get the uniqueness field

  3. access the fields like a normal ES field in your final painless script

if(doc.containsKey('recency.keyword') && doc.containsKey('uniqueness.keyword'))
{
  def val1 = doc['recency.keyword'].value;
  def val2 = doc['uniqueness.keyword'].value;
}

Hope it helps

Upvotes: 1

Related Questions