# Return all results where field is missing or false

**URL:** https://discuss.elastic.co/t/return-all-results-where-field-is-missing-or-false/156162
**Category:** Elasticsearch
**Created:** [November 10, 2018, 8:44pm UTC](https://discuss.elastic.co/t/return-all-results-where-field-is-missing-or-false/156162 "2018-11-10T20:44:50Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![i333](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@i333](https://discuss.elastic.co/u/i333)
#### Post date: [November 10, 2018, 8:44pm UTC](https://discuss.elastic.co/t/return-all-results-where-field-is-missing-or-false/156162/1 "2018-11-10T20:44:50Z")

</div>

How could I return all results where `myboolean` field below is either missing or `false`? I want it to be under `filter` because I don't want it to contribute to the store, it should just remove all results where `myboolean` is `true`. This is the rough idea I had of what it should look like but it doesn't work.

```
{
	"query": {
		"bool": {
			"filter": [
				{ "must_not": { "exists": { "field": "myboolean" } } },
				{ "term": { "myboolean": false } }
			]
		}
	}
}
```

---

<div class="post-metadata">

### Author: ![mats990](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mats990/32/37516_2.png) [@mats990](https://discuss.elastic.co/u/mats990)
#### Post date: [November 11, 2018, 7:55am UTC](https://discuss.elastic.co/t/return-all-results-where-field-is-missing-or-false/156162/2 "2018-11-11T07:55:28Z")

</div>

I think that you should use "should bool query" [Boolean query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)

Something like this:

```
POST /index/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "term": {
                "myboolean": false
              }
            },
            {
              "bool": {
                "must_not": {
                  "exists": {
                    "field": "myboolean"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

> it should just remove all results where `myboolean` is `true`

Don't you think you can simplify this with must not term filter myboolean:true?

---

<div class="post-metadata">

### Author: ![i333](https://avatars.discourse-cdn.com/v4/letter/i/8491ac/32.png) [@i333](https://discuss.elastic.co/u/i333)
#### Post date: [November 11, 2018, 9:41am UTC](https://discuss.elastic.co/t/return-all-results-where-field-is-missing-or-false/156162/3 "2018-11-11T09:41:43Z")

</div>

> [@mats990](#):
>
> Don't you think you can simplify this with must not term filter myboolean:true?

That sounds like a great idea, could you show me how I could add that to this new query? Would I need to add a new object to the `filter` array?

```
{
	"query": {
		"bool": {
			"must": {
				"multi_match": {
					<some content here>
				}
			},
			"filter": [
				{ "must_not": { "term": { "myboolean": true } } },
				{ "term": { "otherboolean": false } },
				{ "range": { "myfield": { gte: 50 } } }
			]
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![mats990](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mats990/32/37516_2.png) [@mats990](https://discuss.elastic.co/u/mats990)
#### Post date: [November 11, 2018, 9:55am UTC](https://discuss.elastic.co/t/return-all-results-where-field-is-missing-or-false/156162/4 "2018-11-11T09:55:19Z")

</div>

You almost got it, the only thing you should add is another bool inside a filter. This should work:

```
POST index/_search
{
  "_source": "myboolean", 
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must_not":{
            "term":{
              "myboolean": true
            }
          }
        }
      }
    }
  }
}

```

I just tested it and my results look like this:

```
"hits": [
      {
        "_index": index",
        "_type": "_doc",
        "_id": "1",
        "_score": 0,
        "_source": {
          "myboolean": false
        }
      },
      {
        "_index": "index",
        "_type": "_doc",
        "_id": "2",
        "_score": 0,
        "_source": {}
      }]
```

---

<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: [December 9, 2018, 9:55am UTC](https://discuss.elastic.co/t/return-all-results-where-field-is-missing-or-false/156162/5 "2018-12-09T09:55:24Z")

</div>

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