Using dis_max query in a filter

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?

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.

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