# Elasticsearch check if key exist in document

**URL:** https://discuss.elastic.co/t/elasticsearch-check-if-key-exist-in-document/303816
**Category:** Elasticsearch
**Created:** [May 3, 2022, 10:32am UTC](https://discuss.elastic.co/t/elasticsearch-check-if-key-exist-in-document/303816 "2022-05-03T10:32:27Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Hovo\_Baghdasaryan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hovo_baghdasaryan/32/105162_2.png) [@Hovo\_Baghdasaryan](https://discuss.elastic.co/u/Hovo_Baghdasaryan)
#### Post date: [May 3, 2022, 10:32am UTC](https://discuss.elastic.co/t/elasticsearch-check-if-key-exist-in-document/303816/1 "2022-05-03T10:32:27Z")

</div>

```auto
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.

```

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [May 5, 2022, 2:23am UTC](https://discuss.elastic.co/t/elasticsearch-check-if-key-exist-in-document/303816/2 "2022-05-05T02:23:48Z")

</div>

Welcome to our community! 😃

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [June 2, 2022, 2:23am UTC](https://discuss.elastic.co/t/elasticsearch-check-if-key-exist-in-document/303816/3 "2022-06-02T02:23:51Z")

</div>

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