🗓️ 16042024 1107
📎 #elasticsearch #wip
Compound Query Clauses
- Use:
- For combining clauses
 - Alter behavior (e.g. 
constant_score) 
 - Wraps leaf / other compound clauses
 bool,dis_maxmust- logical ANDmust_not- logical NOTshould- logical OR
Logical "AND" Conditions
mustorfilterfilterdoes not affectscore
- All conditions specified must be satisfied within these clauses for a document to match
 
{
  "query": {
    "bool": {
      "must": [
        { "term": { "field1": "value1" } },
        { "term": { "field2": "value2" } }
      ]
    }
}
{
  "query": {
    "bool": {
      "filter": [
        { "term": { "field1": "value1" } },
        { "term": { "field2": "value2" } }
      ]
    }
 }
match documents where
field1 == value1andfield2 == value2
Logical "OR" Conditions
- use 
shouldclause - At least one conditions in should satisfied > document match
- Can control this number with 
minimum_should_match 
 - Can control this number with 
 
{
  "query": {
    "bool": {
      "should": [
        { "term": { "field1": "value1" } },
        { "term": { "field2": "value2" } }
      ],
      "minimum_should_match": 1
    }
}
match documents where
field1 == value1ORfield2 == value2
Combining "AND" and "OR" Conditions
{
  "query": {
    "bool": {
      "must": [{ "term": { "field1": "value1" } }],
      "should": [
        { "term": { "field2": "value2" } },
        { "term": { "field3": "value3" } }
      ],
      "filter": [{ "term": { "field4": "value4" } }],
      "minimum_should_match": 1
    }
  }
}
- Match documents where
field1 == value1ANDfield4 == value4ANDfield2 == value2||field3 == value3
 
Logical NOT
must_not
Leaf Query Clauses
