# Using missing and exists fliter together in single query

**URL:** <https://discuss.elastic.co/t/using-missing-and-exists-fliter-together-in-single-query/11549>\
**Category:** Elasticsearch\
**Created:** [April 11, 2013, 12:18pm UTC](https://discuss.elastic.co/t/using-missing-and-exists-fliter-together-in-single-query/11549 "2013-04-11T12:18:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![shyamr](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@shyamr](https://discuss.elastic.co/u/shyamr)\
**Post date:** [April 11, 2013, 12:18pm UTC](https://discuss.elastic.co/t/using-missing-and-exists-fliter-together-in-single-query/11549/1 "2013-04-11T12:18:13Z")

</div>

> <https://stackoverflow.com/questions/15948566/using-missing-and-exists-filter-together-in-single-query>

I am trying to get a query which can return a json which satisfies both  
conditions of a filtering. I am trying to get a response which has feed  
containing one field named "test1" and it should also be missing a field  
"test2", Ive tried the query

```
{
 "query": {
  "filtered": {
  "query": {
    "match_all": {}
   },
      "filter": {
         "missing": {
        "field": "test2"
       },
       "exists": {
         "field": "test1"
       }
      }
    }
  } 
}

```

The above query returns all the fields which has the field "test1" and it  
also returns feeds which are missing field "test2", I am trying to narrow  
down the response as I only want feeds satisfying both conditions. It would  
be appreciated if anyone could tell me what my mistake is..

--  
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:** [April 11, 2013, 1:18pm UTC](https://discuss.elastic.co/t/using-missing-and-exists-fliter-together-in-single-query/11549/2 "2013-04-11T13:18:02Z")

</div>

Hey,

can you try this and see if it works for you

```
    "filtered" : {
        "query" : { "match_all" : {} },
        "filter" : {
            "and": [
                { "missing" : { "field" : "c" } },
                { "exists" : { "field" : "a" } }
            ]
        }
    }

```

--Alex

On Thu, Apr 11, 2013 at 2:18 PM, [shyamr@algotree.com](mailto:shyamr@algotree.com) wrote:

> [elasticsearch - Using missing and exists filter together in single query - Stack Overflow](http://stackoverflow.com/questions/15948566/using-missing-and-exists-fliter-together-in-single-query)
> 
> I am trying to get a query which can return a json which satisfies both  
> conditions of a filtering. I am trying to get a response which has feed  
> containing one field named "test1" and it should also be missing a field  
> "test2", Ive tried the query
> 
> ```
> {
> "query": {
> "filtered": {
> "query": {
> "match_all": {}
> },
> "filter": {
> "missing": {
> "field": "test2"
> },
> "exists": {
> "field": "test1"
> }
> }
> }
> }
> }
> 
> ```
> 
> The above query returns all the fields which has the field "test1" and it  
> also returns feeds which are missing field "test2", I am trying to narrow  
> down the response as I only want feeds satisfying both conditions. It would  
> be appreciated if anyone could tell me what my mistake is..
> 
> --  
> 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:** ![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:41am UTC](https://discuss.elastic.co/t/using-missing-and-exists-fliter-together-in-single-query/11549/3 "2017-07-06T02:41:35Z")

</div>


