title: Check null parameter in painless - Elasticsearch
body: <p>How are we supposed to check whether a key in map passed as parameter to a painless script has a value? I am currently doing this in Elasticsearch 6.8.4</p>
  
  <pre><code>if (params.feedId!=null) {
      whatever()
  }
  </code></pre>
  
  <p>but it throws this exception when </p>
  
  <blockquote>
    <p>params.feedId</p>
  </blockquote>
  
  <p>is null</p>
  
  <pre><code>ElasticsearchException[Elasticsearch exception [type=illegal_argument_exception, reason=Cannot cast null to a primitive type [void].]]
  </code></pre>
  
  <p>It works fine with Elasticsearch 5.6.13, so there seems to be a breaking change but I couldn't find anything so far.</p>
  
  <p><strong>UPDATE:</strong> It was actually not a problem with this part of the script but in some parts of it we were doing <em>return null;</em> to break the process of the script, which does not seem to be working anymore for some reason. To give more context, I am doing those updates using the Java API, more specifically the Rest High Level client.</p>
  
  <p>I have been able to reproduce the issue using a curl command like this</p>
  
  <pre><code>curl -X POST "localhost:9200/myindex/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
  {
          "script": {
                  "source": "ctx._source.result = true; return null;" 
          }
  }'
  </code></pre>
  
  <p>This produces the next output:</p>
  
  <pre><code>{
    "error" : {
      "root_cause" : [
        {
          "type" : "remote_transport_exception",
          "reason" : "[sBix--f][172.18.0.4:9300][indices:data/write/update[s]]"
        }
      ],
      "type" : "illegal_argument_exception",
      "reason" : "failed to execute script",
      "caused_by" : {
        "type" : "script_exception",
        "reason" : "compile error",
        "script_stack" : [
          "... ce.result = true; return null;",
          "                             ^---- HERE"
        ],
        "script" : "ctx._source.result = true; return null;",
        "lang" : "painless",
        "caused_by" : {
          "type" : "illegal_argument_exception",
          "reason" : "Cannot cast null to a primitive type [void]."
        }
      }
    },
    "status" : 400
  }
  </code></pre>
  
  <p>Just replacing <em>return null;</em> for <em>return;</em> makes this script working fine in ES 6.8.4.</p>
  
  <p>Found this issue at Elasticsearch: <a href="https://github.com/elastic/elasticsearch/issues/35888" rel="nofollow noreferrer">https://github.com/elastic/elasticsearch/issues/35888</a>. There are some inconsistencies in the way short-circuit works in different versions of Elasticsearch.</p>