Introduction to RedisJSON - The Perfect Combination for RediSearch

Introduction to RedisJSON - The Perfect Combination for RediSearch

Daily short news for you
  • Since the Lunar New Year holiday has started, I won't be posting anymore. See you all after the holiday! 😁

    » Read more
  • Continuing about jj. I'm wondering if there are any GUI software made for it yet to make it easier to use. There are already so many similar to git that I can't count them all.

    Luckily, the author has compiled them all together in Community-built tools around Jujutsu 🥳

    » Read more
  • Turso announces that they are rewriting SQLite in Rust. This adds another piece of evidence supporting the notion that Rust is "redefining" many things.

    But the deeper reason is more interesting. Why are they doing this? Everyone knows that SQLite is open source, and anyone can create a fork to modify it as they wish. Does the Turso team dislike or distrust C—the language used to build SQLite?

    Let me share a bit of a story. Turso is a provider of database server services based on SQLite; they have made some customizations to a fork of SQLite to serve their purposes, calling it libSQL. They are "generous" in allowing the community to contribute freely.

    Returning to the point that SQLite is open source but not open contribution. There is only a small group of people behind the maintenance of this source code, and they do not accept pull requests from others. This means that any changes or features are created solely by this group. It seems that SQLite is very popular, but the community cannot do what they want, which is to contribute to its development.

    We know that most open source applications usually come with a "tests" directory that contains very strict tests. This makes collaboration in development much easier. If you want to modify or add a new feature, you first need to ensure that the changes pass all the tests. Many reports suggest that SQLite does not publicly share this testing suite. This inadvertently makes it difficult for those who want to modify the source code, as they are uncertain whether their new implementation is compatible with the existing features.

    tursodatabase/limbo is the project rewriting SQLite in Rust mentioned at the beginning of this article. They claim that it is fully compatible with SQLite and completely open source. Limbo is currently in the final stages of development. Let’s wait and see what the results will be in the future. For a detailed article, visit Introducing Limbo: A complete rewrite of SQLite in Rust.

    » Read more

The Problem

I recently wrote an article about using RediSearch as the main database on What is RediSearch? Estacks is using RediSearch as a database!. Along with that, I explained why I chose RediSearch for the full-text search feature in RediSearch, making the search functionality of my blog more powerful and useful for readers.

Everything is working great so far, but there's one problem - Redis does not have a JSON data type, and I want to work with JSON data in the most user-friendly way possible. During my research, I discovered that Redis provides a module called RedisJSON that allows me to do just that. If you are using Redis or RediSearch and want to work with JSON data, this is a fantastic solution.

What is RedisJSON?

RedisJSON is a module that provides JSON support for Redis. RedisJSON allows you to store, update, and retrieve JSON values in the Redis database just like any other Redis data type. RedisJSON also works with RediSearch, allowing you to index and query JSON documents.

127.0.0.1:6379> JSON.SET obj $ '{"title": "Hello Developers - What to Drink and Code Today?", "url": "https://2coffee.dev"}'
"OK"
127.0.0.1:6379> JSON.GET obj $
[{"title":"Hello Developers - What to Drink and Code Today?","url":"https://2coffee.dev"}]

Usage

To use RedisJSON, you need to install Redis v6.x on your server. Redis provides several different methods for loading the RedisJSON module. Two popular methods are:

  • Edit the redis.conf configuration file:
loadmodule /path/to/module/target/release/librejson.so
  • Use the Command-line:
$ redis-server --loadmodule /path/to/module/librejson.so

Where /path/to/module is the path to your Redis installation directory. For example:

$ redis-server --loadmodule /usr/lib/redis/module/librejson.so

For more details, you can refer to the official documentation page.

Basic Syntax

As mentioned above, RedisJSON provides us with many syntaxes to store, update, or retrieve JSON values.

127.0.0.1:6379> JSON.SET obj $.year 2022
OK
127.0.0.1:6379> JSON.SET obj $.users '["admin"]'
OK
127.0.0.1:6379> JSON.ARRAPPEND obj $.users '"hoaitx"'
2
127.0.0.1:6379> JSON.GET obj $
[{"title":"Hello Developers - What to Drink and Code Today?","url":"https://2coffee.dev","year":2022,"users":["admin","hoaitx"]}]
127.0.0.1:6379> JSON.DEL obj
"OK"

To see the complete list of JSON data manipulation commands, you can visit the Commands documentation page

Using with RediSearch

To use RedisJSON with RediSearch, you need to have RediSearch v2.2 or higher and RedisJSON v2.0 or higher installed on your Redis server.

Then, create a schema in RediSearch to serve the search:

127.0.0.1:6379> FT.CREATE article ON JSON PREFIX 1 article: SCHEMA $.title AS title TEXT $.content as content TEXT $.view AS view NUMERIC
OK

To add data to the Schema, use the JSON.SET command:

127.0.0.1:6379> JSON.SET article:1 $ '{"title": "2coffee", "content": "Hello Developers - What to Drink and Code Today?", "view": 0}'

Then, you can perform full-text search using RediSearch syntax:

127.0.0.1:6379> FT.SEARCH article '@title:%coffee%'
1
article:1000
$
{"title":"2coffee","content":"Hello Developers - What to Drink and Code Today?","view":0}

Starting from RediSearch 2.6.0, you can also perform full-text search on attributes that are an array of strings.

127.0.0.1:6379> JSON.SET article:1 $ '{"title": ["2coffee", "hicoffee"], "content": "Hello Developers - What to Drink and Code Today?", "view": 0}'
127.0.0.1:6379> FT.SEARCH article '@title:%coffee%'

Starting from RediSearch 2.6.1, you can perform search on attributes that are an array of numbers (NUMERIC).

127.0.0.1:6379> JSON.SET article:1 $ '{"title": ["2coffee", "hicoffee"], "content": "Hello Developers - What to Drink and Code Today?", "view": [0, 1, 2]}'
127.0.0.1:6379> FT.SEARCH article '@view:[0 1]'

The syntax above will return records with any value in view satisfying >= 0 and <= 1.

There are many other syntaxes to support indexing and searching documents, which you can explore in the official documentation.

Summary

If you are familiar with working with NoSQL, RedisJSON provides a convenient way to handle JSON data without worrying about data inconsistency. SQL databases have long integrated JSON data types, giving us more options in organizing data.

RedisJSON seamlessly integrates with RediSearch for indexing and searching, providing both storage convenience and the powerful full-text search capabilities of RediSearch and the speed of Redis. I'm using this trio of tools, what are your plans for upcoming projects? :D

Premium
Hello

The secret stack of Blog

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

As a developer, are you curious about the technology secrets or the technical debts of this blog? All secrets will be revealed in the article below. What are you waiting for, click now!

View all

Subscribe to receive new article notifications

or
* The summary newsletter is sent every 1-2 weeks, cancel anytime.

Comments (3)

Leave a comment...
Avatar
Ẩn danh2 years ago
Hơi xi đa thằng redisearch này cú pháp các thứ hơi khó dùng
Reply
Avatar
Nguyễn Quang Tú2 years ago
Thằng này mà có orm thì ngon chứ mấy lib query raw mệt vãi
Reply
Avatar
Trần Ngọc Hải2 years ago
Redisearch có vẻ mạnh nhỉ thấy ad giới thiệu suốt
Reply
Avatar
Xuân Hoài Tống2 years ago
Đúng rồi nhanh mà nhẹ đó bạn
Scroll or click to go to the next page