# How to query for empty field or specific value

**URL:** <https://discuss.elastic.co/t/how-to-query-for-empty-field-or-specific-value/119623>\
**Category:** Elasticsearch\
**Created:** [February 13, 2018, 11:38am UTC](https://discuss.elastic.co/t/how-to-query-for-empty-field-or-specific-value/119623 "2018-02-13T11:38:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andresilveira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andresilveira/32/25677_2.png) [@andresilveira](https://discuss.elastic.co/u/andresilveira)\
**Post date:** [February 13, 2018, 11:38am UTC](https://discuss.elastic.co/t/how-to-query-for-empty-field-or-specific-value/119623/1 "2018-02-13T11:38:02Z")

</div>

I'm trying to write a query to achieve the following result:

```
field1: value1 AND 
field2: value2 OR "" (empty)

```

I've tried the following but it always returns me empty results:

```
{
  "_source": ["field1", "field2"], 
  "query": {
    "bool": {
      "must": [
        {"match": { "field1": "value1" }},
        {
          "bool": {
            "should": [
              { 
                "match": {
                  "field2": "value2"
                }
              }
            ],
            "must_not": [
              {
                "exists": { "field": "field2" }
              }
            ]
          }
        }
      ]
    }
  }
}

```

I believe I'm making a query that contradicts itself.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [February 13, 2018, 11:42am UTC](https://discuss.elastic.co/t/how-to-query-for-empty-field-or-specific-value/119623/2 "2018-02-13T11:42:10Z")

</div>

I think you need to put the `exists` query inside the `should` clause as a new `bool` query with a `must_not` clause.

---

<div class="post-metadata">

**Author:** ![andresilveira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andresilveira/32/25677_2.png) [@andresilveira](https://discuss.elastic.co/u/andresilveira)\
**Post date:** [February 16, 2018, 10:56am UTC](https://discuss.elastic.co/t/how-to-query-for-empty-field-or-specific-value/119623/3 "2018-02-16T10:56:36Z")

</div>

Hey @dadoonet thank you for your tip. That's what I ended up with.

```json
{
  "query": {
    "bool": {
      "must": [
        {"match": { "field1": "value1" }},
        {
          "bool": {
            "should": [
              { "match": { "field2": "value2" }},
              {
                "bool": {
                  "must_not": [
                    { "exists": { "field": "field2" }}
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

Your guess was correct.

---

<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:** [March 16, 2018, 10:56am UTC](https://discuss.elastic.co/t/how-to-query-for-empty-field-or-specific-value/119623/4 "2018-03-16T10:56:38Z")

</div>

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