Pros and Cons
Pros
1. It is faster and more efficient.
2. If you currently use Redis for caching, you may utilise the same Redis server and dataset for Full-Text searches.
Cons
1. Search algorithms are limited in comparison to Elasticsearch.
2. Because just a default tokenizer is accessible, there is no way to utilize an n-gram tokenizer like Elasticsearch.
3. There are just a few libraries available for various languages and systems.
Installation
If Docker is already installed in the local environment, RediSearch may be installed following the procedures below:
docker run -p 6379:6379 redislabs/redisearch:latest
Then, in a new terminal tab, type:
docker ps # Get the container id
docker exec -it {container_id} redis-cli
Following the installation procedures outlined above, you will be able to use Redis-CLI with the RediSearch module loaded. The instructions below explain how to build indexes and add documents to them.
Index
Indexes can be viewed as a table or a collection of records, such as a collection of items. Use the command FT.CREATE, to create a new index
FT.CREATE products ON HASH PREFIX 1 product: SCHEMA name TEXT SORTABLE quantity NUMERIC SORTABLE description TEXT
It is vital to remember that indexing is dependent on the prefix given. As a result, any Redis hash keys that begin with the prefix product would be added to the concerned index.
Documents
These are the records in the index, such as individual goods in a product collection. To add a document to the index, use the command HMSET and the appropriate index prefix, such as product:1, product:2.
HMSET product:1 name "Apple Smoothie" quantity 2 description "Fresh apple smoothie"
HMSET product:2 name "Mango Smoothie" quantity 4 description "Fresh mango smoothie"
HMSET product:3 name "Grape Juice" quantity 5 description "Fresh grape juice"
HMSET is a typical Redis command used to set the value of a hash. It has nothing to do with the RediSearch module. Because the prefix determines our index, any hashes added in this manner will be added to the index.
Search
We may use the command FT.SEARCH to find a document. For all Latin characters, the search is case insensitive. RediSearch primarily supports two search algorithms:
Prefix-based search.
This form of search is based on an individual term prefix:
FT.SEARCH products app*
FT.SEARCH products jui*
FT.SEARCH products @name=app*
Fuzzy Search
The Levenshtein Distance is used in this search (L.D.). It is the number of single-character changes necessary to transform one word into the other. For example, the distance between guava and grape is three.
A distance between 1 and 3 might be specified for searching:
FT.SEARCH products %jui%
FT.SEARCH products %%jui%%
FT.SEARCH products %%%jui%%%
When a large amount of L.D is supplied, this form of search might produce very erroneous results. For example, with L.D set to 3, searching for the phrase dog may yield cat.
Other Uses of Search
We can use the SEARCH command to list all index entries:
FT.SEARCH products *
Basic pagination and sorting are also possible:
FT.SEARCH products {term}* LIMIT #{OFFSET} #{LIMIT} SORTBY #{sort_field} #{sort_direction}
FT.SEARCH products jui* LIMIT 0 10 SORTBY quantity desc
Tokenization and Escaping
This is a vital notion to grasp when developing a search, especially if you intend to use special characters. When a product is named Apple Juice or Apple-Juice, it is divided into Apple and Juice phrases. As a result, searching for Apple-Ju* is not feasible since the field is divided into two separate words in the database. Some of the rules used for tokenization:
1. Special characters such as > [ ] " ' : ; ! @ # $ % & * () - + = divide the text into terms. For example, foo-bar will be divided into foo and bar words.
2. As all Latin letters are transformed to lowercase, searches are always case insensitive.
Escaping Special Characters
To search with special characters, both when generating the record and when completing the search query, the special characters must be escaped by a double backslash. Therefore, the word Apple-Juice would be divided into Apple, Juice, and Apple-Juice.
FAQ’s
How does RediSearch function?
RediSearch enables you to generate indexes on datasets (Hashes) easily and employs an incremental indexing strategy for speedy index generation and deletion.
How can we use RediSearch?
To get started with RediSearch, either uses the RediSearch Docker image or sign up for a free Redis Cloud Essentials account to receive a RediSearch instance on the cloud.
What is Fuzzy Search?
The Levenshtein Distance is used in this sort of search (L.D.). The Levenshtein distance between two words is the number of single-character changes necessary to transform one word into the other.
How can we add a document to the index?
To add a document to the index, use the command HMSET and the appropriate index prefix.
Conclusion
This article extensively discussed Search in Redis Stack using RediSearch, its types, and other uses in the multi-platform database systems.
We hope this blog has helped you enhance your knowledge regarding RediSearch. After reading about the Redis Database, are you not feeling excited to read/explore more articles on this topic? Don't worry; Coding Ninjas has you covered. To learn, see Operating System, Unix File System, File System Routing, and File Input/Output.
You can also consider our Mern Stack Course to give your career an edge over others.
Do upvote our blogs if you find them helpful and engaging!
Happy Learning!