Thuta Learning
ExercisesData & Databasesbeginner

Exercise — Debug a Slow or Irrelevant Search Query

What you'll walk away with

  • Explain the core ideas behind Exercise — Debug a Slow or Irrelevant Search Query
  • Run the sample Elasticsearch query or code and verify its output
  • Apply the technique correctly to the Tutorial Platform and production scenarios

Build the mental model

Across this course, Lesson 6 (`match` vs `term`), Lesson 8 (`text` vs `keyword`), Lesson 10 (`must` vs `filter`), and Lesson 18 (leading wildcards, mapping explosion) all showed patterns that make a query look correct but behave wrong or slow — this exercise is not about reciting those patterns as theory again, but practicing the debugging skill of spotting the symptom yourself in a real broken query snippet. A systematic debugging approach breaks the query into its components (field type, clause type, sort/aggregation logic), first confirms the field's mapping type with `GET /index/_mapping`, runs `explain: true`/`profile: true` (Lessons 13 and 20) to verify actual behavior, and only then targets and fixes the root cause. Distinguish two symptom categories: "no results or wrong results" (a correctness bug, like using `match` on a `keyword` field — an analyzer mismatch) versus "results are right but slow" (a performance bug, like putting a binary criterion inside `must` — unnecessary scoring cost) — they can look similar but have completely different root causes and completely different fixes.

Connect it to a real scenario

Suppose a bug report says the Tutorial Platform's production search feature returns zero results when filtering "advanced tutorials" — first check `GET /tutorials/_mapping` to confirm `difficulty` is a `keyword` field, then discover the buggy query used `match: { difficulty: "Advanced" }` (capitalized) — an analyzed `match` where an analyzer-agnostic exact match should have been used on a `keyword` field, causing a case-sensitivity mismatch; the fix is switching to `term: { difficulty: "advanced" }` (lowercase, a `term` query). For another bug report — profiling the search API's p99 latency reveals a binary filter, `term: { status: "published" }`, sitting inside `must`; the fix is moving it to `filter`, letting Elasticsearch's built-in caching be reused effectively.

Try the working example

json
// Buggy query — filtering "advanced" difficulty tutorials returns zero results.
// difficulty is mapped as "keyword".
{
  "query": {
    "bool": {
      "must": [
        { "match": { "difficulty": "Advanced" } },
        { "term": { "status": "published" } }
      ]
    }
  }
}

// Your task: find both problems in this query and rewrite it correctly.
You should see
You can write the corrected query, fixing the difficulty filter to a correctly cased `term` query and moving the `status` filter into the `filter` clause.

5-minute try-it

After fixing the buggy query above, explain in one line each which change fixes correctness (results return again) and which fixes performance (caching becomes effective).

One important caution

Debugging a query by guessing from syntax alone without first confirming the field's mapping type via `GET /index/_mapping` — missing the analyzer-mismatch root cause.

Assuming "no results" (a correctness bug) and "slow results" (a performance bug) share one root cause and trying to fix both with one change — the two symptoms often have distinct causes that need isolating component by component.

Elasticsearch Guide — Query DSLElastic

Easy traps

  • Debugging a query by guessing from syntax alone without first confirming the field's mapping type via `GET /index/_mapping` — missing the analyzer-mismatch root cause.
  • Assuming "no results" (a correctness bug) and "slow results" (a performance bug) share one root cause and trying to fix both with one change — the two symptoms often have distinct causes that need isolating component by component.
  • Validate sample queries and requests on a local or test instance with recoverable data before applying them to production.

Exercise

After fixing the buggy query above, explain in one line each which change fixes correctness (results return again) and which fixes performance (caching becomes effective).

You'll know it worked when: You can write the corrected query, fixing the difficulty filter to a correctly cased `term` query and moving the `status` filter into the `filter` clause.

Exercise — Debug a Slow or Irrelevant Search Query | Thuta Learning