Migration to Elasticsearch 6 : request with script and params not working anymore

226 Views Asked by At

I am migrating my Elasticsearch from v5.16 to 6.8 (and after to 7.16) but I have some problem with this type of request (see below) : using params['_source'] in a script I don't understand why. I didn't find any breakings changes in the documentation. Can you please help me ? FYI : the index mapping doesn't not contain "evts"

Thanks

{
  "query": {
    "bool": {
      "must": [
        { "match": { "closed": "false" }
        },
        {
          "script": {
            "script": {
              "source": "(params['_source']['evts'] !== null) && (params['_source']['evts']).length > 0",
              "lang": "painless"
            }
          }
        }
      ]
    }
  }}

The response

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 2,
    "skipped": 0,
    "failed": 3,
    "failures": [
      {
        "shard": 1,
        "index": "myIndex",
        "node": "XXX",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "(params['_source']['evts'] !== null) && (params['_source']['evts']).length > 0",
            "       ^---- HERE"
          ],
          "script": "(params['_source']['evts'] !== null) && (params['_source']['piecesJointes']).length > 0",
          "lang": "painless",
          "caused_by": {
            "type": "null_pointer_exception",
            "reason": "Cannot invoke \"Object.getClass()\" because \"callArgs[0]\" is null"
          }
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": null,
    "hits": []
  }
}
1

There are 1 best solutions below

0
Pierre Mallet On BEST ANSWER

summary from the comments :

As explain here since since version 6.X its no longer possible to access the source in query to filter documents. Only doc_values are accessible ( see here ).

The clean way to execute such query without script is to add evts to the mapping and use an exists query combined with a must_not clause.

In this case since evts is a nested field a working query can be

{
      "query": {
        "bool": {
          "must": [
            {
              "nested": {
                "path": "evts",
                "query": {
                  "exists": {
                    "field": "evts"
                  }
                }
              }
            }
          ]
        }
      }
    }