Ternary operator: returning default values for missing document values

Hi,
I have troubles to execute the following painless script (used inside a FunctionScoreQuery):

1200 * Math.pow((doc.containsKey(params.population_key) ? doc[params.population_key].value : 0), 0.05)

The params.population_key is a string and the field is present only in some documents, it's mapping is defined as a dynamic template of type double.

The execution works but logs following error:

returning default values for missing document values is deprecated. Set system property '-Des.scripting.exception_for_missing_value=true' to make behaviour compatible with future major versions!

It seems to me that the true branch of the ternary expression is evaluated eagerly, maybe because some kind of type conversion or so? Could you please suggest a way how to rewrite the script so I don't get any warning?

Thank you!
Tomas

1 Like

I have the same problem. The painless language seems to be rather painful here. :frowning:

1 Like

I've probably found the problem. Inside the script, the doc object is a prepared data structure in a tabular format, optimized for fast access. It's not the underlying source document, which we indexed.

So the doc most likely contains all keys known from the index mapping. Which means that doc.containsKey('any_mapping_key') evaluates to true and the first branch of the ternary operator is always triggered.

I solved the problem by using doc[params.population_key].empty condition instead. It makes sense to combine this condition with doc.containsKey(params.population_key) if there is a chance that the param will be something not known from mappings.

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