Elasticsearch check if key exist in document

Hi All.

I have key "field1" in the document of Elasticsearch. With mapping, a field is an object. sometimes field does not exist, sometimes the field exists but is empty ([]), sometimes the field has the correct value -

"field1": [
   {"key" : "value"},
   {"key" : "value1"}
]
Now I want to write a filter with script query but get this kind of error

"caused_by": {
     "type": "illegal_argument_exception",
     "reason": "No field found for [field1] in mapping with types []"
   }
Even when I write a simpler query

if (doc.containsKey('field1')) {return true;} else {return false;}
but I have many documents which have the field. I think containsKey doesn't check the first level in a document but checks the full path.

Can somebody help me to check "field1" exists in the document or not?

But the main problem is to search documents which's field1.key = value1. As "field1" is an array I wrote a loop for that but got the error that I wrote above. 

Thanks in advance.

Welcome to our community! :smiley:

Can you please edit your post and fix the formatting, it's extremely hard to follow what you are asking as it's currently shown.