Search code examples
elasticsearchchewy-gem

Elastic Search blank `should` returning results


I'm running a should query with any empty list against an index. I'm expecting it to return 0 results since there are no should queries/filters to match against:

(Syntax is Chewy but pretty close to regular ES)

OrganizationsIndex.filter(
   bool: {
     must: [
       {
         bool: {
           should: [],
    minimum_should_match: 1
         }
       }
     ]
   })

However, it returns all the documents in the index. Is that expected behavior? Is there a way that I could make should: [] always return 0 documents?


Solution

  • This behaviour was changed in 1.3.3 version of Elasticsearch. Before, the empty should clause was treated as no match, and afterwards as it was mentioned by @Val it became match_all query.

    From the discussion on the issue:

    When a query with just a bool {} is run on its own, the empty bool clause in this case does not throw a NPE and is treated as a valid query, except that it returns no documents (when it should really be returning a match_all):

    The link to this GitHub issue - https://github.com/elastic/elasticsearch/issues/7240