Bypass null_pointer_exception when sorting Scripted Fields Painless

Hi All, I am using NEST to generate the following request to Elastic Search 6.3:

"from": 0,
"size": 14,
"sort": [
"_script": {
"order": "asc",
"type": "number"
"script": {
"source": "if( (doc['1'].value-doc['2'].value) > 10 ) { (doc['1'].value-doc['2'].value) }"

My script does return null which I cant control since it can be a dynamic script because of which I end up getting the following error:
"error": {

  • "root_cause": [
    • {
      • "type": "null_pointer_exception",
      • "reason": null}],
  • "type": "search_phase_execution_exception",
  • "reason": "all shards failed",
  • "phase": "query",
  • "grouped": true,...

How can I bypass this error? Is it possible to do this without modifying the script? The script can be dynamic and the query is generated through a NEST object. Why cant the script sort work with null value results?

I don't think the null pointer exception is because you are returning null, but instead because you are trying to access a field which has no values for a document. Are you sure all documents value values for fields 1 and 2? Could you please look in your elasticsearch log and find the full stack trace for the NullPointerException?

Well, If I change the script to:

if( (doc['1'].value-doc['2'].value) > 10 ) { (doc['1'].value-doc['2'].value) } else { return 0}

The sort works fine. I dont think there seems to be an issue with the documents having nulls in them.

I think I see what is happening here now. When painless is missing a return statement, it will by default return null. By not having an else case for your if, the null was returned and causing an NPE when trying to convert to double (the return type of numeric sort scripts).

Is there a reason you can't keep the else case with 0 (or whatever else you want as the default when your condition is not met)?

Well the script is mostly dynamic and could be anything since it is entered by a user according to the way he wants the data. There could be plenty of scripts like these in our system which we might not be able to correct. They run without any issues and display null in our grids. Is there a way to handle these nulls and handle them when sorting?

This is already changed in 6.5.0. The signature for numeric sort scripts changed from returning Object (which is what causes painless to do an implicit return null) to return double, which painless then will return 0.0 with a missing else. Any handling would have to be on the client side.

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