Query Filter via Script failing for array object

I've a pretty simple content base containing the following entries (no additional index created):

{
                "_index": "myindex",
                "_type": "entry",
                "_id": "qKlZV2EB1YWWmQYihts_",
                "_score": 1,
                "_source": {
                    "priority": 5,
                    "nodeId": [],
                    "category": [
                        "string"
                    ],
                    "title": null,
                    "description": "string",
                    "created": 1517588940157,
                    "modified": 1517588940157,
                    "author": "test",
                    "audience": [
                        {
                            "authority": "test2",
                            "status": "OPEN"
                        }
                    ]
                }
}

When I try to query the index via a script

{"query":
{
  "bool" : {
    "must" : [
      {
        "script" : {
          "script" : {
            "source" : "if(doc['audience'] == null) {return false;} return true;",
            "lang" : "painless"
          },
          "boost" : 1.0
        }
      }
    ],
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
}
   }

I get the following error:

"reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
                        "org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
                        "if(doc['audience'] == null) {",
                        "       ^---- HERE"
                    ],
                    "script": "if(doc['audience'] == null) {return true;} return true;",
                    "lang": "painless",
                    "caused_by": {
                        "type": "illegal_argument_exception",
                        "reason": "No field found for [audience] in mapping with types []"
                    }
                }

Filtering other fields like "created" or "modified" via the script statement works as expected.
I've also tried to define the field as a nested field via index, but it gave me the same error when requesting via the code above.
Interestingly, accessing doc['audience.authority'] does not fail in the first place, but the array seems empty (values.size() equals 0)

I'm running elasticsearch 6.1.3

1 Like

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