AI Hints
Home›Elasticsearch Tutorial›Phrases and Many Fields
Elasticsearch · Lesson 25 of 120

AND or OR

Operator and minimum_should_match.

⏱ 2 min read▶ Video at 22:55🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

By default, match uses OR. Any of the words is enough. Sometimes that's too loose. A customer who types waterproof boots doesn't want every waterproof tent.

  • Default: OR
  • Too loose
  • Need all words?

operator: and

Here we use the longer form of match, with the query and an operator. Operator and means every word must be present. Run it. Only products with both waterproof and jacket in the description match.

Dev Tools Consoletext
GET products/_search?filter_path=hits.total.value,hits.hits._source.name
{
  "query": {
    "match": {
      "description": { "query": "waterproof jacket", "operator": "and" }
    }
  }
}
Output
{
  "hits": {
    "total": {"value": 2},
    "hits": [
      {"_source": {"name": "Breeze Rain Jacket"}},
      {"_source": {"name": "Classic Fleece Jacket"}}
    ]
  }
}
Also uses _load
products

minimum_should_match

Between any word and all words, there's minimum should match. With three words, two is a good middle ground. Run it. Products with at least two of the three words match.

Dev Tools Consoletext
GET products/_search?filter_path=hits.total.value,hits.hits._source.name
{
  "query": {
    "match": {
      "description": {
        "query": "light warm jacket",
        "minimum_should_match": 2
      }
    }
  }
}
Output
{
  "hits": {
    "total": {"value": 5},
    "hits": [
      {"_source": {"name": "Rechargeable 600 Headlamp"}},
      {"_source": {"name": "Storm Rain Jacket"}},
      {"_source": {"name": "Hooded Down Jacket"}},
      {"_source": {"name": "Mini 200 Headlamp"}},
      {"_source": {"name": "LED Camp Lantern"}}
    ]
  }
}
Also uses _load
products

Summary

Match uses OR by default. Operator and needs every word, and minimum should match sets how many words must match. Next, matching words in order.

Key takeaways
  • OR by default
  • operator: and
  • minimum_should_match
Next up: match_phrase — words in order