# Exists vs missing performance

**URL:** https://discuss.elastic.co/t/exists-vs-missing-performance/54420
**Category:** Elasticsearch
**Created:** [June 30, 2016, 2:41pm UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420 "2016-06-30T14:41:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![israel](https://avatars.discourse-cdn.com/v4/letter/i/b77776/32.png) [@israel](https://discuss.elastic.co/u/israel)
#### Post date: [June 30, 2016, 2:41pm UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420/1 "2016-06-30T14:41:34Z")

</div>

Hi,

Is there a difference, in terms of performance, between exists query/filter and missing query/filter?

Thanks,

Israel

---

<div class="post-metadata">

### Author: ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)
#### Post date: [June 30, 2016, 5:38pm UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420/2 "2016-06-30T17:38:03Z")

</div>

In general, elasticsearch prefers positive queries, which it can execute more efficiently thanks to its inverted index. So `exists` queries perform better than `missing` queries just like elasticsearch can more quickly find all documents that contain `foobar` than documents that do NOT contain it. Also see [https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-exists-query.html#\_literal\_missing\_literal\_query](https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-exists-query.html#_literal_missing_literal_query), you should replace your usage of `missing` queries with `exists` queries in `MUST_NOT` clauses.

---

<div class="post-metadata">

### Author: ![israel](https://avatars.discourse-cdn.com/v4/letter/i/b77776/32.png) [@israel](https://discuss.elastic.co/u/israel)
#### Post date: [July 3, 2016, 8:22am UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420/3 "2016-07-03T08:22:48Z")

</div>

thanks!!

---

<div class="post-metadata">

### Author: ![frankshad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frankshad/32/11031_2.png) [@frankshad](https://discuss.elastic.co/u/frankshad)
#### Post date: [July 22, 2016, 11:43am UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420/4 "2016-07-22T11:43:42Z")

</div>

My common use case here is "match if missing or false" (or maybe "match if missing or 0"), looking for the opposite of {"example": true}. This is quite easy to write as  
{"bool": {"should":{"missing": {"field": "example"}, "term": {"example": false}}}}  
(maybe with a minimum\_should\_match if other conditions are there as well), but that's now deprecated. The alternative with must\_not and exists seems to require nested bools to achieve and is much more complicated to express. Or am I missing something?

---

<div class="post-metadata">

### Author: ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)
#### Post date: [July 22, 2016, 12:42pm UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420/5 "2016-07-22T12:42:09Z")

</div>

You aren't missing anything. The query language is designed to mimic the  
underlying implementation so you don't have any surprises. It can be  
verbose because of it. I wasn't around when that decision was made but I  
expect it is an example of a place where a complete abstraction could very  
easily hide performance issues - so a less abstract DSL.

Could you do `{"bool": {"must_not": [{"term": "example": true}]}}`?

---

<div class="post-metadata">

### Author: ![frankshad](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/frankshad/32/11031_2.png) [@frankshad](https://discuss.elastic.co/u/frankshad)
#### Post date: [July 22, 2016, 12:52pm UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420/6 "2016-07-22T12:52:21Z")

</div>

I could indeed, thank you, it gives exactly the same result in the case I am looking at. I had kind-of assumed to be not-equal it also had to exist.

---

<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: [July 5, 2017, 10:33pm UTC](https://discuss.elastic.co/t/exists-vs-missing-performance/54420/7 "2017-07-05T22:33:24Z")

</div>


