# Missing field query like FilterBuilders.missingFilter()

**URL:** https://discuss.elastic.co/t/missing-field-query-like-filterbuilders-missingfilter/11058
**Category:** Elasticsearch
**Created:** [March 7, 2013, 2:59pm UTC](https://discuss.elastic.co/t/missing-field-query-like-filterbuilders-missingfilter/11058 "2013-03-07T14:59:11Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Greg\_4](https://avatars.discourse-cdn.com/v4/letter/g/e99b99/32.png) [@Greg\_4](https://discuss.elastic.co/u/Greg_4)
#### Post date: [March 7, 2013, 2:59pm UTC](https://discuss.elastic.co/t/missing-field-query-like-filterbuilders-missingfilter/11058/1 "2013-03-07T14:59:11Z")

</div>

Hi,

is it possible to create a query for not existing field like missingFilter.

I use bool query with should clause to boost matching queries.

Example:

```
   "bool" : {
        "should" : [ {
          "terms" : {
            "devices" : ["samsung"],
            "boost" : 5.0
          }
        }, {
          "terms" : {
            "devices" : ["htc"],
            "boost" : 10.0
          }
        }
        } ],
        "minimum_should_match" : "1"
      }

```

If "devices"-field does not exist in document, document should match  
nevertheless.

Thanks.  
Greg.

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [March 8, 2013, 12:06pm UTC](https://discuss.elastic.co/t/missing-field-query-like-filterbuilders-missingfilter/11058/2 "2013-03-08T12:06:25Z")

</div>

Hey

a possibility is to add a filtered query - containing an not + exists  
filter - to your should clauses from the bool query like this:

{  
"filtered" : {  
"query" : { "match\_all" : {} },  
"filter" : {  
"not" : {  
"exists" : { "field" : "devices" }  
}  
}  
}  
}

See here for more:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

When doing it this way, check for the scores of the matching document to  
make sure you get the desired results.

--Alex

On Thu, Mar 7, 2013 at 3:59 PM, Greg [linuxzwerg@gmail.com](mailto:linuxzwerg@gmail.com) wrote:

> Hi,
> 
> is it possible to create a query for not existing field like missingFilter.
> 
> I use bool query with should clause to boost matching queries.
> 
> Example:
> 
> ```
> "bool" : {
> "should" : [ {
> "terms" : {
> "devices" : ["samsung"],
> "boost" : 5.0
> }
> }, {
> "terms" : {
> "devices" : ["htc"],
> "boost" : 10.0
> }
> }
> } ],
> "minimum_should_match" : "1"
> }
> 
> ```
> 
> If "devices"-field does not exist in document, document should match  
> nevertheless.
> 
> Thanks.  
> Greg.
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![Greg\_4](https://avatars.discourse-cdn.com/v4/letter/g/e99b99/32.png) [@Greg\_4](https://discuss.elastic.co/u/Greg_4)
#### Post date: [March 9, 2013, 11:12am UTC](https://discuss.elastic.co/t/missing-field-query-like-filterbuilders-missingfilter/11058/3 "2013-03-09T11:12:41Z")

</div>

Thanks, I have not come up with this idea 🙂

Greg.

On Friday, March 8, 2013 1:06:25 PM UTC+1, Alexander Reelsen wrote:

> Hey
> 
> a possibility is to add a filtered query - containing an not + exists  
> filter - to your should clauses from the bool query like this:
> 
> {  
> "filtered" : {  
> "query" : { "match\_all" : {} },  
> "filter" : {  
> "not" : {  
> "exists" : { "field" : "devices" }  
> }  
> }  
> }  
> }
> 
> See here for more:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/filtered-query.html)  
> When doing it this way, check for the scores of the matching document to  
> make sure you get the desired results.
> 
> --Alex
> 
> On Thu, Mar 7, 2013 at 3:59 PM, Greg \<[linux...@gmail.com](mailto:linux...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi,
> > 
> > is it possible to create a query for not existing field like  
> > missingFilter.
> > 
> > I use bool query with should clause to boost matching queries.
> > 
> > Example:
> > 
> > ```
> > "bool" : {
> > "should" : [ {
> > "terms" : {
> > "devices" : ["samsung"],
> > "boost" : 5.0
> > }
> > }, {
> > "terms" : {
> > "devices" : ["htc"],
> > "boost" : 10.0
> > }
> > }
> > } ],
> > "minimum_should_match" : "1"
> > }
> > 
> > ```
> > 
> > If "devices"-field does not exist in document, document should match  
> > nevertheless.
> > 
> > Thanks.  
> > Greg.
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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 6, 2017, 2:47am UTC](https://discuss.elastic.co/t/missing-field-query-like-filterbuilders-missingfilter/11058/4 "2017-07-06T02:47:23Z")

</div>


