Master Elasticsearch Queries: From match_all to Fuzzy Search

This article introduces Elasticsearch's powerful Query DSL and walks through common query types—including match_all, term, terms, range, prefix, wildcard, regexp, bool, and fuzzy queries—providing clear explanations and complete JSON request examples for each.

Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Mike Chen's Internet Architecture
Master Elasticsearch Queries: From match_all to Fuzzy Search

Elasticsearch Overview

Elasticsearch provides a powerful way to retrieve data using Query DSL, a JSON‑based query language accessed via RESTful endpoints.

Elasticsearch Query Syntax

GET /<index_name>/_search {
  // JSON request body
}

Common Elasticsearch Queries

1. Match All Query

Retrieves all documents without any conditions.

GET /mikechen/_search {
  "query": {
    "match_all": {}
  }
}

2. Term Query

Exact match on a single term; the query term is not analyzed.

GET /mikechen/_search {
  "query": {
    "term": {
      "title": "love China"
    }
  }
}

3. Terms Query

Matches any of the specified terms, similar to SQL IN.

{
  "query": {
    "terms": {
      "keyword": ["mikechen", "互联网架构"]
    }
  }
}

4. Range Query

Searches for documents where a field falls within a specified range.

{
  "query": {
    "range": {
      "age": {
        "gte": 18,
        "lt": 30
      }
    }
  }
}

5. Prefix Query

Finds documents whose field values start with a given prefix.

GET /user/_search {
  "query": {
    "prefix": {
      "name": "mikechen"
    }
  }
}

6. Wildcard Query

Uses * for zero or more characters and ? for a single character.

GET /user/_search {
  "query": {
    "wildcard": {
      "name": "mikechen*"
    }
  }
}

7. Regexp Query

Applies a regular expression to match field values.

POST /mikechen/book/_search {
  "query": {
    "regexp": {
      "authors": "t[a-z]*y"
    }
  }
}

8. Bool (Compound) Query

Combines multiple queries using four clauses: must, should, must_not, and filter.

GET /books/_search {
  "query": {
    "bool": {
      "must": [
        { "term": { "name": { "value": "java" } } }
      ],
      "should": [
        { "match": { "info": "mikechen互联网架构" } }
      ]
    }
  }
}

9. Fuzzy Query

Handles misspellings by allowing a specified edit distance.

GET /blogs_index/_search {
  "query": {
    "fuzzy": {
      "author": {
        "value": "方财兄",
        "fuzziness": 1,
        "prefix_length": 1,
        "max_expansions": 100
      }
    }
  }
}

These examples demonstrate how to construct a variety of Elasticsearch queries to meet different search requirements.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Elasticsearchfuzzy-searchQuery DSLFull‑Text SearchBool query
Mike Chen's Internet Architecture
Written by

Mike Chen's Internet Architecture

Over ten years of BAT architecture experience, shared generously!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.