RedisJSON 2.0 Private Preview was announced for the first time during RedisConf 2021. With this newer version, RedisJSON will fully support JSONPath expressions and Active-Active geo-distribution. The Active-Active implementation is based on Conflict-free Replicated Data-Types (CRDT).
Prior to v2.2, RediSearch only supported Redis hashes. Going forward, RediSearch will support RedisJSON documents. This opens a powerful new set of document-based indexing use cases. In addition, RediSearch now provides query profiling. This will empower developers to understand and optimize their RediSearch queries, increasing their developer experience.
RediSearch has been providing indexing and search capabilities on hashes. Under the hood, RedisJSON 2.0 exposes an internal public API. Internal, because this API is exposed to other modules running inside a Redis node. Public, because any module can consume this API. So does RediSearch 2.2 ! In addition to indexing Redis hashes, RediSearch also indexes JSON. To index JSON, you must use the RedisJSON module.
By exposing its capabilities to other modules, RedisJSON gives RediSearch the ability to index JSON documents so users can now find documents by indexing and querying the content. These combined modules give you a powerful, low latency, JSON-oriented document database!
- Redis 6.x or later
- RediSearch 2.2 or later
- RediJSON 2.0 or later
Please note that this publicly available Docker Image is a community preview and doesn't support Active-Active.This Docker image contains Redis together with the main Redis modules, including RediSearch and RedisJSON. You'll need the preview tag of the image, which you can access as follows:
Let's start by creating an index.
We can now specify ON JSON to inform RediSearch that we want to index JSON documents. Then, on the SCHEMA part, you can provide JSONPath expressions. The result of each JSON Path expression is indexed and associated with a logical name ( attribute ). This attribute (previously called field ) is used in the query part.
This is the basic syntax for indexing a JSON document:Syntax:
We should first populate the database with a JSON document using the JSON.SET command. In our example we are going to use the following JSON document:
Because indexing is synchronous, the document will be visible on the index as soon as the JSON.SET command returns. Any subsequent query matching the indexed content will return the document
This new version includes a comprehensive support of JSONPath. It is now possible to use all the expressiveness of JSONPath expressions.
To create a new index, we use the FT.CREATE command. The schema of the index now accepts JSONPath expressions. The result of the expression is indexed and associated with an attribute (here: title).
We can now do a search query and find our JSON document using FT.SEARCH:Command:
We just saw that, by default, FT.SEARCH returns the whole document. We can also return only specific attribute (here name)
The RETURN parameter also accepts a JSON Path expression which let us extract any part of the JSON document. In this example, we return the result of the JSON Path expression $.user.hp .Command:
Please Note: It is not possible to index JSON object and JSON arrays. To be indexed, a JSONPath expression must return a single scalar value (string or number). If the JSONPath expression returns an object or an array, it will be ignored.
Given the following document:
If we want to index the array under the address key, we have to create two fields:Command:
It's time to index the document:Command:
We can now search in the address:Command: