This post is my personal notes on the Redis Search Engine course from Redis University. It covers the basics of the Redis Query Engine, including how to create indexes, perform queries, and use advanced features like aggregation and spellcheck.
To make the most of the commands and examples in this post, set up your Redis environment and load the data by following the instructions in the GitHub Repository. Once set up, create the index by running the following command in the Redis CLI:
| |
Querying Structured Data with Redis Query Engine
Exact Matches with TAG
The TAG field type is ideal for exact string matches, offering high efficiency.
| |
To query strings containing special characters (,.<>{}[]"':;!@#$%^&*()-+=~), escape them with a backslash (\).
| |
Numeric Range Queries with NUMERIC
Use the NUMERIC field type for numerical data, enabling powerful range queries. The range syntax is [min max], inclusive of both endpoints. Use -inf or +inf for unbounded ranges. To exclude an endpoint, add ( before the value.
| |
Full-Text Search with TEXT
Queries without a specified field default to full-text search across all TEXT fields.
| |
TEXTvs.TAG:TEXTfields undergo stemming (e.g., “running” becomes “run”), allowing broader matches.TAGfields provide exact string matches without stemming.
Sorting Results with SORTABLE
The SORTABLE option allows you to sort query results by a specified field (NUMERIC, TEXT, or TAG). Note that sorting is limited to one field at a time.
| |
Using
SORTBYon a field not marked SORTABLE will cause an error.
Limiting Results with LIMIT
LIMIT controls the number of results returned, taking a zero-based offset and a count.
| |
Full-text search with Redis Query Engine
Stemming
TEXT fields in RedisSearch store the root form of words (stemming), improving search results by matching different word forms (e.g., “running” matches “run”).
> FT.SEARCH books-idx "@title:running" RETURN 1 title
1) "14"
2) "ru203:book:details:9780451197962"
3) 1) "title"
2) "The Running Man"
4) "ru203:book:details:9780385315289"
5) 1) "title"
2: "Running from Safety"
6) "ru203:book:details:9780679722946"
7) 1) "title"
2) "Running Dog"
8) "ru203:book:details:9780345461612"
9) 1) "title"
2) "Running from the Deity"
10) "ru203:book:details:9780330281720"
11) 1) "title"
2) "Running in the Family"
12) "ru203:book:details:9780590317672"
13) 1) "title"
2) "Run"
14) "ru203:book:details:9780439650366"
15) 1) "title"
2) "Werewolves Don't Run For President"
16) "ru203:book:details:9781400033829"
17) 1) "title"
2) "Who Will Run the Frog Hospital?"
18) "ru203:book:details:9780340769201"
19) 1) "title"
2) "Running Away from Richard"
20) "ru203:book:details:9780671024185"
21) 1) "title"
2) "Morgan's Run"
Prefix Search
Prefix search finds words starting with a specific prefix.
Be aware that less specific queries can impact performance.
| |
Summarizing and Highlighting Results
SUMMARIZE provides a brief description of search results, returning the first LEN characters and FRAGS (number of fragments) from specified FIELDS.
| |
HIGHLIGHT emphasizes search terms within results for better readability.
| |
Aggregation with Redis Query Engine
The FT.AGGREGATE command performs calculations like counting, summing, or averaging on data, useful for large datasets.
Counting Results with COUNT
COUNT is used to count the number of records that match a specific query.
| |
Grouping Data with GROUPBY
GROUPBY is used to group the results by a specific field.
| |
Performing Calculations with REDUCE
REDUCE executes calculations on grouped results. Common functions include COUNT, COUNT_DISTINCTISH, SUM, AVG, MIN, and MAX.
| |
Applying Custom Functions with APPLY
APPLY allows custom functions for complex calculations not supported by built-in reduce functions.
| |
Advanced Redis Query Engine Topics
Partial Indexing
Partial indexing indexes only a subset of fields, useful for large documents where only specific fields require searching.
| |
Adjusting Result Scores with WEIGHT
The WEIGHT option allows you to adjust the score of results, boosting the relevance of certain fields.
| |
Handling Punctuation in Fields
If you wanna do matching on a field that contains punctiation, you need to proccess this field by escaping the punctuation characters.
| |
Spelling Error Correction
The fuzzy operator (%) enables fuzzy search for spelling errors, with the number of % signs determining the maximum Levenshtein distance.
| |
Alternatively, SPELLCHECK provides suggestions for misspelled words. A common practice is to check if a query returns results, and if not, use SPELLCHECK. This feature supports languages like English, French, German, Italian, and Spanish. I will cover how to customize the spellcheck dictionary in a future post.
| |
