JSON, Search and the Built-in Modules
What Redis 8 ships that Redis 6 did not.
Everything so far has been core Redis. This chapter is the modules — which, since Redis 8, are no longer something you install.
What is loaded
A stock redis:8-alpine container already has five:
redis-cli MODULE LIST | grep -A1 '^name$' | grep -v 'name\|--' | sortJSON documents
JSON.SET takes a path and a value. $ is the root:
JSON.SET u $ '{"name":"ada","age":36}'
JSON.GET u
TYPE uTYPE answers ReJSON-RL, not string — a real type stored as a parsed tree. Which is
what makes this possible, one field at a time:
JSON.SET u $ '{"name":"ada","age":36}'
JSON.SET u $.age 37
JSON.GET uPaths
Dotted for objects, bracketed for arrays:
JSON.SET o $ '{"user":{"city":"London"},"tags":["a","b"]}'
JSON.GET o $.user.city
JSON.GET o $.tags[0]
JSON.GET o $.tagsEditing in place
Typed operations that read and write in one atomic step:
JSON.SET s $ '{"views":10,"tags":["a"]}'
JSON.NUMINCRBY s $.views 5
JSON.ARRAPPEND s $.tags '"b"'
JSON.GET sJSON.NUMINCRBY is chapter 3’s INCR inside a document. JSON.DEL removes a path, and
JSON.TYPE reports what is there:
JSON.SET d $ '{"n":"ab","x":1}'
JSON.STRAPPEND d $.n '"cd"'
JSON.DEL d $.x
JSON.GET d
JSON.TYPE d $.nThree ways to store an object
| One field read | One field write | Queryable | Expiry | |
|---|---|---|---|---|
| String of JSON | whole value | whole value | no | per key |
| Hash | HGET |
HSET |
no | per field |
| JSON | JSON.GET path |
JSON.SET path |
with an index | per key |
A hash is flat, so nesting is the dividing line — use JSON when the document has structure.
An index
FT.CREATE indexes keys by prefix, declaring a type per field:
FT.CREATE idx ON HASH PREFIX 1 book: SCHEMA title TEXT author TAG year NUMERIC SORTABLE
HSET book:1 title "Dune" author herbert year 1965
HSET book:2 title "Neuromancer" author gibson year 1984
HSET book:3 title "Dune Messiah" author herbert year 1969Searching
A bare word is a full-text query over every TEXT field:
FT.SEARCH idx dune NOCONTENTdune matched both titles, tokenised and case-insensitive. Tag and numeric fields take
their own syntax, and SORTBY orders the result:
FT.SEARCH idx "@author:{herbert}" NOCONTENT
FT.SEARCH idx "@year:[1960 1970]" NOCONTENT
FT.SEARCH idx "*" SORTBY year DESC NOCONTENTVector sets
Members with coordinates, queried by nearness rather than by value:
VADD vs VALUES 3 1 2 3 item1
VADD vs VALUES 3 1 2 4 item2
VCARD vs
VDIM vs
VSIM vs VALUES 3 1 2 3 COUNT 2TimeSeries and Bloom
A time series stores timestamped numbers with downsampling and retention built in:
TS.CREATE temp
TS.ADD temp 1000 20.5
TS.ADD temp 2000 21.0
TS.RANGE temp - +A Bloom filter answers “have I seen this” in a fraction of a set’s memory:
BF.ADD seen alice
BF.EXISTS seen alice
BF.EXISTS seen bob