Scrivio Interview library
ConceptFoundations6 min read · updated 13 November 2026

The Redis query engine adds secondary indexes over hashes and JSON documents, so you can search by text, tags, numeric ranges and vectors instead of only looking up keys. It ships with Redis 8 and Redis Stack.

Show how to create an index over product JSON documents and query them by text, tag and price range, and explain how the index stays up to date.

We create the index once with FT.CREATE idx:product ON JSON PREFIX 1 product: SCHEMA $.name AS name TEXT $.brand AS brand TAG $.price AS price NUMERIC SORTABLE, and query it with FT.SEARCH using @name for full text, @brand:{acme} for exact tags and @price:[50 120] for a range. The query engine updates the index on every write, change, expiry and delete of a matching key, so a document is searchable as soon as its write returns.

Why this question is asked

For years the rule in Redis was simple: you find data by its key and nothing else. Anything else, such as all products from one brand under 100 dollars, meant maintaining your own sets and sorted sets by hand. The Redis query engine, built into Redis 8 and available in Redis Stack, removes that work by keeping secondary indexes for us. Interviewers ask about it because it changes how we model data in Redis and because the field types have real cost differences. In this answer, we will store a few product documents, create the index, run a combined query, see how the index stays current, compare TEXT and TAG fields, and look at the costs.

1A Quick Recap: Documents, Prefixes and Indexes

A JSON document in Redis is a key whose value is a JSON tree, written with JSON.SET. An index in the query engine is a separate structure that watches keys whose names start with a given prefix and records chosen fields from them. The index does not store the documents; it stores what is needed to find their keys quickly, and then reads the documents to return results.

Each indexed field gets a type. TEXT means full-text search: the value is split into words (tokenised), common endings are removed (stemming, so running also matches run), and queries match words. TAG means exact values, such as a brand or category. NUMERIC means ranges. SORTABLE keeps a copy of the value ready for fast sorting.

2Creating the Index and Adding Documents

We create the index once. From then on, any key starting with product: is indexed automatically, including keys that already existed, which the engine scans in the background.

Java
FT.CREATE idx:product ON JSON PREFIX 1 product: SCHEMA
$.name AS name TEXT
$.brand AS brand TAG
$.price AS price NUMERIC SORTABLE -- OK
JSON.SET product:1 $ '{"name":"Trail running shoes","brand":"Acme","price":89}' -- OK
JSON.SET product:2 $ '{"name":"Road running shoes","brand":"Zoom","price":110}' -- OK
JSON.SET product:3 $ '{"name":"Running shoes junior","brand":"Acme","price":45}' -- OK
JSON.SET product:4 $ '{"name":"Hiking boots","brand":"Acme","price":99}' -- OK

The JSONPath expressions ($.name and so on) say where each field lives in the document, and AS gives it a short name to use in queries. PREFIX 1 product: means one prefix follows. If we stored products as hashes instead, we would write ON HASH and plain field names.

3Querying by Text, Tag and Price Together

Now we ask for running shoes from Acme between 50 and 120 dollars, cheapest first, first page of 20.

Java
FT.SEARCH idx:product '@name:(running shoes) @brand:{acme} @price:[50 120]'
SORTBY price ASC LIMIT 0 20
-- 1) (integer) 1 total matches
-- 2) "product:1" Trail running shoes, Acme, 89
-- 3) 1) "$" 2) "{\"name\":\"Trail running shoes\",...}"
Step throughHow FT.SEARCH narrows the products
  1. Text clause: name has running and shoesproducts 1, 2, 3 (stemmed words)
  2. Tag clause: brand is acmeproducts 1, 3
  3. Numeric clause: price 50 to 120product 1
  4. Sort by price, take offset 0 count 20returns product:1 with its JSON

4 steps in order. Play them, or step through them one at a time.

Each clause narrows the result, because a space between clauses means AND. product:2 fails the brand filter, product:3 fails the price range (45 is below 50), and product:4 fails the text clause because its name has neither word. Notice that @brand:{acme} matched Acme: tags are case-insensitive by default. LIMIT 0 20 is offset and count, so the second page is LIMIT 20 20, and the first number in the reply is the total count, which is what a page shows as 1 result.

