Skip to main content

🗓️ 16042024 1107
📎 #elasticsearch #wip

es_query_dsl

Compound Query Clauses

  • Use:
    • For combining clauses
    • Alter behavior (e.g. constant_score)
  • Wraps leaf / other compound clauses
  • bool, dis_max
  • must - logical AND
  • must_not - logical NOT
  • should - logical OR

Logical "AND" Conditions

  • must or filter
    • filter does not affect score
  • 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 == value1 and field2 == value2

Logical "OR" Conditions

  • use should clause
  • At least one conditions in should satisfied > document match
    • Can control this number with minimum_should_match
{
"query": {
"bool": {
"should": [
{ "term": { "field1": "value1" } },
{ "term": { "field2": "value2" } }
],
"minimum_should_match": 1
}
}

match documents where field1 == value1 OR field2 == 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 == value1 AND
    • field4 == value4 AND
    • field2 == value2 || field3 == value3

Logical NOT

  • must_not

Leaf Query Clauses

es_leaf_query_clauses.png

match

term

range

exists

prefix

ids

fuzzy


References