AlgoViz

API search and filtering

easy 25 min

A 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

productcategorypriceratingstock
1 Wireless Mouse Proelectronics49.994.5yes
2 USB-C Hubelectronics79.994.8yes
3 Keyboard RGBelectronics149.994.7no
4 Laptop Standaccessories59.994.3yes
5 Webcam HDelectronics89.994.2yes
6 Desk Organizeraccessories34.994.0yes
7 Monitor Light Barlighting69.994.6yes
8 Ergonomic Chairfurniture399.994.9no
9 Cable Kitaccessories19.993.8yes
10 NC Headphoneselectronics299.994.7yes
11 Desk Mat XXLaccessories29.994.4yes
12 Smart Power Stripelectronics44.994.1yes

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

Our systemWeb appwireless, < 100Products APIPostgreSQLElasticsearchIndexer

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

    critical

    It is text from a stranger inside a statement.

    Wrong
    sql += f" ORDER BY {args['sort']}"
    Right
    column = SORTABLE.get(args["sort"])if column is None: raise BadRequest("sort")sql += f" ORDER BY {column}, id"
  • Ignoring unknown parameters

    major

    prise_lt=50 returns everything and nobody notices.

    Wrong
    for f in KNOWN:    if f in args: apply(f)
    Right
    unknown = set(args) - KNOWNif unknown: raise BadRequest(unknown)
  • LIKE with a wildcard in front

    major

    A normal index cannot help, so every row is read.

    Wrong
    WHERE name ILIKE '%wireless%'
    Right
    WHERE search @@ websearch_to_tsquery('english', $1)-- or a trigram index for substrings
  • Writing to database and index together

    major

    When one write fails they differ, and nothing repairs it.

    Wrong
    db.save(p)elastic.index(p)
    Right
    db.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.