Elasticsearch · Databasesintermediate
Elasticsearch bool Query
An Elasticsearch Query DSL request combining full-text match, a term filter and a date range, with sorting and pagination — the everyday search shape.
elasticsearchsearchquery-dsldatabasebool
Preview
{
"from": 0,
"size": 20,
"query": {
"bool": {
"must": [
{
"match": {
"title": "wireless headphones"
}
}
],
"filter": [
{
"term": {
"category": "electronics"
}
},
{
"range": {
"created_at": {
"gte": "now-30d/d",
"lte": "now/d"
}
}
}
]
}
},
"sort": [
{
"_score": "desc"
},
{
"created_at": "desc"
}
],
"_source": [
"id",
"title",
"category",
"price",
"created_at"
]
}AI actions
Documentation
Purpose
Retrieve documents that match a search term, are constrained by filters, and come back sorted and paginated.
When to use
Powering a search box or filtered list view over an Elasticsearch/OpenSearch index.
Required fields
- query — the top-level query clause (here a bool query)
- bool.must / filter / should — how sub-clauses combine
Optional fields
- sort — result ordering
- from / size — pagination window
- _source — which fields to return
Best practices
- Put exact-match constraints in filter (cached, no scoring), text in must.
- Prefer keyword fields for term filters, text fields for match.
- Cap size and use search_after for deep pagination.
Security considerations
- Validate and bound user input — huge from/size values can exhaust the cluster.
- Never build query JSON via string concatenation of raw user input.