0

I add 3 documents

POST test/_doc
{"value": 1}

POST test/_doc
{"value": 2}

POST test/_doc
{"value": 3}

then do the following query I expect to return all the 3 docs with documents matching should clause being ranked higher

GET /test/_search
{
  "query": {
    "bool": {
      "minimum_should_match": 0,
      "should": [
        {
          "range": {
            "value": {
              "gte": 2
            }
          }
        }
      ]
    }
  }
}

but instead i get only 2 docs (value 2,3) "minimum_should_match": 0, does not have any effect until i add the filter or must clause in the bool query like below,

GET /test/_search
{
  "query": {
    "bool": {
       "filter": [ { "match_all": { } } ],
      "should": [
        {
          "range": {
            "value": {
              "gte": 2
            }
          }
        }
      ]
    }
  }
}

What I want

in the bool query, either the must clause or filter clause is empty or filled, the should clause must not filter any documents BUT only participate in ranking, please share how can i achieve that, thanks

Kamboh
  • 155
  • 1
  • 12

1 Answers1

1

It's a little weird that minimum_should_match: 0 is not working with the should clause. This may be due to the documentation mentioned here

No matter what number the calculation arrives at, a value greater than the number of optional clauses, or a value less than 1 will never be used. (ie: no matter how low or how high the result of the calculation result is, the minimum number of required matches will never be lower than 1 or greater than the number of clauses.

There are two ways in which you can get all the documents in the result and using the should clause only for the ranking purpose

  1. Use must or filter clause with match_all query, which you already figured out as shown in the question above.

  2. Another way could be to use the should clause with the boost parameter

Search Query:

{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "value": {
              "gte": 2,
              "boost": 2.0
            }
          }
        },
        {
          "range": {
            "value": {
              "lt": 2,
              "boost": 1.0
            }
          }
        }
      ]
    }
  }
}
   

Search Result will be

"hits": [
      {
        "_index": "68040640",
        "_type": "_doc",
        "_id": "2",
        "_score": 2.0,
        "_source": {
          "value": 2
        }
      },
      {
        "_index": "68040640",
        "_type": "_doc",
        "_id": "3",
        "_score": 2.0,
        "_source": {
          "value": 3
        }
      },
      {
        "_index": "68040640",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "value": 1
        }
      }
    ]
ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • @Kamboh did you get a chance to go through the answer, looking forward to get feedback from you :-) – ESCoder Jun 20 '21 at 09:58