Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Documents and Leaf Queries  ›  Capstone Project

Range, Exists, IDs, Prefix and Wildcard

Range Query 15 minRange compares values between bounds, exists checks a field has a value, ids fetches by document ID, and prefix and wildcard match patterns on keyword fields.

Range, Exists, IDs, Prefix and Wildcard

Match and term cover most searches, but the bookshop also asks "books between 10 and 20", "books missing a price", "books 1 and 4", and "authors starting with Mi". Each has its own small leaf query, and each is easy once you know what it compares against.

The rule

Range compares, exists checks presence, ids fetches by key, prefix and wildcard match patterns.

Range — numbers, dates and keywords between bounds, with gt, gte, lt, lte:

{ "query": { "range": { "price": { "gte": 10, "lte": 20 } } } }
{ "query": { "range": { "published": { "gte": "2020-01-01", "lt": "now" } } } }

Exists — documents that have at least one indexed, non-null value in the field:

{ "query": { "exists": { "field": "price" } } }

A document where price is missing, null or an empty array [] does not match. Wrap it in must_not (next section) to find the ones without a price.

IDs — fetch documents by their _id:

{ "query": { "ids": { "values": [ "1", "4" ] } } }

Prefix — keyword values that start with the given characters, case-sensitive:

{ "query": { "prefix": { "author": "Mi" } } }

Wildcard — a pattern where * means any run of characters (including none) and ? means exactly one character:

{ "query": { "wildcard": { "isbn": "978-1-23?-*" } } }

Two warnings. Prefix and wildcard do not analyse your input, so on a text field they test single stored words, not the whole string; aim them at keyword fields. And a pattern that starts with * forces Elasticsearch to check every term in the field — slow on big indices.

The shortcut

Turn the English into one of five shapes: "between" → range, "has a" → exists, "these exact records" → ids, "starts with" → prefix, "looks like" → wildcard.

Four requests from the managerStep 1 of 4
  1. "Books from 10 to 20, both included"

    "between" → range with gte: 10 and lte: 20.

Drills

Answer fast — trust the rule.

quick drill

Which range finds books priced over 10 but strictly under 20?

quick drill

Which document does {"exists": {"field": "price"}} match?

quick drill

What does the wildcard pattern Mi?a* match on a keyword field?

quick drill

Why is {"wildcard": {"author": "*Das"}} a concern on a large index?

Remember

  • Range compares, exists checks presence, ids fetches by key, prefix and wildcard match patterns.
  • between → range, has a → exists, exact records → ids, starts with → prefix, looks like → wildcard.
  • Aim prefix and wildcard at keyword fields and avoid a leading *.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainFive shapes

For each request, name the leaf query and write its key settings: books priced 10 to 20 inclusive; books with an isbn value; books 1 and 4; authors starting with Ana. Then explain why *Das is a poor wildcard pattern.