BoolQueryDsl

class BoolQueryDsl(builder: BoolQuery.Builder)

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 MUST match these queries (affects scoring)

  • mustNot: Documents MUST NOT match these queries (filters only)

  • should: Documents SHOULD match these queries (affects scoring, optional)

  • filter: Documents MUST match 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

Constructors

Link copied to clipboard
constructor(builder: BoolQuery.Builder)

Types

Link copied to clipboard
data object Filter
Link copied to clipboard
data object Must
Link copied to clipboard
data object MustNot
Link copied to clipboard
data object Should

Properties

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
fun filterDsl(block: QueryVariantDsl.() -> Unit)

Adds queries to the filter occurrence.

Link copied to clipboard

Convenience overload for the common integer form of minimum_should_match.

Sets the minimum_should_match parameter on the underlying BoolQuery.Builder.

Link copied to clipboard
fun mustDsl(block: QueryVariantDsl.() -> Unit)

Adds queries to the must occurrence.

Link copied to clipboard
fun mustNotDsl(block: QueryVariantDsl.() -> Unit)

Adds queries to the must_not occurrence.

Link copied to clipboard
operator fun BoolQueryDsl.Filter.plus(block: QueryVariantDsl.() -> Unit)

Operator syntax for adding queries to the filter occurrence.

operator fun BoolQueryDsl.Must.plus(block: QueryVariantDsl.() -> Unit)

Operator syntax for adding queries to the must occurrence.

operator fun BoolQueryDsl.MustNot.plus(block: QueryVariantDsl.() -> Unit)

Operator syntax for adding queries to the must_not occurrence.

operator fun BoolQueryDsl.Should.plus(block: QueryVariantDsl.() -> Unit)

Operator syntax for adding queries to the should occurrence.

Link copied to clipboard
fun shouldDsl(block: QueryVariantDsl.() -> Unit)

Adds queries to the should occurrence.