For statistics rather than documents, FT.AGGREGATE adds grouping and reducers. FT.AGGREGATE idx:product '*' GROUPBY 1 @brand REDUCE COUNT 0 AS n returns each brand with its number of products, here Acme 3 and Zoom 1.

4How the Index Stays Up to Date

The engine updates the index as part of every write to a matching key. When JSON.SET changes product:1's price to 79, the numeric index is updated before the command returns, so the very next FT.SEARCH sees 79. When a key is deleted or its TTL expires, its entries are removed from the index. There is no refresh interval and no separate indexing job, which is a real difference from engines that make new documents visible only after a refresh.

Two details are worth knowing. Keys that existed before FT.CREATE are indexed by a background scan, and FT.INFO shows its progress in percent_indexed. And a document whose field does not match the declared type, such as a price stored as the string "cheap", is counted as an indexing failure in FT.INFO and is not searchable by that field.

5TEXT or TAG, and What Indexes Cost

A category or brand field is almost always a TAG. A TAG stores the whole value as one exact term, so filtering is a fast lookup with no tokenising or stemming, and a value such as Running Gear is matched only as that exact value. A TEXT field would split it into words, let running match a category called Running Gear and Trail Running alike, and spend memory on positions and frequencies we never use for a filter.

Indexes are not free. Each indexed field costs memory, often a noticeable fraction of the data itself for TEXT fields, and every write does extra work to update them. Support also differs between engines: Redis 8 ships the query engine, while Valkey offers search as a separate module whose features have been growing, so check exactly which field types and commands your deployment supports before designing around them.

Field types at a glance
TypeMatchesGood for
TEXTwords, stemmed, with relevance scoringnames, descriptions
TAGexact values, optionally several per fieldbrand, category, status, tenant
NUMERICranges, sortableprice, stock, dates as numbers
VECTORnearest neighbourssemantic search over embeddings

6Follow-up questions

Why choose TAG rather than TEXT for a category field?

Because a category is a value we filter on exactly, not prose we search in. TAG matching is cheaper in memory and time, has no stemming surprises, and treats a multi-word category as one value. TEXT would make a query for shoes also match a category called Shoe Care and would rank results by relevance, which a filter does not need.

Is the index replicated to replicas?

Replicas receive the same writes as the primary and update their own copy of the index from them, so a replica can answer FT.SEARCH queries. The index itself is in memory and, depending on the version, may be rebuilt from the data after a restart, so check how your version handles that.

How do I search JSON arrays such as tags on a product?

Index the array with a JSONPath that selects its elements, for example $.tags[*] AS tags TAG. Each element becomes a tag value of the document, so @tags:{waterproof} matches any product whose array contains waterproof.

7Conclusion

In this answer, we created a JSON index with FT.CREATE over the product: prefix, declared name as TEXT, brand as TAG and price as NUMERIC SORTABLE, and combined all three in one FT.SEARCH with sorting and paging. We saw that the index updates synchronously on every write, change, expiry and delete, that FT.AGGREGATE adds grouping, that TAG is the right type for exact filters, and that indexes cost memory and write time and differ between engines.

What to say in the interviewAbout 90 seconds aloud

Open with

I would create the index once with FT.CREATE idx:product ON JSON PREFIX 1 product: and a schema that maps JSON paths to fields: dollar dot name as name TEXT, dollar dot brand as brand TAG, and dollar dot price as price NUMERIC SORTABLE.

Then, if they want more

From then on every key starting with product: is indexed, and existing keys are picked up by a background scan.

To query, I run FT.SEARCH with at name running shoes for full text, at brand in curly braces acme for an exact tag, and at price 50 to 120 in square brackets for a range; clauses separated by spaces are combined with AND, and I add SORTBY price ASC and LIMIT 0 20 for paging.

TEXT fields are tokenised and stemmed, so running also matches run, while TAG fields match exact values and are much cheaper for filters like brand or category. For counts per brand I would use FT.AGGREGATE with GROUPBY and a COUNT reducer.

The index is updated as part of each write, change, expiry or delete, so a document is searchable as soon as its write returns. The trade-off is memory and slower writes for every indexed field, and support differs between Redis 8, Redis Stack and Valkey's search module, so I would check what our deployment offers.

About this answer

Last updated
13 November 2026
Versions it names
Redis 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Redis documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Show how to create an index over product JSON documents and query them by text, tag and...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/f67c16e742/