Is it possible to perform a faceted search with Redis?

Last updated 24, Apr 2024

Question

Is it possible to perform a faceted search with Redis?

Answer

Faceted search helps you classify data by attributes (or facets) in your Redis database. You can implement faceted search using the FT.AGGREGATE command. In this example, we will store five documents classified into different categories. The index is created as follows.

FT.CREATE doc_idx PREFIX 1 doc: SCHEMA category TAG content TEXT title TEXT

Documents can be stored as hashes (JSON documents work too).

HSET doc:1 title "How to perform vector search with Redis" content "..." category "docs"
HSET doc:2 title "Configure Redis durability" content "..." category "docs"
HSET doc:3 title "Reduce memory consumption" content "..." category "kb"
HSET doc:4 title "Addressing performance tuning" content "..." category "kb"
HSET doc:5 title "Getting started with the C#/.NET NRedisStack client library" content "..." category "training"

Then we can get an overview of how many documents are stored per category.

FT.AGGREGATE doc_idx * GROUPBY 1 @category REDUCE COUNT 0 AS num_per_ctg SORTBY 2 @num_per_ctg DESC
1) (integer) 3
2) 1) "category"
   2) "docs"
   3) "num_per_ctg"
   4) "2"
3) 1) "category"
   2) "kb"
   3) "num_per_ctg"
   4) "2"
4) 1) "category"
   2) "training"
   3) "num_per_ctg"
   4) "1"

And retrieve all the documents in a certain category.

FT.SEARCH doc_idx '@category:{docs}' RETURN 1 title LIMIT 0 10
1) (integer) 2
2) "doc:2"
3) 1) "title"
   2) "Configure Redis durability"
4) "doc:1"
5) 1) "title"
   2) "How to perform vector search with Redis"

And include additional filters, such as a textual filter for the term "vectors" to perform a full-text search.

FT.SEARCH doc_idx 'vectors @category:{docs}' RETURN 1 title LIMIT 0 10
1) (integer) 1
2) "doc:1"
3) 1) "title"
   2) "How to perform vector search with Redis"

References