Redis®*: Indexing and searching those documents

This documentation is part of the Search and JSON guide. View the full guide here: Store JSON documents and query them efficiently with the Redis query engine.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Redis cloud solution that provides a host of benefits, including:

  • Redis Commander web UI included.
  • Unlimited message size and transfers.
  • Effortless updates with just a click.
  • Optimal performance and robust security powered by a private, dedicated infrastructure.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Redis cloud hosting solution!

You only need to declare an index once. Redis keeps it up to date automatically for every key matching the chosen prefix, including keys that existed before the index was created.

FT.CREATE productsIndex
  ON JSON
  PREFIX 1 product:
  SCHEMA
    $.name AS name TEXT
    $.brand AS brand TAG
    $.price AS price NUMERIC SORTABLE
    $.tags[*] AS tags TAG

Now you can run queries like:

# Full-text search on the name, with a price range
FT.SEARCH productsIndex "@name:(espresso) @price:[0 500]"

# Exact brand match, sorted by price
FT.SEARCH productsIndex "@brand:{Bianca}" SORTBY price ASC

# Prefix search for autocomplete scenarios
FT.SEARCH productsIndex "@name:(espr*)"

Redis can also handle aggregations directly on the index, so you can answer analytical queries without exporting data:

FT.AGGREGATE productsIndex "*"
  GROUPBY 1 @brand
  REDUCE COUNT 0 AS products
  REDUCE AVG 1 @price AS averagePrice
  SORTBY 2 @products DESC

If you store data in plain hashes instead of JSON, you can use ON HASH and standard field names in your schema.