Redis

JSON Support in Redis

Redis expands its existing features with advanced module support. It uses the RedisJSON module to provide the JSON support in Redis databases. The RedisJSON module gives you an interface to read, save, and update the JSON documents with ease.

RedisJSON 2.0 provides an internal and public API that can be consumed by any other modules that reside in the same Redis node. It gives the ability for the modules like RediSearch to interact with the RedisJSON module. With these capabilities, the Redis database can be used as a powerful document-oriented database like MongoDB.

RedisJSON still lacks the indexing capabilities as a document database. Let’s have a quick look at how Redis provides indexing for JSON documents.

Indexing Support for JSON Documents

One of the major problems of RedisJSON is it does not come with in-built indexing mechanisms. Redis has to support the indexing with the help of other modules. Fortunately, the RediSearch module is already there that provides indexing and searching tools for Redis Hashes. Hence, Redis released the RediSearch 2.2 which supports indexing for document-based JSON data. It became fairly easy with RedisJSON’s internal public API. With the combined effort of RedisJSON and RediSearch modules, the Redis database can store and index the JSON data, and consumers can locate the JSON documents by querying the content that makes Redis a highly performing document-oriented database.

Create an Index with RediSearch

The FT.CREATE command is used to create an index using RediSearch. The ON JSON keyword should be used along with the FT.CREATE command to let Redis know that the existing or newly created JSON documents need to be indexed. Since RedisJSON supports JSONPath (from version 2.0), the SCHEMA part of this command can be defined using the JSONPath expressions. The following syntax is used to create a JSON index for JSON documents in the Redis data store.

Syntax:

FT.CREATE {name_of_index} ON JSON SCHEMA {JSONPath_expression} as {[attribute_name]} {data_type}

When you map the JSON elements to schema fields, it is a must to use the relevant schema field types as shown in the following:

JSON Document Element Schema Field Type
Strings TEXT, GEO, TAG
Numbers NUMERIC
Boolean TAG
Array of Numbers (JSON Array) NUMERIC, VECTOR
Array of Strings (JSON Array) TAG, TEXT
Array of Geo coordinates (JSON Array) GEO

 

In addition, the null element values and null values in an array are ignored. Moreover, it is not possible to index the JSON objects with RediSearch. In such situations, use each element of the JSON object as a separate attribute and index them.

The indexing process runs asynchronously for the existing JSON documents and the newly created or modified documents are indexed synchronously at the end of the “create” or “update” command.

In the following section, let’s discuss how to add a new JSON document to your Redis data store.

Create a JSON Document with RedisJSON

The RedisJSON module provides the JSON.SET and JSON.ARRAPPEND commands to create and modify the JSON documents.

Syntax:

JSON.SET <key> $<JSON_string>

Use Case – Indexing the JSON Documents that Contain the Employee Data

In this example, we will create three JSON documents that hold the employee data for the ABC company. Next, those documents are indexed using RediSearch. Finally, a given document is queried using the newly created index.

Before creating the JSON documents and indexes in Redis, the RedisJSON and RediSearch modules should be installed. There are a couple of approaches to use:

  • Redis Stack comes with RedisJSON and RediSearch modules which are already installed. You can use the Redis Stack docker image to up and run a Redis database that consists of those two modules.
  • Install the Redis 6.x or later version. Then, install the RedisJSON 2.0 or a later version along with the RediSearch 2.2 or a later version.

We use the Redis Stack to run a Redis database with RedisJSON and RediSearch modules.

Step 1: Configure the Redis Stack

Let’s run the following docker command to download the latest Redis-Stack docker image and start a Redis database inside a docker container:

udo docker run -d -name redis-stack-latest -p 6379:6379 -p 8001:8001 redis/redis-stack:latest

We assign the container name, redis-stack-latest. In addition, the internal container port 6379 is mapped to the local machine port 8001 as well. The redis/redis-stack:latest image is used.

Output:

Next, we run the redis-cli against the running Redis container database as follows:

sudo docker exec -it redis-stack-latest redis-cli

Output:

As expected, the Redis CLI prompt starts. Also, you can type the following URL on the browser and check whether the Redis stack is running:

localhost:8001

Output:

Step 2: Create an Index

Before creating an index, you need to know what your JSON document elements and structure look like. In our case, the JSON document structure looks like the following:

{
 "name": "John Derek",
 "salary": "198890",
}

We index the name attribute of each JSON document. The following RediSearch command is used to create the index:

FT.CREATE empNameIdx ON JSON SCHEMA $.name AS employeeName TEXT

Output:

Since RediSearch supports JSONPath expressions from the version 2.2, you can define the schema using the JSONPath expressions as in the previous command.

$.name

NOTE: You can specify multiple attributes in one single FT.CREATE command as shown in the following:

FT.CREATE empIdx ON JSON SCHEMA $.name AS employeeName TEXT $.salary AS employeeSalary NUMERIC

Step 3: Add JSON Documents

Let’s add three JSON documents using the JSON.SET command as follows. Since the index is already created, the indexing process is synchronous in this situation. The newly added JSON documents is immediately available on the index:

JSON.SET emp:1 $ '{"name" : "Harris Rauf", "Salary": 10000}'

JSON.SET emp:2 $ '{"name" : "Mark Wood", "Salary": 34000}'

JSON.SET emp:3 $ '{"name" : "Mary Jane", "Salary": 23000}'

Output:

To know more about manipulating the JSON documents with RedisJSON, have a look here.

Step 4: Query the Employee Data Using the Index

Since you already created the index, the previously created JSON documents should be already available in the index. The FT.SEARCH command can be used to search any attribute which is defined in the empNameIdx schema.

Let’s search for the JSON document that contains the “Mark” word in the name attribute.

FT.SEARCH empNameIdx '@employeeName:Mark'

You can also use the following command:

FT.SEARCH empNameIdx '@employeeName:(Mark)'

Output:

As expected, the JSON document is stored at the key. Emp:2 is returned.

Let’s add a new JSON document and check whether it is indexed properly. The JSON.SET command is used as follows:

JSON.SET emp:4 $ '{"name" : "Mary Nickolas", "Salary": 56000}'

Output:

We can retrieve the added JSON document using the JSON.GET command as follows:

JSON.GET emp:4 $

NOTE: The syntax of the JSON.GET command is as follows:

JSON.GET <key> $

Output:

Let’s run the FT.SEARCH command to search for the document(s) that contains the word “Mary” in the name attribute of JSON.

FT.SEARCH empNameIdx '@employeeName:Mary'

Output:

Since we got two JSON documents that contain the word Mary in the name attribute, two documents are returned.

There are several ways to do your searching and index creation using the RediSearch module and those are discussed in the other article. This guide mainly focuses on giving a high-level overview and understanding of indexing JSON documents in Redis using RediSearch and RedisJSON modules.

Conclusion

This guide explains how powerful the Redis indexing is where you can query or search for the JSON data based on its content with low latency.

Follow the following links to get more details on RedisJSON and RediSearch modules:

About the author

Nimesha Jinarajadasa

Being a Full-stack Senior Software Engineer for more than five years, I love technology, as technology has the power to solve our many problems within just a minute. I try to learn more and create more opportunities for this new world.