Skip to main content

FT.AGGREGATE

Syntax

FT.AGGREGATE index query
[LOAD count field [field ...]]
[GROUPBY nargs property [property ...] [REDUCE function nargs arg [arg ...]] [REDUCE function nargs arg [arg ...]]]
[SORTBY nargs property [ASC|DESC] [property [ASC|DESC] ...] [MAX num]]
[LIMIT offset num]
[PARAMS nargs name value [name value ...]]

Time complexity: O(N)

Description

Run a search query and perform aggregate transformations on the results.

This command provides aggregation functionality similar to SQL GROUP BY operations, allowing you to group search results and apply reduction functions.

Required arguments

index

is index name. You must first create the index using FT.CREATE.

query

is text query to search. If it's more than a single word, put it in quotes. Refer to query syntax for more details.

Optional arguments

LOAD count field [field ...]

loads additional fields from the document that are not part of the index.

count is the number of fields to load. field is the field name to load from the document.

GROUPBY nargs property [property ...] [REDUCE ...]

groups the results according to one or more properties.

nargs is the number of properties to group by. property is the property name to group by.

Optionally followed by one or more REDUCE clauses that aggregate the grouped results.

REDUCE function nargs arg [arg ...]

applies an aggregate function to the grouped results.

Common functions include:

  • COUNT - counts the number of records in each group
  • SUM property - sums the values of the given property
  • MIN property - finds minimum value
  • MAX property - finds maximum value
  • AVG property - calculates average value
  • STDDEV property - calculates standard deviation
SORTBY nargs property [ASC|DESC] [property [ASC|DESC] ...] [MAX num]

sorts the results by the given properties.

nargs is the number of properties to sort by. property is the property name to sort by. ASC|DESC specifies sort order (default is ASC). MAX num limits the number of results.

LIMIT offset num

limits the results to the offset and number of results given.

The offset is zero-indexed. Default is 0 10.

PARAMS nargs name value [name value ...]

defines one or more value parameters that can be referenced in the query.

Similar to FT.SEARCH PARAMS option.

Return

FT.AGGREGATE returns an array reply with aggregated results based on the specified grouping and reduction operations.

Limited support

FT.AGGREGATE has partial support in Dragonfly. Some advanced aggregation functions and options may not be fully implemented.

Examples

Group results by category and count
dragonfly> FT.AGGREGATE products "*" GROUPBY 1 @category REDUCE COUNT 0 AS count
Calculate average price by category
dragonfly> FT.AGGREGATE products "*" GROUPBY 1 @category REDUCE AVG 1 @price AS avg_price

See also

FT.SEARCH | FT.CREATE