Search code examples
redisredisearch

Is it possible to build a faceted search with Redisearch?


Consider the following model:

{
  id: string;
  name: string;
  description: string;
  price: number;
  brand: string;
  size: string;
  color: string;
}

Using RediSearch, is it possible to get a list of counts for each field mapping that exists within the result set for a given query? For example, I'd like to get data that looks something like this:

{
  brand: {
    nike: 6,
    adidas: 3,
    reebok: 2
  },
  size: {
    sm: 2,
    md: 4,
    lg: 3,
    xl: 2
  },
  color: {
    blue: 4,
    black: 4,
    white: 2,
    gray: 1
  }
}

The data doesn't have to fit exactly that shape, but the intent should be clear.

I know how to get the values for a single field doing this:

FT.AGGREGATE products "*" GROUPBY 1 @brand REDUCE COUNT 0 AS count

However, if there are a large number of indexed fields, this would require quite a few queries to look up each one individually, and on a large document set (more than 1,000,000 records), this could be quite slow and intensive.

Is there a way to aggregate by multiple fields at the same time? If not, is there a better approach? Are my assumptions about this being too an expensive an operation correct, or is this acceptable for an in-memory database?

Thanks for any guidance you can provide!


Solution

  • Unfortunately, it seems the answer to my question is no. I received this response from one of the Redis admins on their Discord server:

    I'm not seeing another way to do this either. You could pipeline all the queries via a pipeline to reduce network round trips which would help with performance but I think you are forced to either a) denormalize your data somehow or b) execute multiple queries.