Search and Speed - RediSearch

Search and Speed - RediSearch

What is Redis?

According to the Rediswebsite and Redis doc, Redis is an open source, in-memory data structure store used as a database, cache, message broker, and streaming engine that is utilized by millions of developers today.

Redis increased the popularity of the concept of a system being used as both a cache and a store. It uses a design in which data is always modified and read from the main computer memory, but it is also stored on disk in a format that is not suitable for random access of data, but only to reconstruct the data back in memory after a system restart.

What is RediSearch?

RediSearch is a potent full-text search, query, and indexing engine for Redis that is offered both locally and as a managed service in the cloud. In a full-text search, a search engine looks at every word in every document it has stored in an effort to match search parameters, such as the text entered by a user, e.g., searching for "Buy" would search for "Buying food," "bought" etc. Additionally, you may improve search results by including auto-complete hints with "fuzzy" queries.

With the help of RediSearch's Enterprise and Enterprise Cloud services, you can easily scale RediSearch over an entire cluster and expand your indexes to billions of pages across a large number of servers.

You can easily generate indexes on datasets (hashes) using RediSearch, and it employs an incremental indexing method for efficient index generation and deletion. The indexes enable quick data queries, sophisticated grouping, and filtering by attributes, numerical ranges, and proximity to a specific distance.

Where's Redis JSON in this?

Redis can support JSON thanks to the RedisJSON module. Similar to any other Redis data type, RedisJSON enables you to save, update, and retrieve JSON values from a Redis database. RedisJSON and RediSearch integrate well so that you can index and search JSON documents.

The benefits of RediSearch

RediSearch has proven to be beneficial in lots of ways, I'll be going through a few of them.

  1. RediSearch maintains concurrent loads of querying and indexing and consistently offers excellent performance with short latency times. RediSearch also gives continuous results without performance deterioration.

  2. Redisearch is built as a Redis module and written in C natively, allowing it to write data to DRAM on a server without the need for any translation or other layers. It implements specific data types that are designed for searches, queries, and indexing as a Redis module. This results in extraordinarily high speed for real-time indexing, with index entries being accessible for querying in nanoseconds.

  3. Infrastructure expenses are decreased because RediSearch eliminates the requirement for a cache, which lowers the cost of maintaining the search engine. Redisearch expands to multi-node configurations with ease and is especially well suited for simultaneous and immediate indexing and search situations. It also effectively indexes and searches billions of pages at high efficiency.

  4. RediSearch's clean-sheet redesign of search and secondary indexing allows it to retain Redis' speed and ease of operation while avoiding decades of incremental bloat.

Learn more about Redis 2.0 using these links. Getting started with RediSearch 2.0 RediSearch 2.0 Tutorial

Where do I start?

The majority of people regularly ask this question, especially engineers who are just learning about RediSearch's possibilities. The best thing is that I'll be walking you through the process of putting RediSearch to use.

A couple of things to take into account are:

  1. Redis Cloud
  2. Docker
  3. Redis Cloud account

Making use of Docker

Using Docker, run the RediSearch redis-stack-server Docker image as follows:

$ docker run -d - name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest

Visit Docker with Redis Stack for additional details on how to run Redis Stack within a Docker container.

Binary download

Do the following to download and launch RediSearch from a precompiled binary:

  1. Visit the Redis Download Center to obtain a pre-compiled version of RediSearch.
  2. Execute Redis with the RediSearch command:
$ redis-server - loadmodule /path/to/module/src/redisearch.so

Source Build

Do the following to build and run RediSearch from source code:

Clone the RediSearch repository on git (include the - recursive option to ensure you properly clone submodules):

$ git clone - recursive https://github.com/RediSearch/RediSearch.git
$ cd RediSearch

Install dependencies

For macOS:

$ make setup

For Linux:

$ sudo make setup

To Build:

$ make build

Execute Redis with RediSearch:

$ make run

Visit the development documentation for more detailed "build" instructions.

Creating an index

Create an index with fields and weights using the FT.CREATE command (the default weight is 1.0):

127.0.0.1:6379> FT.CREATE myIdx ON HASH PREFIX 1 doc: SCHEMA title TEXT WEIGHT 5.0 body TEXT url TEXT
OK

Any existing hash documents that have a key prefixed with doc: are automatically added to the index at this time.

Adding documents

New hash documents with the doc: prefix that are created after you build the index are immediately and automatically indexed.

Create a fresh hash document and add it to the index using the HSET command:

127.0.0.1:6379> HSET doc:1 title "hello world" body "lorem ipsum" url "http://redis.io"
(integer) 3

Get more information about hash and HSET

Searching for the index

Use the FT.SEARCH command to look through the index for records that contain particular terms.

127.0.0.1:6379> FT.SEARCH myIdx "hello world" LIMIT 0 101) (integer) 1
2) "doc:1"
3) 1) "title"
 2) "hello world"
 3) "body"
 4) "lorem ipsum"
 5) "url"
 6) "http://redis.io"

Drop the index.

Run FT.DROPINDEX without the DD option to eliminate the index without erasing the related hash documents:

127.0.0.1:6379> FT.DROPINDEX myIdx
OK

For more information, see FT.DROPINDEX

Add the DD option to the command to delete both the index and all indexed hash documents:

127.0.0.1:6379> FT.DROPINDEX myIdx DD
OK

Auto-complete

FT.SUGADD can be used to add an auto-complete suggestion:

127.0.0.1:6379> FT.SUGADD autocomplete "hello world" 100
(integer) 1

See the Auto-complete doc for more info.

