# ElasticSearch 5.x should query vs filtering

**URL:** <https://discuss.elastic.co/t/elasticsearch-5-x-should-query-vs-filtering/71952>\
**Category:** Elasticsearch\
**Created:** [January 18, 2017, 8:32am UTC](https://discuss.elastic.co/t/elasticsearch-5-x-should-query-vs-filtering/71952 "2017-01-18T08:32:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blade\_Watcher](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@Blade\_Watcher](https://discuss.elastic.co/u/Blade_Watcher)\
**Post date:** [January 18, 2017, 8:32am UTC](https://discuss.elastic.co/t/elasticsearch-5-x-should-query-vs-filtering/71952/1 "2017-01-18T08:32:15Z")

</div>

I have a problem with results come from an ES query. Filtering a query gives some confusing results, and I don't know how to overcome of it. So:

> ```
> {
> "query":{
> "bool":{
> "should":[
> {
> "multi_match":
> {
> "query":"searchquery",
> "operator":"and",
> "fields":["Name", "Description", "Text"]
> }
> },
> {
> "bool":{
> "must": [
> {"terms": {"Id": [1, 2]}},
> {"term": {"_type": "Project"}}
> ]
> }
> }
> ]
> }
> }
> }
> 
> ```

What I want here is: get all records whose Name, or Description, or Text is match the query OR all Projects (it's a \_type in the mapping), whose Id 1, or 2. The results seems exactly what I expected, I got eg: 5 hits, 2 Contact \_type (matches "searchquery"), 1 Project (matches "searchquery") and 2 projects with Id 1, or 2. Good!

But I have some authorization, so not all users access all Modules (Projects have another field eg: ModuleId).

So I want to restrict the results to specific ModuleIds:

> ```
> {
> "query":{
> "bool":{
> "should":[
> {
> "multi_match":
> {
> "query":"searchquery",
> "operator":"and",
> "fields":["Name", "Description", "Text"]
> }
> },
> {
> "bool":{
> "must": [
> {"terms": {"Id": [1, 2]}},
> {"term": {"_type": "Project"}}
> ]
> }
> }
> ],
> "filter": [
> {"terms": {"ModuleId": [9, 5]}}
> ]
> }
> }
> }
> 
> ```

And boom! In the result I got all, whose match the above query and **all** Projects with ModuleId 9, or 5. What I want is to narrow the results, not broaden them. I've tried many combinations with should, must, filter(bool filter as well). I've also tried post\_filter which "magically" gives me the expected result, but I have some aggregations as well (not in this example), sot it is not an option.

---

<div class="post-metadata">

**Author:** ![mainec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mainec/32/5557_2.png) [@mainec](https://discuss.elastic.co/u/mainec)\
**Post date:** [January 18, 2017, 10:22am UTC](https://discuss.elastic.co/t/elasticsearch-5-x-should-query-vs-filtering/71952/2 "2017-01-18T10:22:15Z")

</div>

I think the easiest would be to add a `minimum_should_match` parameter set to `1` to your outer bool query.

Here's an explanation of the parameter:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-minimum-should-match.html)

The reason for the behaviour you are seeing is explained in more detail here which also has a hint towards the minimum\_should\_match docs:

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html)

"The clause (query) should appear in the matching document. In a boolean query with no must or filter clauses, one or more should clauses must match a document."

Hope this helps,  
Isabel

---

<div class="post-metadata">

**Author:** ![Blade\_Watcher](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@Blade\_Watcher](https://discuss.elastic.co/u/Blade_Watcher)\
**Post date:** [January 18, 2017, 1:38pm UTC](https://discuss.elastic.co/t/elasticsearch-5-x-should-query-vs-filtering/71952/3 "2017-01-18T13:38:44Z")

</div>

Works, like a charm, thank you!

---

<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:** [February 15, 2017, 1:38pm UTC](https://discuss.elastic.co/t/elasticsearch-5-x-should-query-vs-filtering/71952/4 "2017-02-15T13:38:52Z")

</div>

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