Implement Full-Text Search Using RediSearch
Hello Everyone, I’m back with yet another article on Redis, this time on RediSearch, so let’s get started with a simple definition of RediSearch.
RediSearch is a Redis module that enables Redis's querying, real-time secondary index, and full-text search. These features allow multi-field queries, aggregation, exact phrase matching, numeric filtering, and geo-filtering for text queries.
Now Let’s see RediSearch in practice.
To get started, create a free Redis account. With this account, you will also get one free database.
Just follow the instructions and your free database will be ready in just seconds.
You should see the following screen after your Database is created:-
Now that you have created your account and database successfully, we can dive into our main course which is to see RediSearch in Practice.
How the RediSearch 2.0 works
In RediSearch 2.0 Redis has re-architected the way indices are kept in sync with the data. So now instead of having to write data through the index (using the FT.ADD command), RediSearch now follows the data written in hashes and synchronously indexes it.
The following diagram shows how RediSearch converts documents to hashes and indexes them synchronously.
So now to use the full-text search feature, we have to store documents in hashes. We have to initialize the index with FT.CREATE
command and use the FT.SEARCH
command to search by text.
So now we have covered the basics let’s dive into some action
Start Redis CLI using the below command on your terminal:-
>redis-cli -h host_name -p port_number -a password
In the above command, you have to update host_name, port_number, and password, you can get this information when you click on the Database that you have just created on Redis Labs.
So now you should see the Redis CLI prompt similar to the following image:-
Now let’s insert a few docs
This will create 5 hashes, named document:1
,document:2
and so on, till document:5
with title
and body
as text fields.
Now as our database contains these hashes, It is simple to retrieve information using the following command, if you know the key of the document (document:1)
>HMGET document:1 title body
Here title and body are the fields that we want to fetch using the key.
So this is all nice, we have fetched the data from the database using the key, but this is not why we are here, we are here for full-text searches, like how can you query the database to get a list of documents based on using the title, the body or any other field that we might have created?
To do this we can simply define an index associated with our data and let the database manage them. We can then use the query engine to query/search the data using secondary indices.
Create a RediSearch index for our documents
> FT.CREATE doc_index PREFIX 1 “document:” SCHEMA title TEXT body text
Before running queries, though, let’s take a closer look at the FT.CREATE command:
- doc_index: the name of the index, which you will use when doing queries
- PREFIX 1 “document:”: the prefix of the keys that should be indexed. This is a list, so since we want to only index document:* keys the number is 1.
- SCHEMA …: defines the schema, the fields, and their type to index. As you can see in the command, we are using TEXT, NUMERIC, TAG, and SORTABLE parameters.
Searching index
The RediSearch 2.0 engine will scan the database using the PREFIX values, Searching is as easy as calling FT.SEARCH
command with index name and phrase to search:
> FT.SEARCH doc_index "redis"
Real-time indexing
Add a new document and Redis will index it in real-time.
> HSET document:6 title "Doc 6" body "New documents indexed automatically"
Searching for specific fields
We can search on specific fields with the following syntax:-
> FT.SEARCH doc_index "@body:fast"
You can check all the variations of FT.SEARCH command in the official documentation.
Conclusion
RediSearch is a powerful and feature-rich full-text search engine with an already known performance of Redis, it can be efficient for production solutions for full-text search in your applications.
P.S. This post is in collaboration with Redis.
Learn more: