# Range query with should not working

**URL:** <https://discuss.elastic.co/t/range-query-with-should-not-working/83273>\
**Category:** Elasticsearch\
**Created:** [April 22, 2017, 11:40am UTC](https://discuss.elastic.co/t/range-query-with-should-not-working/83273 "2017-04-22T11:40:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tushar\_chevulkar](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tushar_chevulkar/32/46270_2.png) [@tushar\_chevulkar](https://discuss.elastic.co/u/tushar_chevulkar)\
**Post date:** [April 22, 2017, 11:40am UTC](https://discuss.elastic.co/t/range-query-with-should-not-working/83273/1 "2017-04-22T11:40:19Z")

</div>

I have a query whose purpose is to get results with should query and filter it via date range.  
In the below query only the range filter is working and ignoring the should query. I have no idea what am I doing wrong.

In mysql the query will be  
`select * form user where (id=1 OR id=2) AND join_date>xxxx`

Current elastic query

```
{
	"query": {
		"bool": {
			"should": [{
				"term": {
					"id": "1"
				}
			}, {
				"term": {
					"id": "2"
				}
			}]
		},
		"filter": {
			"range": {
				"join_date": {
					"gt": "now-10d"
				}
			}
		}
	}
}
```

---

<div class="post-metadata">

**Author:** ![shanec](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shanec/32/4004_2.png) [@shanec](https://discuss.elastic.co/u/shanec)\
**Post date:** [April 22, 2017, 3:25pm UTC](https://discuss.elastic.co/t/range-query-with-should-not-working/83273/2 "2017-04-22T15:25:11Z")

</div>

This is because you're combining `should` and `filter`, and it's treating the filter as a sort of "must" and then the "shoulds" as optional. See [Boolean query | Elasticsearch Guide [8.11] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) for more information, but the relevant bit is:

> 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. The minimum number of should clauses to match can be set using the minimum\_should\_match parameter.

Something like the following should work

```
{
	"query": {
		"bool": {
			"should": [
			  {
  				"term": {
  					"id": 1
  				}
			  },
			  {
				  "term": {
					  "id": 2
				  }
			  }
			],
			"minimum_should_match": 1,
  			"filter": {
				"range": {
  					"join_date": {
  						"gt": "now-10d"
  					}
  				}
  			}
		}
	}
}

```

---

<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:** [May 20, 2017, 3:27pm UTC](https://discuss.elastic.co/t/range-query-with-should-not-working/83273/3 "2017-05-20T15:27:15Z")

</div>

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