Overview
This article details practical usage of Elasticsearch Filter DSL, covering filter query, sort pagination, highlight display and batch operations.
Filter DSL
Basic Concepts
Filter DSL is Elasticsearch’s filter query language, different from query:
- Doesn’t participate in relevance scoring: Only filters documents that meet criteria
- Faster execution: No score calculation needed, can be cached in memory
- Applicable scenarios: Log analysis, data classification and other scenarios that don’t need match degree calculation
Usage Example
POST /book/_search
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"price": { "gte": 200, "lte": 1000 }
}
}
}
}
}
Query Sort
Default Relevance Sort
POST /book/_search
{
"query": { "match": {"description":"solr"} }
}
Field Value Sort
POST /book/_search
{
"query": { "match_all": {} },
"sort": [{ "price": { "order": "desc" } }]
}
Multi-level Sort
POST /book/_search
{
"query": { "match_all": {} },
"sort": [
{ "price": { "order": "desc" } },
{ "timestamp": { "order": "desc" } }
]
}
Pagination Query
POST /book/_search
{
"query": { "match_all": {} },
"sort": [{ "price": { "order": "desc" } }],
"size": 2,
"from": 0
}
Result Highlight
POST /book/_search
{
"query": { "match": { "name": "elasticsearch" } },
"highlight": {
"pre_tags": "<font color='pink'>",
"post_tags": "</font>",
"fields": [{ "name": {} }]
}
}
Batch Operations
Batch Query (_mget)
POST /_mget
{
"docs": [
{ "_index": "book", "_id": 1 },
{ "_index": "book", "_id": 2 }
]
}
Batch Query Same Index
POST /book/_mget
{
"docs": [{ "_id": 2 }, { "_id": 3 }]
}
Batch Add/Delete/Update (_bulk)
POST /_bulk
{ "delete": { "_index": "book", "_id": "1" }}
{ "create": { "_index": "book", "_id": "5" }}
{ "name": "test14", "price": 100.99 }
{ "update": { "_index": "book", "_id": "2" }}
{ "doc": { "name": "test" } }
Notes
- Bulk request size: A few thousand operations per request, size a few MB
- Best practice: 10k-50k documents, about 5-15MB, default not exceed 100MB
Common Error Quick Reference
| Issue | Solution |
|---|---|
| Filter seems not working | Check field type, ensure numeric/date fields use range |
| Sort not as expected | Use .keyword for text field sorting |
| Deep pagination timeout | Consider using search_after/scroll instead of from/size |
| Highlight no output | Ensure highlight.fields matches query field |
| bulk returns 400 | Check JSON format, ensure action line and data line strictly paired |