Backend Development 4 min read

Advanced Elasticsearch Queries: Bool, Range, Fuzzy, Filter, and Sorting

This article introduces advanced Elasticsearch query techniques, covering boolean queries with must/must_not/should clauses, range and fuzzy queries, filter usage that does not affect scoring, and single- and multi-field sorting, each illustrated with concrete request examples.

Laravel Tech Community
Laravel Tech Community
Laravel Tech Community
Advanced Elasticsearch Queries: Bool, Range, Fuzzy, Filter, and Sorting

In this tutorial we extend basic Elasticsearch usage by demonstrating several advanced query types and how to combine them for more precise search results.

1. Boolean query (bool) combines other queries using must (AND), must_not (NOT) and should (OR) clauses.

GET /yoshop/_search
{
  "query": {
    "bool": {
      "must": { "match": { "title": "大米" }},
      "must_not": { "match": { "title": "电视" }},
      "should": { "match": { "title": "手机" }}
    }
  }
}

2. Range query (range) selects documents whose numeric or date fields fall within a specified interval.

GET /heima/_search
{
  "query": {
    "range": {
      "price": {
        "gte": 1000.0,
        "lt": 2800.00
      }
    }
  }
}

3. Fuzzy query (fuzzy) is a tolerant version of a term query that allows a limited edit distance (default up to 2) between the searched term and the indexed term.

GET /youshop/_search
{
  "query": {
    "fuzzy": {
      "title": "applo"
    }
  }
}

To control the allowed edit distance explicitly, use the fuzziness parameter:

GET /youshop/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "appla",
        "fuzziness": 1
      }
    }
  }
}

4. Filter applies criteria that restrict the result set without influencing the relevance score.

GET /youshop/_search
{
  "query": {
    "bool": {
      "must": { "match": { "title": "小米手机" }},
      "filter": {
        "range": { "price": { "gt": 3000, "lt": 3800.00 } }
      }
    }
  }
}

Filters can also be combined with additional bool conditions.

5. Sorting

Single‑field sorting orders results by a specific field using the order parameter.

GET /youshop/_search
{
  "query": { "match": { "title": "小米手机" } },
  "sort": [
    { "price": { "order": "desc" } }
  ]
}

Multi‑field sorting combines several fields, for example sorting first by price and then by _id :

GET /goods/_search
{
  "query": {
    "bool": {
      "must": { "match": { "title": "小米手机" }},
      "filter": { "range": { "price": { "gt": 2000, "lt": 3000 } } }
    }
  },
  "sort": [
    { "price": { "order": "desc" } },
    { "_id": { "order": "desc" } }
  ]
}

These examples illustrate how to build more powerful Elasticsearch queries by combining boolean logic, range constraints, fuzzy matching, filters, and sorting.

Elasticsearchfilteringsortingrange queryAdvanced QueryBool QueryFuzzy Query
Laravel Tech Community
Written by

Laravel Tech Community

Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.

0 followers
Reader feedback

How this landed with the community

login 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.