|

Elasticsearch Queries – Part 2: Practical Query Types

In Part 1 of this series I walked through the foundations of Elasticsearch queries: the mental model, why mapping is your best friend, and how to choose between filters and matches. Now it’s time to roll up our sleeves and look at some of the practical query types that you’ll actually use when building real-world search systems.

If Part 1 was about thinking like Elasticsearch, this post is about talking its language.


Phrase Queries (match_phrase)

One of the most common pitfalls for beginners is not realizing that a simple match query doesn’t guarantee word order.

For example:

  • match: "machine learning engineer" could return results like:
    • “Learning paths for engineers in machine design”
    • “Engineers working on learning algorithms for machines”

But if your user is typing a job title, they probably expect exact word order. That’s when match_phrase comes in:

{
  "query": {
    "match_phrase": {
      "title": "machine learning engineer"
    }
  }
}

This ensures you only get results where “machine learning engineer” appears as a phrase. Think of it as the difference between a grocery list of words vs. a precisely written recipe.


Range Queries (Numbers, Prices, Metrics)

Elasticsearch isn’t just about text. Range queries let you filter numeric fields like prices, ratings, or even metrics from logs.

For example, if you’re building an e-commerce filter:

{
  "query": {
    "range": {
      "price": {
        "gte": 100,
        "lte": 500
      }
    }
  }
}

This returns products priced between $100 and $500.

Remember: Range queries work just as well on dates and metrics. That means you can:

  • Find logs within the last 24 hours.
  • Select products with a review score above 4.5.
  • Fetch employees with experience between 3 and 7 years.

Here is another example for time ranges:

{
  "query": {
    "range": {
      "timestamp": {
        "gte": "now-24h"
      }
    }
  }
}


Prefix & Wildcard Queries

Sometimes users don’t remember the full keyword. Prefix queries allow you to search for documents starting with a given string.

{
  "query": {
    "prefix": { "username": "sha" }
  }
}

This will match shayan, shahrzad, or shahram. Wildcards go further:

{
  "query": {
    "wildcard": { "username": "sha*" }
  }
}

Note: wildcards and prefixes can hurt performance because they require Elasticsearch to scan more terms. Use them only for autocomplete-like use cases, and consider using edge n-grams at index time for faster performance.


Multi-Match Queries

What if a user searches “Shayan” and you want to check both name and username fields? Instead of writing two queries, you can use multi_match:

{
  "query": {
    "multi_match": {
      "query": "Shayan",
      "fields": ["name", "username"]
    }
  }
}

This is especially powerful for building search bars across multiple fields—titles, descriptions, tags, categories—all in a single query.


Exists Queries

Sometimes what’s missing is just as important as what’s there.

  • Want to filter only users who have an email address?
  • Or products that don’t yet have a discount set?

That’s where exists shines:

{
  "query": {
    "exists": {
      "field": "email"
    }
  }
}

Combine this with a must_not clause to find users without email.


IDs Query

The most underrated query type in Elasticsearch is also the fastest: ids.

If you already know the document IDs you want to fetch (e.g., from a recommendation system or external service), this query avoids the overhead of full-text search and fetches them directly:

{
  "query": {
    "ids": {
      "values": ["1", "2", "3"]
    }
  }
}

This is a hidden gem for hybrid workflows, like combining Elasticsearch with machine learning pipelines.


Wrapping Up

These query types—phrase, range, prefix/wildcard, multi-match, exists, and IDs—form the toolbox of any serious Elasticsearch practitioner.

They let you move beyond toy examples and into real-world use cases:

  • Building job search engines.
  • Powering e-commerce filters.
  • Handling messy user input with prefixes and wildcards.
  • Creating hybrid AI + search workflows.

In the next part of this series, I’ll go deeper into aggregations and scoring—how Elasticsearch not only finds documents but also ranks and summarizes them.

Until then, try mixing these queries into your own projects. You’ll quickly see that search isn’t just about finding—it’s about understanding what users really mean.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *