elasticsearch bool query combine must with OR

  • OR is spelled should
  • AND is spelled must
  • NOR is spelled should_not

Example:

You want to see all the items that are (round AND (red OR blue)):

    {
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {"shape": "round"}
                    },
                    {
                        "bool": {
                            "should": [
                                {"term": {"color": "red"}},
                                {"term": {"color": "blue"}
                            ]
                        }
                    }
                ]
            }
        }
    }

You can also do more complex versions of OR, for example if you want to match at least 3 out of 5, you can specify 5 options under “should” and set a “minimum_should” of 3.

Thanks to Glen Thompson and Sebastialonso for finding where my nesting wasn’t quite right before.

Thanks also to Fatmajk for pointing out that “term” becomes “match” in ElasticSearch 6.

Leave a Comment