Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Minimum should match on filtered query

Is it possible to have a query like this

  "query": {
  "filtered": {
     "filter": {
        "terms": {
           "names": [
             "Anna",
             "Mark",
             "Joe"
           ],
           "execution" : "and"
        }
      }
    }
  } 

With the "minimum_should_match": "2" statement?

I know that I can use a simple query (I've tried, it works) but I don't need the score to be computed. My goal is just to filter documents which contains 2 of the values.

Does the score generally heavily impact the time needed to retrieves document?

Using this query:

  "query": {
  "filtered": {
     "filter": {
        "terms": {
           "names": [
             "Anna",
             "Mark",
             "Joe"
           ],
           "execution" : "and",
           "minimum_should_match": "2"
        }
      }
    }
  } 

I got this error:

QueryParsingException[[my_db] [terms] filter does not support [minimum_should_match]]
like image 403
betto86 Avatar asked Sep 28 '15 13:09

betto86


1 Answers

Minimum should match is not a parameter for the terms filter. If that is the functionality you are looking for, I might rewrite your query like this, to use the bool query wrapped in a query filter:

{
   "filter": {
      "query": {
         "bool": {
            "should": [
               {
                  "term": {
                     "names": "Anna"
                  }
               },
               {
                  "term": {
                     "names": "Mark"
                  }
               },
               {
                  "term": {
                     "name": "Joe"
                  }
               }
            ],
            "minimum_should_match": 2
         }
      }
   }
}

You will get documents matching preferably exactly all three, but the query will also match document with exactly two of the three terms. The must is an implicit and. We also do not compute score, as we have executed the query as a filter.

like image 191
IanGabes Avatar answered Oct 15 '22 19:10

IanGabes