# Filter by field only if the field exists

**URL:** <https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219>\
**Category:** Elasticsearch\
**Created:** [March 7, 2019, 2:53am UTC](https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219 "2019-03-07T02:53:48Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![all9lives](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/all9lives/32/41653_2.png) [@all9lives](https://discuss.elastic.co/u/all9lives)\
**Post date:** [March 7, 2019, 2:53am UTC](https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219/1 "2019-03-07T02:53:48Z")

</div>

I need to filter items by a certain field value, only if that field exists, otherwise it should include items which don't have that field value

Here's the example, a **Todo app** with assigned `users` and `projects`. I need to be able to get all items which are assigned to `userA` or `userB` and if any or the items are part of a project, only get the items from `projectX.

I'm using `"must"` to get items which are assigned to the users, this works as expected. But when I try to use `"should"` and `"must"` to get items where if `projectId.keyword` `exists` make sure that it equals `projectX`, but I also get items from other projects, i.e. `projectY` and `projectZ`, so the `projectId.keyword` constraint isn't working.

**Here's my query.**

```auto
{
  "query": {
    "bool": {
      "must": {
        "terms": {
          "assignedTo.keyword": [
            "userA",
            "userB"
          ]
        }
      },
      "should": [
        {
          "bool": {
            "must": [
              {
                "exists": {
                  "field": "projectId.keyword"
                }
              },
              {
                "term": {
                  "projectId.keyword": "projectX"
                }
              }
            ]
          }
        },
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "projectId.keyword"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

```

Maybe there's a way to use `must_not` to make sure `projectId.keyword` doesn't equal something other than `projectX`?

---

<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:** [March 7, 2019, 3:59am UTC](https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219/2 "2019-03-07T03:59:29Z")

</div>

It looks good. What is the result you can see?

It could be great to provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible as you did so far.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [March 7, 2019, 8:47am UTC](https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219/3 "2019-03-07T08:47:59Z")

</div>

@all9lives

Within Elasticsearch query context if you are using should with must (clearly is your case), you should provide `minimum_should_match` parameter equal to 1 so that your should clause is evaluated as well.

[https://www.elastic.co/guide/en/elasticsearch/reference/6.6/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/6.6/query-dsl-bool-query.html)

Hope this helps out.

---

<div class="post-metadata">

**Author:** ![all9lives](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/all9lives/32/41653_2.png) [@all9lives](https://discuss.elastic.co/u/all9lives)\
**Post date:** [March 7, 2019, 6:40pm UTC](https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219/4 "2019-03-07T18:40:33Z")

</div>

Thanks @mjunaidmuzammil `"minimum_should_match": 1` worked for me.

Thanks for your help 😄 🙏

---

<div class="post-metadata">

**Author:** ![mjunaidmuzammil](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mjunaidmuzammil/32/56910_2.png) [@mjunaidmuzammil](https://discuss.elastic.co/u/mjunaidmuzammil)\
**Post date:** [March 8, 2019, 4:43am UTC](https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219/5 "2019-03-08T04:43:00Z")

</div>

Great. Nice to know that it worked for you. 🙂

Perhaps you can close the topic now.

---

<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:** [April 5, 2019, 4:43am UTC](https://discuss.elastic.co/t/filter-by-field-only-if-the-field-exists/171219/6 "2019-04-05T04:43:02Z")

</div>

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