ElasticSearch Query Types Explained: 9 Essential Query Methods with Examples
This comprehensive guide covers nine essential ElasticSearch query types including match_all, term, terms, range, prefix, wildcard, regexp, bool compound queries, and fuzzy queries, with practical code examples for each method.
1. Query All Documents (match_all)
The match_all query retrieves all documents without any filtering conditions. It is the simplest query type.
Basic example querying all documents in the mikechen index:
GET mikechen/_search
{
"query": {
"match_all": {}
}
}To sort results by a specific field (e.g., balance descending) while still matching all documents:
GET mikechen/_search
{
"query": {
"match_all": {}
},
"sort": [
{
"balance": {
"order": "desc"
}
}
]
}2. Exact Match Query (term)
The term query performs exact matching — the search term is not analyzed or tokenized before matching. It matches a single exact value.
Example: find documents where the title field exactly equals "love China":
GET mikechen/_search
{
"query": {
"term": {
"title": "love China"
}
}
}This matches only the precise phrase "love China" as a single token.
3. Multiple Exact Match Query (terms)
The terms query is the plural form of term; it matches documents where the field contains any of the provided exact values — analogous to SQL's IN operator.
Example: match documents where the keyword field is either "mikechen" or "互联网架构":
{
"query": {
"terms": {
"keyword": ["mikechen", "互联网架构"]
}
}
}4. Range Query (range)
The range query selects documents with field values within a specified range. Supported operators: gt — greater than ( >) gte — greater than or equal ( >=) lt — less than ( <) lte — less than or equal ( <=)
Example: find documents where age is >= 18 and < 30:
{
"query": {
"range": {
"age": {
"gte": 18,
"lt": 30
}
}
}
}5. Prefix Query (prefix)
The prefix query returns documents where a field value starts with the specified prefix. The prefix is not analyzed.
Example: find users whose name begins with "mikechen":
GET user/_search
{
"query": {
"prefix": {
"name": "mikechen"
}
}
}6. Wildcard Query (wildcard)
The wildcard query matches documents using wildcard expressions. Supported wildcards: * — matches zero or more characters ? — matches exactly one character
Example with * (names starting with "mikechen"):
GET user/_search
{
"query": {
"wildcard": {
"name": "mikechen*"
}
}
}Example with ? (names like "mike?chen" where ? is any single character):
GET user/_search
{
"query": {
"wildcard": {
"name": "mike?chen"
}
}
}7. Regular Expression Query (regexp)
The regexp query allows full regular expression patterns for more complex matching than wildcards.
Example: match authors whose name starts with 't', ends with 'y', and has any lowercase letters in between (pattern t[a-z]*y):
POST mikechen/book/_search
{
"query": {
"regexp": {
"authors": "t[a-z]*y"
}
}
}8. Compound Query (bool)
The bool query combines multiple sub-queries using four logical clauses:
must — document must match all sub-queries (logical AND, contributes to score)
should — document may match; each match increases score (logical OR)
must_not — document must not match (logical NOT, excludes documents)
filter — like must but does not compute scores; used for pure filtering and cached for performance
Example: find books where name must contain "java" (exact term) and info should contain "mikechen互联网架构" (full-text match):
GET books/_search
{
"query": {
"bool": {
"must": [
{
"term": {
"name": "java"
}
}
],
"should": [
{
"match": {
"info": "mikechen互联网架构"
}
}
]
}
}
}9. Fuzzy Query (fuzzy)
The fuzzy query handles typos or approximate matches using edit distance (Levenshtein distance). It is useful for "did you mean" style searches.
Example: searching for author "方财兄" with fuzziness 1 will also match "方才兄" (one character difference).
GET blogs_index/_search
{
"query": {
"fuzzy": {
"author": {
"value": "方财兄",
"fuzziness": 1,
"prefix_length": 1,
"max_expansions": 100
}
}
}
}Parameters: fuzziness — maximum edit distance (here 1) prefix_length — number of initial characters that must match exactly (here 1) max_expansions — maximum number of terms to expand to (here 100)
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
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.
