Painless pains: Weird behaviour with try-catch and dotProduct functions

Hello, in our team we use painless scripting quite heavily and have come across some very weird behaviour from time to time, the latest having to do with the following piece of code:

try {
    double test = dotProduct(k_vector, vector_name);
} catch(Exception e) {
    double test = dotProduct(k_vector, vector_name);
}

Where "k_vector" is a dense_vector of size 50 and "vector_name" is the name of the field containing the other dense_vector inside the document.

On some documents, the first execution of the function throws the exception "java.lang.IllegalArgumentException: A document doesn't have a value for a vector field!" however the second execution (inside the catch statement) works fine and returns the correct value for the dotProduct of the vectors.
We took a thorough look at the documents and couldn't find any difference between those that work and those that don't, so we're at a loss here.

Can anyone explain this behaviour? Is it a bug or are we missing something?

1 Like

Hi Alessio!

After looking through the code for this specific function, this certainly does seem like an impossibility. Would you please be able to provide some more information? Specifically, the original script where this first occurred, the entirety of the stack trace, your mappings, and an example document.

Thank you!
Jack