Use FT.SUGGET to test auto-complete suggestions:

127.0.0.1:6379> FT.SUGGET autocomplete "he"
1) "hello world"

See the FT.SUGADD for more information.

Why is RediSearch so potent?

RediSearch begins with a schema that describes how the data will be progressively indexed when new documents are added. There are four general categories that can apply to any field in the index:

  1. RediSearch is multilingual and includes built-in support for 18 languages. Text for character-based data (including Chinese).
  2. Numbers represent information that can already be counted.
  3. Data that is spatial and relies on geographic coordinates
  4. Labels for meta-data that identify a specific document

Building a schema may be made as simple as a few letters, or it can be enhanced and extended in a vast variety of ways. After a schema has been created, documents may be added to RediSearch. These documents are immediately indexed after being added, making them easily accessible to queries.

Each document includes a unique ID and a weight that aids in queries in addition to the data specified in the schema.

Why is RediSearch so powerful?

RediSearch enables you to search for documents using simple keywords or the built-in query language, which provides a wide range of filtering and enhancing possibilities. The query language may mix logical operators with parenthesis to restrict the results and can automatically generate clauses based on any of the fields and types. RediSearch may also be used to aggregate the values that are stored in an index. Filters, grouping and reduction, transformation, sorting, and limiting are all part of the aggregate's processing pipeline. Below are some features of why RediSearch is right for your needs.

Full-text indexing of a document's many fields, such as:

  • Exactly identical phrases
  • Many different languages stem
  • Chinese support for tokenization
  • Query prefix
  • Negative, union, and optional queries

Searching through billions of documents. Indexing by numeric properties. Filters based on radius and geographic indexing. Indexing incrementally without sacrificing performance. An organized query language for complex questions, such as:

  • intersections and unions
  • Negative and optional questions
  • Tag filtration
  • matching the prefix

Engine for integrated aggregations that enables the construction of pipelinable activities, including:

  • Groups
  • Reductions
  • Sorts
  • Transformations
  • Limits

RediSearch is an effective auto-complete tool with fuzzy matching, sorting by values, and utilizing various scoring methods, updates, and insertion of documents concurrently and with minimum delay.

Redis is not blocked by concurrent searches that enable lengthy queries. It is a framework for extensions that permits query extensions and customized scoring models. In Redis databases, there is assistance for indexing existing hash objects by using a coordinator object, which encourages clustering.

Here's more with Redis Cloud and RedisInsight.

Data Model, Storing, and Retrieval

Do you require a key-value pair-based database? NoSQL, obviously, and disk persistence, all at high performance? If the answer is "yes," but you're unsure about what the solution may be, I'd definitely advise taking a look at RedisDB, an excellent choice for handling significant performance issues like log collecting or page view ranking.

Redis also has capabilities like the Publisher/Subscribe paradigm, keys with TTLs, and transaction support.

I won't go into detail about all the features mentioned above because the emphasis of this piece is the data component.

For instance, the user data in Redis appears as follows:

  • ``` "user:0:first_name"="Michael" "user:0:last_name"="Brown" "user:0:age"="57" … "user:1:first_name"="Melanie" "user:1:last_name"="Janson" "user:1:age"="21" … "user:2:first_name"="Leanora" "user:2:last_name"="Dennell" "user:2:age"="51" …
  • ``` The pattern is: [entity]:[key]:[property] = [value]

Obviously, this is not the only way to store the data; rather, it is just a common practice among Redis users. Nevertheless, I'll demonstrate a different strategy in my example, even if it is based on the aforementioned schema.

How do you determine the 1..N relations, furthermore? As an illustration, the entity User has a relationship with relatives, who are also users. The Redis method is as follows:

How about utilizing the children's names to contact the relative? You need to create a new dataset using the child's name as a key, which appears just like:

"child:melanie_janson:father"="user:0"
"child:melanie_janson:mother"="user:2"

Additionally, if you wish to find Melanie's family members, you must search the map for the key "child:melanie janson:."*

Fundamentally, the goal is to create any key/index map for any information that an application can ask for; the conditions under which an application can access data are crucial to the design of the store.

Also, with Redis being a key-value store that only accepts strings as keys and a few alternative data types as values. This fact hasn't been stated. Given that each Bigfoot encounter has a variety of details, using a JSON structure as a value would be the best course of action.

Each key can combine a concrete sighting identification number with the prefix "sighting" (e.g., sighthing:2512).

We may use the JSON.SET command to associate a JSON value with a particular key in order to enter specific key-value data. Although the data in the sample below may be more intricate and hierarchical, it does have a number of JSON fields.

127.0.0.1:6379> JSON.SET sighting:2512
'{
"id": "2512"
"title": "I was suddenly next to a giant rock…',
"State": "Alabama",
"Date": "19062022",
"Location": "-139.7667,61.09086"
}'
OK

Conclusion

We've covered the fundamental ideas behind delivering effective search with Redis and its modules in a concise overview of (RedisJSON and RediSearch). We've gone through some of the instructions for loading and retrieving data from Redis along the way, as well as the commands for searching and aggregating data.

You may always investigate the GitHub repository, as was already indicated, which merely lengthens the learning curve for Redis. You can learn more about how to search for and aggregate Bigfoot encounters using the Redis client there (which is designed particularly for NodeJS).

This post is in collaboration with Redis.

Check out some useful resources below.

Try Redis Cloud for free Watch this video on the benefits of Redis Cloud over other Redis providers Redis Developer Hub - tools, guides, and tutorials about Redis> > RedisInsight Desktop GUI