Redis®*: Indexing and searching those documents

This documentation is part of the Search and JSON guide. You can view the complete guide here: Store JSON documents and query them efficiently with the Redis query engine.

👋 Welcome to the Stackhero documentation!

Stackhero provides a ready-to-use Redis cloud solution that delivers numerous advantages, including:

  • Redis Commander web interface included.
  • Unlimited message size and transfers.
  • One-click simplified updates.
  • Optimal performance and enhanced security thanks to a private, dedicated infrastructure.

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

You only need to declare an index once. Redis will then automatically keep it up to date 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

You can then run powerful queries, for example:

# 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, allowing you to 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 your data in plain hashes instead of JSON, you can use ON HASH and standard field names in your schema.