API search and filtering
easy 25 minA list API lets the caller narrow, order and search.
See it run
Twelve products: narrow them, order them, find them by words, and refuse questions that are not allowed.
Operations
Query builder
Add one filter after another and watch the list shrink. Filters are joined by AND.
Step 1 of 7
No filters
| product | category | price | rating | stock |
|---|---|---|---|---|
| 1 Wireless Mouse Pro | electronics | 49.99 | 4.5 | yes |
| 2 USB-C Hub | electronics | 79.99 | 4.8 | yes |
| 3 Keyboard RGB | electronics | 149.99 | 4.7 | no |
| 4 Laptop Stand | accessories | 59.99 | 4.3 | yes |
| 5 Webcam HD | electronics | 89.99 | 4.2 | yes |
| 6 Desk Organizer | accessories | 34.99 | 4.0 | yes |
| 7 Monitor Light Bar | lighting | 69.99 | 4.6 | yes |
| 8 Ergonomic Chair | furniture | 399.99 | 4.9 | no |
| 9 Cable Kit | accessories | 19.99 | 3.8 | yes |
| 10 NC Headphones | electronics | 299.99 | 4.7 | yes |
| 11 Desk Mat XXL | accessories | 29.99 | 4.4 | yes |
| 12 Smart Power Strip | electronics | 44.99 | 4.1 | yes |
12 of 12
Every product is returned.
Step 1: No filters
GET /products ?category=electronics &price_gte=50 &price_lte=200 &in_stock=true &sort=-rating &fields=id,name,price- Finds by
- Field values
- Index
- Composite
- Download all and filter in the app
- 12 of 12 sent
The answer also carries pagination. Each extra filter can only make the list shorter.
In the real world
The same api search and filtering doing real jobs. Pick one and step through it.
Words, filters and fresh prices in one page.
Step 1 of 4
The question
wireless, under 100, four stars or more.
Step 1: The question
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Pasting the sort field into SQL
criticalIt is text from a stranger inside a statement.
Wrongsql += f" ORDER BY {args['sort']}"Rightcolumn = SORTABLE.get(args["sort"])if column is None: raise BadRequest("sort")sql += f" ORDER BY {column}, id"Ignoring unknown parameters
majorprise_lt=50 returns everything and nobody notices.
Wrongfor f in KNOWN: if f in args: apply(f)Rightunknown = set(args) - KNOWNif unknown: raise BadRequest(unknown)LIKE with a wildcard in front
majorA normal index cannot help, so every row is read.
WrongWHERE name ILIKE '%wireless%'RightWHERE search @@ websearch_to_tsquery('english', $1)-- or a trigram index for substringsWriting to database and index together
majorWhen one write fails they differ, and nothing repairs it.
Wrongdb.save(p)elastic.index(p)Rightdb.save(p) # commit with an outbox row# the indexer reads changes and repeats until done
Interview tips
How to explain it
- Filters compare fields, search compares words.
- Names from our list, values as parameters.
- The index is a copy that follows the database.
Memory trick: SAFE
- Search with q, Array with in.
- Filter with gt, gte, lt, lte, eq.
- Evaluate the order with sort.
Stay in the database when
- Only exact fields are asked.
- The text is short and small.
- Results must be exactly current.
Add a search engine when
- People type words with mistakes.
- Counts per filter are shown.
- Ranking matters to the business.