Full-text search using Redis Stack
RediSearch is a powerful text search and secondary indexing engine, built on top of Redis as a Redis module. Written in C, RediSearch is extremely fast compared to other open-source search engines. It implements multiple data types and commands that fundamentally change what you can do with Redis. RediSearch supports capabilities for search and filtering such as geo-spatial queries, retrieving only IDs (instead of whole documents), and custom document scoring. Aggregations can combine map, filter, and reduce/group-by operations in custom pipelines that run across millions of elements in an instant.
RediSearch also supports auto-completion with fuzzy prefix matching, and atomic real-time insertion of new documents to a search index. With the latest RediSearch 2.0 release, it’s now easier than ever to create a secondary index on top of your existing data. You can just add RediSearch to your existing Redis database, create an index, and start querying it, without having to migrate your data or use new commands for adding data to the index. This drastically lowers the learning curve for new RediSearch users and lets you create indexes on your existing Redis databases—without even having to restart them.
Step 1. Create a free Cloud account
Create your free Redis Enterprise Cloud account. Once you click on “Get Started”, you will receive an email with a link to activate your account and complete your signup process.
For a limited time, use TIGER200 to get $200 credits on Redis Enterprise Cloud and try all the advanced capabilities!
Step 2. Create Your database
Choose your preferred cloud vendor. Select the region and then click "Let's start free" to create your free database automatically.
If you want to create a custom database with your preferred name and type of Redis, click "Create a custom database" option shown in the image.
Step 3. Verify the database details
You will be provided with Public endpoint URL and "Redis Stack" as the type of database with the list of modules that comes by default.
Step 4. Using RedisInsight
RedisInsight is a visual tool that lets you do both GUI- and CLI-based interactions with your Redis database, and so much more when developing your Redis based application. It is a fully-featured pure Desktop GUI client that provides capabilities to design, develop and optimize your Redis application. It works with any cloud provider as long as you run it on a host with network access to your cloud-based Redis server. It makes it easy to discover cloud databases and configure connection details with a single click. It allows you to automatically add Redis Enterprise Software and Redis Enterprise Cloud databases.
Assuming that you already have RedisInsight v2 installed on your MacOS, you can browse through the Applications and click "RedisInsight-v2" to bring up the Redis Desktop GUI tool.
Step 5. Add Redis database
Step 6. Enter Redis Enterprise Cloud details
Add the Redis Enterprise cloud database endpoint, port and password.
Step 7. Verify the database under RedisInsight dashboard
Step 8. Getting Started with Redisearch
To begin, let’s create a basic dataset based on movies information, which we will use to show how to:
- Insert data
- Create an index
- Query data
Insert data into RediSearch
We are now ready to insert some data. This example uses movies data stored as Redis Hashes, so let’s insert a couple of movies:
HSET movies:11002 title "Star Wars: Episode V - The Empire Strikes Back" plot "Luke Skywalker begins Jedi training with Yoda." release_year 1980 genre "Action" rating 8.7 votes 1127635
(integer) 6
> HSET movies:11003 title "The Godfather" plot "The aging patriarch of an organized crime dynasty transfers control of his empire to his son." release_year 1972 genre "Drama" rating 9.2 votes 1563839
(integer) 6
Your Redis Enterprise Cloud database now contains two Hashes. It is simple to retrieve information using the HMGET command, if you know the key of the movies (movies:11002):
> HMGET movies:11002 title rating
1) "Star Wars: Episode V - The Empire Strikes Back"
2) "8.7"
Create an index in RediSearch
To be able to query the hashes on the field for title, say, or genre, you must first create an index. To create an index, you must define a schema to list the fields and their types that are indexed, and that you can use in your queries.
Use the FT.CREATE command to create an index, as shown here:
> FT.CREATE idx:movies ON hash PREFIX 1 "movies:" SCHEMA title TEXT SORTABLE release_year NUMERIC SORTABLE rating NUMERIC SORTABLE genre TAG SORTABLE
OK
In the command above, we:
- Create an index named idx:movies
- Used a schema made up of four fields: title release_year rating genre
Before running queries on our new index, though, let’s take a closer look at the elements of the FT.CREATE command:
- idx:movies: the name of the index, which you will use when doing queries
- ON hash: the type of structure to be indexed. (Note that RediSearch 2.0 supports only the Hash structure, but this parameter will allow RediSearch to index other structures in the future.)
- PREFIX 1 “movies:”: the prefix of the keys that should be indexed. This is a list, so since we want to index only movies:* keys the number is 1. If you want to index movies and TV shows with the same fields, you could use: PREFIX 2 “movies:” “tv_show:”
- SCHEMA …: defines the schema, the fields, and their type to index. As you can see in the command, we are using TEXT, NUMERIC, and TAG, as well as SORTABLE parameters.
The RediSearch 2.0 engine will scan the database using the PREFIX values, and update the index based on the schema definition. This makes it easy to add an index to an existing application that uses Hashes, there’s no need to change your code.
Search the movies in the RediSearch index
You can now use the FT.SEARCH to search your database, for example, to search all movies sorted by release year:
> FT.SEARCH idx:movies * SORTBY release_year ASC RETURN 2 title release_year
1) (integer) 2
2) "movies:1003"
3) 1) "release_year"
2) "1972"
3) "title"
4) "The Godfather"
4) "movies:1002"
5) 1) "release_year"
2) "1980"
3) "title"
4) "Star Wars: Episode V - The Empire Strikes Back"
You can also search “action” movies that contain “star” in the index (in our sample index, the term “star” will occur only in the title):
> FT.SEARCH idx:movies "star @genre:{action}" RETURN 2 title release_year
1) (integer) 1
2) "movies:1002"
3) 1) "title"
2) "Star Wars: Episode V - The Empire Strikes Back"
3) "release_year"
4) "1980"
The FT.SEARCH command is the base command to search your database, it has many options and is associated with a powerful and rich query syntax that you can find in the documentation. (Note: You can also use the index to do data aggregation using the FT.AGGREGATE command.)
Next Steps
- Learn more about RediSearch in the Getting Started with RediSearch 2.0 tutorial on GitHub.
- How to list and search Movie database using Redisearch