# Using dis\_max query in a filter

**URL:** <https://discuss.elastic.co/t/using-dis-max-query-in-a-filter/88870>\
**Category:** Elasticsearch\
**Created:** [June 9, 2017, 5:05pm UTC](https://discuss.elastic.co/t/using-dis-max-query-in-a-filter/88870 "2017-06-09T17:05:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevenmwall](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/stevenmwall/32/12199_2.png) [@stevenmwall](https://discuss.elastic.co/u/stevenmwall)\
**Post date:** [June 9, 2017, 5:05pm UTC](https://discuss.elastic.co/t/using-dis-max-query-in-a-filter/88870/1 "2017-06-09T17:05:55Z")

</div>

I have documents in my Elasticsearch index like tihs:

> ```
> {
> "id": 1,
> "text": "My document text",
> "field1": "value1",
> "field2": "value2"
> }
> 
> ```

I need to support:

1. A user entered query string that should search the "text" field. (No problem)
2. Unaware to the user, I need to add a filter that essentially says: "WHERE field1=value1 OR field2=value2".

This second requirement is causing me problems. In my boolean query, I am trying to add a filter query that consists of a dis\_max query. So like this:

> ```
> {
> "query": {
> "bool": {
> "filter": [
> "constant_score": {
> "filter": {
> "dis_max": {
> "tie_breaker": 0.0,
> "queries": [
> {
> "match": {
> "field1": {
> "query": "value1",
> "operator": "OR",
> "prefix_length": 0,
> "max_expansions": 50,
> "fuzzy_transpositions": true,
> "lenient": false,
> "zero_terms_query": "NONE",
> "boost": 1.0
> }
> }
> },
> {
> "match": {
> "field2": {
> "query": "value2",
> "operator": "OR",
> "prefix_length": 0,
> "max_expansions": 50,
> "fuzzy_transpositions": true,
> "lenient": false,
> "zero_terms_query": "NONE",
> "boost": 1.0
> }
> }
> }
> ]
> }
> }
> }
> ] //Filters array
> }
> }
> }
> 
> ```

But it appears I am getting the dis\_max query AND'ing those two queries together instead of OR'ing so I do not get enough results.

Is using a dis\_max query in a filter the proper way to achieve this type of behavior?

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [June 9, 2017, 7:49pm UTC](https://discuss.elastic.co/t/using-dis-max-query-in-a-filter/88870/2 "2017-06-09T19:49:40Z")

</div>

The use of dis max does not make sense in a filter context because dis max  
is a compound query that only returns the score of the highest scoring  
clause. But since you are filtering, you have no need for scores. I would  
use a bool query, where both match queries are part of the "should" portion.

---

<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 7, 2017, 7:49pm UTC](https://discuss.elastic.co/t/using-dis-max-query-in-a-filter/88870/3 "2017-07-07T19:49:50Z")

</div>

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