Elasticsearch DSL Query Syntax Overview (Version 7.x)
This article provides a comprehensive beginner-friendly guide to Elasticsearch 7.x DSL query syntax, covering core keywords, mapping types, query examples, boolean logic, and code snippets to help readers understand and construct effective search queries.
The article introduces Elasticsearch DSL (Domain Specific Language) query syntax for version 7.x, aimed at beginners who find the nested JSON statements confusing and need a clear summary for learning and memorization.
It notes that official documentation often refers to the older 2.x version, while many keywords have changed or been removed in newer releases; the guide follows the 7.x syntax.
Top‑level keywords include:
query : analogous to SQL's SELECT.
aggs : analogous to SQL's GROUP BY, used for aggregations such as averages or max values.
highlight : highlights matching terms in the result, e.g., highlighting "中华人民共和国万岁".
Search‑related keywords include:
sort : sorts results, similar to ORDER BY.
from and size : paginate results, similar to LIMIT.
post_filter : applies a filter after aggregations.
The guide explains how field types affect indexing: keyword fields keep the original value (e.g., "America"), while text fields are lower‑cased and tokenized (e.g., "Rain" becomes "rain").
Key query clauses are described with examples:
{
"query": {
"match": {
"name": "比尔盖茨"
}
}
} {
"query": {
"match_phrase": {
"country": "中国"
}
}
} {
"query": {
"term": {
"country": "America"
}
}
} {
"query": {
"terms": {
"country": ["America", "america"]
}
}
} {
"query": {
"exists": {
"field": "country"
}
}
} {
"query": {
"range": {
"age": {
"lte": 50,
"gte": 20
}
}
}
} {
"query": {
"ids": {
"values": ["JJpNJHgBLLjdyTtc34ag", "JZp5JHgBLLjdyTtcEobD"]
}
}
} {
"query": {
"fuzzy": {
"name": {
"value": "fain",
"fuzziness": 1,
"prefix_length": 0,
"max_expansions": 50,
"transpositions": true
}
}
}
}For compound queries, the article focuses on the bool query, which can nest leaf queries and uses four clauses:
must : documents must satisfy the clause and contribute to scoring.
must_not : documents must not satisfy the clause.
should : documents may satisfy the clause; at least one should clause is required if no must/filter is present.
filter : documents must satisfy the clause but it does not affect scoring; filters are cached for better performance.
The article concludes by encouraging readers to experiment with these queries, join the community, and explore further Elasticsearch features.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.