🗓️ 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 ANDmust_not
- logical NOTshould
- logical OR
Logical "AND" Conditions
must
orfilter
filter
does 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 == value1
andfield2 == value2
Logical "OR" Conditions
- use
should
clause - 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 == value1
ORfield2 == 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
ANDfield4 == value4
ANDfield2 == value2
||field3 == value3
Logical NOT
must_not