Skip to content
Sign inGet all access
Chapter 8·10 minFree

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:

terminal
redis-cli MODULE LIST | grep -A1 '^name$' | grep -v 'name\|--' | sort
Output$ redis-cli MODULE LIST | grep -A1 '^name$' | grep -v 'name\|--' | sortReJSONbfsearchtimeseriesvectorset

JSON documents

JSON.SET takes a path and a value. $ is the root:

JSON.SET u $ '{"name":"ada","age":36}'
JSON.GET u
TYPE u
OutputOK"{\"name\":\"ada\",\"age\":36}"ReJSON-RL

TYPE 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 u
OutputOKOK"{\"name\":\"ada\",\"age\":37}"

Paths

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 $.tags
OutputOK"[\"London\"]""[\"a\"]""[[\"a\",\"b\"]]"

Editing 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 s
OutputOK"[15]"1) (integer) 2"{\"views\":15,\"tags\":[\"a\",\"b\"]}"

JSON.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 $.n
OutputOK1) (integer) 4(integer) 1"{\"n\":\"abcd\"}"1) "string"

Three 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 1969
OutputOK(integer) 3(integer) 3(integer) 3

Searching

A bare word is a full-text query over every TEXT field:

continues
FT.SEARCH idx dune NOCONTENT
Output1) (integer) 22) "book:1"3) "book:3"

dune matched both titles, tokenised and case-insensitive. Tag and numeric fields take their own syntax, and SORTBY orders the result:

continues
FT.SEARCH idx "@author:{herbert}" NOCONTENT
FT.SEARCH idx "@year:[1960 1970]" NOCONTENT
FT.SEARCH idx "*" SORTBY year DESC NOCONTENT
Output1) (integer) 22) "book:1"3) "book:3"1) (integer) 22) "book:1"3) "book:3"1) (integer) 32) "book:2"3) "book:3"4) "book:1"

Vector 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 2
Output(integer) 1(integer) 1(integer) 2(integer) 31) "item1"2) "item2"

TimeSeries 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 - +
OutputOK(integer) 1000(integer) 20001) 1) (integer) 1000 2) 20.52) 1) (integer) 2000 2) 21

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
Output(integer) 1(integer) 1(integer) 0