Sub topic 9
Designing search queries: filters, facets, and pagination
This chapter assumes you know how an inverted index and BM25 scoring work, and that you have an index mapping with text fields for full-text search and keyword fields for exact matches. The examples use Elasticsearch-style syntax, and the ideas carry over to other engines.
Separate scoring from restricting
A search query does two jobs. It ranks documents by relevance, and it restricts which documents are eligible. Mixing them causes most query design mistakes.
- Put text relevance clauses in
must. They contribute to the score. - Put exact conditions in
filter. They include or exclude documents and do not change the score.
GET /products/_search
{
"query": {
"bool": {
"must": [ { "match": { "name": "running shoes" } } ],
"filter": [
{ "term": { "category": "footwear" } },
{ "range": { "price": { "lte": 100 } } }
]
}
},
"size": 10
}Filters are cheaper than scored clauses because the engine skips scoring and can cache the matching document sets. A price range should not change how relevant a shoe is to "running shoes", so it belongs in filter.
A common error is running term against a text field. The field was analyzed at index time, so "Footwear" is stored as "footwear" and the exact string will not match. Use term on keyword fields and match on text fields.
Facets
A facet is a count of matching documents per value, such as "footwear (42), apparel (17)". Search pages use facets to show filter choices with result counts. The engine computes them with aggregations over keyword or numeric fields.
GET /products/_search
{
"query": { "match": { "name": "running" } },
"size": 10,
"aggs": {
"by_category": { "terms": { "field": "category" } }
}
}Two points matter when you design facets:
- Facet fields must be
keyword, numeric, or date fields, so decide this in the mapping. - Counts on a term aggregation can be approximate when an index has several shards, because each shard returns only its top values.
When a user selects a facet value, the usual pattern applies it with post_filter. The hits narrow to the selection, while the facet counts still show the other categories the user could switch to.
Pagination
The simplest pagination uses from and size. Page 3 with 10 results per page is from: 20, size: 10. The cost is the problem. To return page N, each shard must collect and sort from + size documents, and the coordinating node merges them. Deep pages become slow and memory-heavy, and engines cap them (Elasticsearch defaults to 10,000 results).
| Method | How it works | Good for | Weakness |
|---|---|---|---|
| `from` / `size` | Skips `from` results | Shallow page numbers | Cost grows with depth, capped |
| `search_after` | Continues after the last sort values seen | Infinite scroll, "next" buttons | No jump to page N |
| Point in time + `search_after` | Freezes a view of the index | Consistent deep paging | Holds resources until closed |
search_after needs a deterministic sort. Add a unique tiebreaker, such as the document ID, or documents with equal scores can repeat or vanish between pages.
Practical limits
Most users never go past the first few pages, so limit the page depth you offer. Cap size on the server, and do not pass a client-supplied value straight to the engine. Return only the fields the page needs, and fetch full records from the primary database by ID if required.
Exercise
A search page shows "running shoes" results with a category filter and price cap. Which clauses go in must and which in filter, and why? Also, why should a search_after sort include the document ID?
Show answer
The match on name goes in must because it should affect relevance. The category term and price range go in filter because they only include or exclude documents, which skips scoring and allows caching. The document ID acts as a unique tiebreaker. Without it, documents with identical sort values have no stable order, so they can repeat or be skipped across pages.