Redis JSON & Query Engine
Everything so far finds data by key. Many applications also need to store nested documents and to query them: "products under 20 € in the kitchen category, sorted by rating", or a full-text search for "ceramic mug". Redis 8 can do this with two capabilities that used to be separate modules (RedisJSON and RediSearch) and are now part of the full Redis Open Source distribution:
| Capability | Commands | What it adds |
|---|---|---|
| JSON data type | JSON.SET, JSON.GET, JSON.NUMINCRBY, JSON.ARRAPPEND, ... |
nested documents, updated in place by JSONPath |
| Redis Query Engine | FT.CREATE, FT.SEARCH, FT.AGGREGATE, ... |
secondary indexes over hashes and JSON: text, tags, numeric ranges, geo and vectors |
Note: These features ship with the official
redis:8Docker image and the Redis Open Source packages, but not with minimal builds such as the Alpine package behind this course's playground (or with some forks). On such a serverJSON.SETfails withERR unknown command. The sessions below are therefore shown as text; run them againstdocker run -p 6379:6379 redis:8to try them yourself.MODULE LISTandCOMMAND INFO JSON.SETtell you whether a server has them.
JSON documents
JSON.SET key path value stores a document; the root path is $. Paths select parts of the document for reading and in-place updates, so you never rewrite the whole thing:
redis> JSON.SET product:1 $ '{"name":"Ceramic mug","price":8.5,"tags":["kitchen","gift"],"stock":{"hanoi":12,"lyon":4}}'
OK
redis> JSON.GET product:1 $.name
"[\"Ceramic mug\"]"
redis> JSON.NUMINCRBY product:1 $.stock.hanoi -2
"[10]"
redis> JSON.ARRAPPEND product:1 $.tags '"sale"'
1) (integer) 3
redis> JSON.GET product:1 $.tags
"[[\"kitchen\",\"gift\",\"sale\"]]"
redis> JSON.TYPE product:1 $.price
1) "number"JSONPath results are arrays, because a path like $..price can match several places in one document.
Without the JSON type: strings and hashes
On any Redis server you can store a document as a JSON string: simple, but every change means reading, modifying and writing the whole value. For flat objects, a hash gives field-level updates natively:
SET product:1 "{\"name\":\"Ceramic mug\",\"price\":8.5,\"stock\":12}"
GET product:1
HSET product:2 name "Teapot" price 24 stock 3 category kitchen
HINCRBY product:2 stock -1
HMGET product:2 name stockredis> SET product:1 "{\"name\":\"Ceramic mug\",\"price\":8.5,\"stock\":12}"
OK
redis> GET product:1
"{\"name\":\"Ceramic mug\",\"price\":8.5,\"stock\":12}"
redis> HSET product:2 name "Teapot" price 24 stock 3 category kitchen
(integer) 4
redis> HINCRBY product:2 stock -1
(integer) 2
redis> HMGET product:2 name stock
1) "Teapot"
2) "2"
| Approach | Nested data | Update one field | Query by field |
|---|---|---|---|
| JSON string | yes | rewrite all | no |
| Hash | no (flat) | HSET, HINCRBY |
with the Query Engine |
| JSON type | yes | JSON.SET on a path |
with the Query Engine |
Creating an index
The Query Engine indexes keys by prefix: every hash (or JSON document) whose key starts with the prefix is indexed automatically when it is written, updated or deleted. You declare which fields to index and how:
redis> FT.CREATE idx:products ON HASH PREFIX 1 product: SCHEMA
name TEXT SORTABLE
category TAG
price NUMERIC SORTABLE
OK
redis> HSET product:1 name "Ceramic mug" category kitchen price 8.5
redis> HSET product:2 name "Glass teapot" category kitchen price 24
redis> HSET product:3 name "Travel mug" category outdoor price 15| Field type | Query syntax | Example |
|---|---|---|
TEXT |
words, prefixes, phrases | @name:mug, @name:tea* |
TAG |
exact values in braces | @category:{kitchen} |
NUMERIC |
ranges in brackets | @price:[10 20], @price:[(10 +inf] |
GEO |
radius | @location:[105.85 21.03 5 km] |
VECTOR |
KNN | *=>[KNN 5 @embedding $vec] |
Searching
redis> FT.SEARCH idx:products "@name:mug" RETURN 2 name price
1) (integer) 2
2) "product:1"
3) 1) "name"
2) "Ceramic mug"
3) "price"
4) "8.5"
4) "product:3"
5) 1) "name"
2) "Travel mug"
3) "price"
4) "15"
redis> FT.SEARCH idx:products "@category:{kitchen} @price:[10 +inf]" RETURN 1 name
1) (integer) 1
2) "product:2"
3) 1) "name"
2) "Glass teapot"
redis> FT.SEARCH idx:products "*" SORTBY price DESC LIMIT 0 1 RETURN 1 name
1) (integer) 3
2) "product:2"
3) 1) "name"
2) "Glass teapot"The first number is the total number of matches; then come key names and the requested fields. Conditions separated by a space are combined with AND, | means OR and - means NOT. FT.AGGREGATE adds grouping and reductions (GROUPBY @category REDUCE COUNT 0 AS n).
What an index does: a model
A text index is an inverted index: for every word, the set of documents that contain it. A numeric index keeps documents sorted by value. You can build a tiny version yourself with sets and sorted sets on any Redis server, which is what people did before the Query Engine existed:
HSET product:1 name "Ceramic mug" category kitchen price 8.5
SADD idx:word:ceramic product:1
SADD idx:word:mug product:1
SADD idx:category:kitchen product:1
ZADD idx:price 8.5 product:1
HSET product:2 name "Glass teapot" category kitchen price 24
SADD idx:word:glass product:2
SADD idx:word:teapot product:2
SADD idx:category:kitchen product:2
ZADD idx:price 24 product:2
HSET product:3 name "Travel mug" category outdoor price 15
SADD idx:word:travel product:3
SADD idx:word:mug product:3
SADD idx:category:outdoor product:3
ZADD idx:price 15 product:3
SCARD idx:word:mug
SINTER idx:word:mug idx:category:kitchen
ZRANGE idx:price 10 +inf BYSCOREredis> HSET product:1 name "Ceramic mug" category kitchen price 8.5
(integer) 3
redis> SADD idx:word:ceramic product:1
(integer) 1
redis> SADD idx:word:mug product:1
(integer) 1
redis> SADD idx:category:kitchen product:1
(integer) 1
redis> ZADD idx:price 8.5 product:1
(integer) 1
redis> HSET product:2 name "Glass teapot" category kitchen price 24
(integer) 3
redis> SADD idx:word:glass product:2
(integer) 1
redis> SADD idx:word:teapot product:2
(integer) 1
redis> SADD idx:category:kitchen product:2
(integer) 1
redis> ZADD idx:price 24 product:2
(integer) 1
redis> HSET product:3 name "Travel mug" category outdoor price 15
(integer) 3
redis> SADD idx:word:travel product:3
(integer) 1
redis> SADD idx:word:mug product:3
(integer) 1
redis> SADD idx:category:outdoor product:3
(integer) 1
redis> ZADD idx:price 15 product:3
(integer) 1
redis> SCARD idx:word:mug
(integer) 2
redis> SINTER idx:word:mug idx:category:kitchen
1) "product:1"
redis> ZRANGE idx:price 10 +inf BYSCORE
1) "product:3"
2) "product:2"
It works, but every write must update several index keys (ideally in a transaction), and combining a text match with a price range means extra steps in your program. The Query Engine does all of this inside Redis, consistently and with ranking, stemming, typo tolerance and pagination.
When to use it
- Product catalogues, user directories and admin screens that filter on several fields.
- Autocomplete and full-text search over moderate amounts of text.
- Vector search with metadata filters for AI applications (an alternative to vector sets when you also need text and numeric filters).
- When search is the product (huge corpora, complex relevance tuning), a dedicated engine such as Elasticsearch or OpenSearch may still be the better fit.
Test yourself
What does the $ path mean in JSON.SET product:1 $ '{...}'?
- A variable
- The root of the document
- The last element
- A price field
How does the Query Engine decide which keys to index?
- It indexes every key
- By the key prefix(es) given in FT.CREATE
- Only keys listed by FT.ADD
- Keys with a TTL
Which query matches kitchen products priced from 10 upwards?
@category:kitchen AND price > 10@category:{kitchen} @price:[10 +inf]SELECT * WHERE category = kitchen@price:{10}