BoolQueryDsl
Type-safe DSL for building Elasticsearch boolean queries with typed occurrences.
Boolean queries combine multiple query clauses using boolean logic. This DSL provides a fluent API for composing queries within the four typed occurrences of a bool query: must, mustNot, should, and filter.
Query Occurrences
See the bool query documentation.
must: Documents
MUSTmatch these queries (affects scoring)mustNot: Documents
MUST NOTmatch these queries (filters only)should: Documents
SHOULDmatch these queries (affects scoring, optional)filter: Documents
MUSTmatch these queries (does not affect scoring)
Usage Example
val document = Metamodels.product
BoolQuery.of {
it.boolQueryDsl {
// Must match: affects scoring
must + {
document.title match "laptop"
document.status term Status.ACTIVE
}
// Filter: must match but doesn't affect scoring
filter + {
document.price.range(Range.closed(500.0, 2000.0))
document.inStock term true
}
// Should match: boosts score if matched
should + {
document.brand term "Dell"
document.tags.containsTerms("featured", "sale")
}
// Must not match: excludes documents
mustNot + {
document.category term "refurbished"
}
}
}Operator Syntax
The DSL supports an operator-based syntax using the + operator:
bool {
must + {
document.title match "search term"
}
filter + {
document.status term Status.ACTIVE
}
}See also
for available query types
Types
Properties
Functions
Adds queries to the filter occurrence.
Convenience overload for the common integer form of minimum_should_match.
Sets the minimum_should_match parameter on the underlying BoolQuery.Builder.
Adds queries to the must occurrence.
Adds queries to the must_not occurrence.
Operator syntax for adding queries to the filter occurrence.
Operator syntax for adding queries to the must occurrence.
Operator syntax for adding queries to the must_not occurrence.
Operator syntax for adding queries to the should occurrence.
Adds queries to the should occurrence.