Skip to main content

Query Understanding, Filters, and Facets

A search request mixes free text with structured constraints. Query understanding interprets the text; filters decide eligibility with yes/no rules; facets summarize the current result set so a visitor can refine it.

Prerequisites​

Understand BM25 scoring, catalog fields, and basic aggregation.

Learning goals​

  • Separate scored query clauses from exact filter clauses.
  • Make normalization and fallback behavior observable.
  • Compute facets in the current query and filter context.
  • Avoid leaking sensitive or misleading counts through facets.

Request contract​

Parse known structured controls through a schema, not by concatenating user text into a backend query. Normalize only where the meaning is stable. Preserve the raw request for debugging under an approved retention policy, and log the interpreted query version separately.

Filters answer whether an item matches; they do not add relevance evidence. Facet buckets count matching documents by a field. Decide whether each facet is computed before or after its own selected filter, because the two interfaces answer different questions. Zero-result recovery may relax spelling or optional filters, but it must not silently relax safety, availability, access, or legal restrictions.

Worked example​

The training query q1 declares an exact category filter. The example applies it before scoring and reports facets over the filtered candidates.

import csv
from collections import Counter

base = "tests/fixtures/recommender-systems"
with open(f"{base}/search-catalog.csv", encoding="utf-8", newline="") as handle:
catalog = list(csv.DictReader(handle))
with open(f"{base}/search-queries.csv", encoding="utf-8", newline="") as handle:
query = next(row for row in csv.DictReader(handle) if row["query_id"] == "q1")

candidates = [item for item in catalog if item["category"] == query["category_filter"]]
facets = Counter(item["provider"] for item in candidates)
print(f"raw_query={query['query']!r} category_filter={query['category_filter']!r}")
print(f"candidate_ids={[item['item_id'] for item in candidates]} provider_facets={dict(sorted(facets.items()))}")

Expected output:

raw_query='python preprocessing' category_filter='machine-learning'
candidate_ids=['s1'] provider_facets={'core': 1}

Facet counts are not recommendations and do not imply category quality. High counts may merely reflect catalog supply. For user-dependent permissions, compute counts only over items that the requester is allowed to discover.

Limitations​

  • Intent classification and entity extraction can fail on short, multilingual, or ambiguous queries.
  • Synonyms can raise recall while introducing harmful false matches.
  • Facets can reveal small sensitive groups or inaccessible inventory.
  • Applying a filter after top-k retrieval can produce an empty or biased list; define stage order explicitly.

Practice​

Design a typed request for query, category, publication date, and availability. Mark which constraints may be relaxed on zero results and which must remain hard.

Next steps​

Learning to rank uses judged query-item features to order a retrieved candidate set.

Primary sources​

Checked 2026-10-17: Elastic, query and filter context; Elastic, bucket aggregations; Apache Lucene, query parser syntax.