# Minimum should match is not working

**URL:** <https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528>\
**Category:** Elasticsearch\
**Created:** [August 2, 2019, 12:02pm UTC](https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528 "2019-08-02T12:02:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sballa](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@sballa](https://discuss.elastic.co/u/sballa)\
**Post date:** [August 2, 2019, 12:02pm UTC](https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528/1 "2019-08-02T12:02:25Z")

</div>

I have a query with an array of 'must' clauses. Two of those clauses have array of 'should' clauses in them as follows just providing pseuodocode here):

must: [  
{ clause 1 },  
{ clause 2 },  
{ bool:  
should: [  
{clause3},  
{clause4},  
{clause5}  
],  
"minimum\_should\_match" : "1"  
},  
{ bool:  
should: [  
{clause6},  
{clause7}  
],  
"minimum\_should\_match" : "1"  
}  
]

The requirement here is to build a series of AND conditions with inner ORs as below:  
clause1 AND clasue2 AND (clause3 OR clause4 OR clause5) AND (clause6 OR clause7)

I m using Java High Level Rest Client BoolQueryBuilder (version 7.1.1).  
When I set minimumShouldMatch(1), it builds it as a String value "minimum\_should\_match" : "1"(as shown above). My intention of providing it is to say that atleast one clause must match in each of the should clauses.

But, I m seeing some records in the results where both clause6 and clause7 do not match. Meaning these records just satisfy 'clause1 AND clasue2 AND (clause3 OR clause4 OR clause5)'

It looks like minimum should match set as 1 for each of the should clauses are not respected.

Can you please advise?

---

<div class="post-metadata">

**Author:** ![sballa](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@sballa](https://discuss.elastic.co/u/sballa)\
**Post date:** [August 2, 2019, 7:44pm UTC](https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528/2 "2019-08-02T19:44:55Z")

</div>

Can anyone help please? Is this an inherent bug?

---

<div class="post-metadata">

**Author:** ![wangqinghuan](https://avatars.discourse-cdn.com/v4/letter/w/d26b3c/32.png) [@wangqinghuan](https://discuss.elastic.co/u/wangqinghuan)\
**Post date:** [August 4, 2019, 7:50am UTC](https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528/3 "2019-08-04T07:50:12Z")

</div>

Could you provide some authentic sample data to reproduce as you said ?

---

<div class="post-metadata">

**Author:** ![sballa](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@sballa](https://discuss.elastic.co/u/sballa)\
**Post date:** [August 5, 2019, 1:45pm UTC](https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528/4 "2019-08-05T13:45:18Z")

</div>

I have simplified the several clauses and kept the problem clauses alone in my explanation above to give you the following details.  
Basically, in the following query, I m trying to identify documents whose 'code' are in the 5 values provided in 'terms' and they have either data matching category\_1 or category\_2 conditions.  
What we observe is that the date range queries for these categories are not respected well. Data returned are having documents that do not fall in the specified date ranges.

```
{
 "bool" : {
  "must" : [
   {
    "terms" : {
     "code" : [
      "xxxxxxxx1",
      "xxxxxxxx2",
      "xxxxxxxx3",
      "xxxxxxxx4",
      "xxxxxxxx5"
     ],
     "boost" : 1.0
    }
   },
   {
    "bool" : {
     "should" : [
      {
       "bool" : {
        "must" : [
         {
          "exists" : {
           "field" : "category_1.category_1_score",
           "boost" : 1.0
          }
         },
         {
          "range" : {
           "category_1.category_1_date" : {
            "from" : "2019-05-26T00:00:00.000Z",
            "to" : null,
            "include_lower" : true,
            "include_upper" : false,
            "time_zone" : "UTC",
            "boost" : 1.0
           }
          }
         },
         {
          "range" : {
           "category_1.category_1_date" : {
            "from" : null,
            "to" : "2019-07-25T00:00:00.000Z",
            "include_lower" : false,
            "include_upper" : true,
            "time_zone" : "UTC",
            "boost" : 1.0
           }
          }
         }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
       }
      },
      {
       "bool" : {
        "must" : [
         {
          "exists" : {
           "field" : "category_2.category_2_score",
           "boost" : 1.0
          }
         },
         {
          "range" : {
           "category_2.category_2_date" : {
            "from" : "2019-05-26T00:00:00.000Z",
            "to" : null,
            "include_lower" : true,
            "include_upper" : false,
            "time_zone" : "UTC",
            "boost" : 1.0
           }
          }
         },
         {
          "range" : {
           "category_2.category_2_date" : {
            "from" : null,
            "to" : "2019-07-25T00:00:00.000Z",
            "include_lower" : false,
            "include_upper" : true,
            "time_zone" : "UTC",
            "boost" : 1.0
           }
          }
         }
        ],
        "adjust_pure_negative" : true,
        "boost" : 1.0
       }
      }
     ],
     "adjust_pure_negative" : true,
     "minimum_should_match" : "1",
     "boost" : 1.0
    }
   }
  ],
  "adjust_pure_negative" : true,
  "boost" : 1.0
 }
}
```

---

<div class="post-metadata">

**Author:** ![sballa](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@sballa](https://discuss.elastic.co/u/sballa)\
**Post date:** [August 5, 2019, 8:11pm UTC](https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528/5 "2019-08-05T20:11:56Z")

</div>

We identified the issue in the way the query was being built.

After using the from() and to() methods in RangeQueryBuilder when both limits were available and using gte() or lte() only when one of the limits were available and not explicitly setting includeUpper() and includeLower(), the query returned the correct results.

If the documentation were a bit more explanatory about the application of each of the above methods, it would have been helpful and saved time in narrowing down the cause of the problem.

Please consider this thread as resolved.

---

<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:** [September 2, 2019, 8:12pm UTC](https://discuss.elastic.co/t/minimum-should-match-is-not-working/193528/6 "2019-09-02T20:12:02Z")

</div>

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