# AND-ing a 'must' and 'filter' query

**URL:** https://discuss.elastic.co/t/and-ing-a-must-and-filter-query/39105
**Category:** Elasticsearch
**Created:** [January 13, 2016, 1:06pm UTC](https://discuss.elastic.co/t/and-ing-a-must-and-filter-query/39105 "2016-01-13T13:06:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![spireofdreams](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@spireofdreams](https://discuss.elastic.co/u/spireofdreams)
#### Post date: [January 13, 2016, 1:06pm UTC](https://discuss.elastic.co/t/and-ing-a-must-and-filter-query/39105/1 "2016-01-13T13:06:09Z")

</div>

I'm trying to fetch data from an elasticsearch index when the following criteria are met.

1. When the stream\_id is an empty string (e.g. "")
2. When the source\_id is a specific string (e.g. "unknown\_source")

I can use the two following queries separately:  
For checking empty string:

```
`{
"query": {
    "filtered": {
        "filter": {
             "script": {
                 "script": "_source.stream_id.length() == 0"
             }
        }
    }
}
}`

```

For checking source\_id

```
`{
"query": { 
    "bool" : {
        "must" : {
            "term" : { "source_id" : "unknown_source" }
        }
    }
}
}`

```

But now I see the 'filtered' query is deprecated in ES version 2.0 and I'm using version 2.1. So, how do I AND these two conditions? I've looked into the filtered query page documentation and it says

> Use the bool query instead with a must clause for the query and a filter  
> clause for the filter.

So, then I was trying the following query but it's not working.

```
` {
"query": {
    "bool": {
        "must" : {
            "term" : { "source_id" : "unknown_source" }
       },
    "filter": {
        "script": {
            "script": "_source.stream_id.length() == 0"
        }
    }
}
}
}`

```

---

<div class="post-metadata">

### Author: ![spireofdreams](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@spireofdreams](https://discuss.elastic.co/u/spireofdreams)
#### Post date: [January 13, 2016, 1:38pm UTC](https://discuss.elastic.co/t/and-ing-a-must-and-filter-query/39105/2 "2016-01-13T13:38:08Z")

</div>

Okay, it's solved. please refer to [this](http://stackoverflow.com/questions/34767085/using-two-checks-in-elastic-search-query) answer.

---

<div class="post-metadata">

### Author: ![Daverino](https://avatars.discourse-cdn.com/v4/letter/d/e79b87/32.png) [@Daverino](https://discuss.elastic.co/u/Daverino)
#### Post date: [January 14, 2016, 4:51pm UTC](https://discuss.elastic.co/t/and-ing-a-must-and-filter-query/39105/3 "2016-01-14T16:51:48Z")

</div>

The syntax above looks correct and jives with the documentation. The bool query takes "must", "should", 'must\_not" and "filter." "filter" is treated as a "must" that has no scoring.

Can you explain what you mean by 'not working'? Does it cause an error or is it not working logically as you expect?

The answers on the linked page don't seem to be going in the right direction to me. They seem more convoluted than should be required here.

---

<div class="post-metadata">

### Author: ![spireofdreams](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@spireofdreams](https://discuss.elastic.co/u/spireofdreams)
#### Post date: [January 14, 2016, 6:20pm UTC](https://discuss.elastic.co/t/and-ing-a-must-and-filter-query/39105/4 "2016-01-14T18:20:25Z")

</div>

I was getting some parsing failures. The second answer in that link works fine for me.

---

<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, 11:24pm UTC](https://discuss.elastic.co/t/and-ing-a-must-and-filter-query/39105/5 "2017-07-05T23:24:25Z")

</div>


