>_ IzzyLab

Hands-on task

This task is step 17 of the course “Redis in production”

Searching hashes: the Query Engine

medium~15 min

Redis holds products — hashes product:1 … product:24 with the fields name (the title), category (a single category tag) and price (an integer).

  1. Create an index idx:products over hashes with the prefix product: and the schema: name — TEXT, category — TAG, price — NUMERIC.
  2. Using the index, find all products in the category exactly books with a price no more than 500 (500 inclusive), sorted by price ascending.
  3. Write the keys of the products found to /root/books.txt — one per line, in sort order, with no other lines.
What you'll practise:
  • redis
  • Query Engine
  • RediSearch
  • FT.CREATE
  • FT.SEARCH
  • FT.INFO
  • TAG
  • NUMERIC

Topic breakdown

The Query Engine (formerly RediSearch, built into Redis 8) builds secondary indexes over hashes or JSON documents with a given key prefix and then maintains them itself on every write. Without an index, finding "all books cheaper than 500" is only possible with a full SCAN that reads every key. The field type determines how you search it: TEXT — full-text search with stemming, TAG — exact tag match (categories, statuses, ids), NUMERIC — ranges and sorting. A wrong type (category as TEXT) gives "similar" matches instead of exact ones. Mind pagination too: FT.SEARCH returns the first 10 results by default.

What's checked

  1. index idx:products over product: hashes with the right schema
  2. all 24 products in the index
  3. /root/books.txt has the right keys in the right order

What it looks like