# How to write query for a property which exists but can also not exist?

**URL:** <https://discuss.elastic.co/t/how-to-write-query-for-a-property-which-exists-but-can-also-not-exist/262544>\
**Category:** Kibana\
**Tags:** kql-kibana-query-language\
**Created:** [January 28, 2021, 6:17pm UTC](https://discuss.elastic.co/t/how-to-write-query-for-a-property-which-exists-but-can-also-not-exist/262544 "2021-01-28T18:17:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmuresan](https://avatars.discourse-cdn.com/v4/letter/l/d07c76/32.png) [@lmuresan](https://discuss.elastic.co/u/lmuresan)\
**Post date:** [January 28, 2021, 6:17pm UTC](https://discuss.elastic.co/t/how-to-write-query-for-a-property-which-exists-but-can-also-not-exist/262544/1 "2021-01-28T18:17:51Z")

</div>

I have the next weird case: I have the fruitBox. This fruit box can have apples, pears, bananas. But it also can be empty, in this case it doesn't have any property for box.  
Now lets say that i want to get from the Elasticsearch all the boxes with apples, pears and the ones that are empty.  
For the boxes with fruits in it i have :  
\</\>

```auto
{
      "bool": {
       "should": [
         {"match_phrase": {"fruit": "apple"}},
         {"match_phrase": {"fruit": "pear"}},
          ]
      }
    },

```

\</\>  
Which works fine!  
But if i add the condition to get also the empty boxes:  
\</\>

```auto
    "bool": {
      "must_not": {
        "exists": {
          "field": "fruit"
        }
      }
}

```

\</\>  
I get 0 hits.  
Thank you!

---

<div class="post-metadata">

**Author:** ![wylie](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/wylie/32/81794_2.png) [@wylie](https://discuss.elastic.co/u/wylie)\
**Post date:** [January 28, 2021, 6:21pm UTC](https://discuss.elastic.co/t/how-to-write-query-for-a-property-which-exists-but-can-also-not-exist/262544/2 "2021-01-28T18:21:28Z")

</div>

Are you using KQL for this, or the ES query DSL directly? I believe the KQL way to do this is `NOT fruitBox: * OR fruitBox: apple OR fruitBox: pear`. With the query DSL you can add an inner `bool` to your `should` clause, so you can do something resembling `should > bool > must_not > exists`

---

<div class="post-metadata">

**Author:** ![lmuresan](https://avatars.discourse-cdn.com/v4/letter/l/d07c76/32.png) [@lmuresan](https://discuss.elastic.co/u/lmuresan)\
**Post date:** [January 28, 2021, 6:37pm UTC](https://discuss.elastic.co/t/how-to-write-query-for-a-property-which-exists-but-can-also-not-exist/262544/3 "2021-01-28T18:37:08Z")

</div>

I was using DSL.  
Thanks a lot!

---

<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:** [February 25, 2021, 6:37pm UTC](https://discuss.elastic.co/t/how-to-write-query-for-a-property-which-exists-but-can-also-not-exist/262544/4 "2021-02-25T18:37:12Z")

</div>

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