Table of contents
1.
Introduction
2.
Searching and Indexing the Json in redis
2.1.
Indexing
2.1.1.
Indexing JSON arrays with tags 
2.1.2.
Current indexing limitations
2.2.
Searching
2.3.
Field projection
3.
Frequently asked questions
3.1.
Could we at any point store JSON in Redis?
3.2.
How might you productively store JSON in Redis?
3.3.
How would I record a JSON document?
3.4.
What is a record in Redis?
3.5.
How enormous could a Redis at any point esteem be?
4.
Conclusion
Last Updated: Mar 27, 2024

Searching and Indexing the Json in redis

Author Adya Tiwari
0 upvote

Introduction

RedisJSON is a Redis module that gives JSON support in Redis. RedisJSON lets your store update and recover JSON values in Redis, similarly to you would with some other Redis information type. RedisJSON likewise works flawlessly with RediSearch to allow you to file and question your JSON archives. As well as putting away JSON archives, you can again record them utilizing the RediSearch module. This empowers full-text search abilities and reports recovery given their substance. You should introduce two modules to use this element: RedisJSON and RediSearch.

Searching and Indexing the Json in redis

To start indexing JSON documents, one must have 

  • Redis 6. x or later
  • RedisJSON 2.0 or later
  • RediSearch 2.2 or later

Download and introduce Redis on the off chance that you don't, as of now, have it on your OS.

Utilize the redis-cli - - rendition order to affirm that you additionally have the intuitive Redis order line interface.

Indexing

You can now determine ON JSON to illuminate RediSearch that you need to record JSON archives.

For the SCHEMA, you can give JSONPath articulations. The consequence of each JSON Path articulation is ordered and connected with a coherent name (trait). Utilize the characteristic name in the question.

Here is the essential linguistic structure for ordering a JSON record:

FT.CREATE userIdx ON JSON SCHEMA $.user.name AS name TEXT $.user.tag AS country TAG

Note: The characteristic is discretionary in FT.CREATE, yet FT.SEARCH and FT.AGGREGATE questions require property modifiers. Likewise, you ought to try not to utilize JSON Path articulations, which are not entirely upheld by the inquiry parser.

Any prior JSON report or any new JSON archive, added or changed, is naturally listed when the record is made.

You can utilize any compose order from the RedisJSON module (JSON.SET, JSON.ARRAPPEND, etc.).

Example:

{
  "user": {
    "name": "John Smith",
    "tag": "foo,bar",
    "hp": "1000",
    "dmg": "150"
  }
}

Since ordering is coordinated, the record will be noticeable on the file when the JSON.SET 

order returns. Any resulting question that matches the listed substance will return the report.

Indexing JSON arrays with tags 

It is feasible to list scalar string and boolean qualities in JSON clusters by utilizing the special case administrator in the JSON Path. For instance, if you were ordering blog entries, you could have a field called labels, a variety of labels that apply to the blog entry.

{
   "title":"Using RedisJson is Easy and Fun",
   "tags":["redis","json","redisjson"]
}

You can apply a list to the labels field by determining the JSON Path $.tags.* in your construction creation:

FT.CREATE blog-idx ON JSON PREFIX 1 Blog: SCHEMA $.tags.* AS tags TAG

You would then set a blog entry as you would some other JSON record:

JSON.SET Blog:1 . '{"title":"Using RedisJson is Easy and Fun", "tags":["redis","json","redisjson"]}'

Lastly you can look through utilizing the commonplace label looking through linguistic structure:

127.0.0.1:6379> FT.SEARCH blog-idx "@tags:{redis}"
1) (integer) 1
2) "Blog:1"
3) 1) "$"
   2) "{\"title\":\"Using RedisJson is Easy and Fun\",\"tags\":[\"redis\",\"json\",\"redisjson\"]}"

Current indexing limitations

JSON clusters must be listed in TAG identifiers.

It is simply conceivable to list a variety of strings or booleans in a TAG identifier. Different sorts (numeric, geo, invalid) are not upheld.

It is unimaginable to expect to file JSON objects.

A JSONPath articulation should return a solitary scalar worth (string or number) to be recorded.

On the off chance that the JSONPath articulation returns an article, it will be disregarded.

Anyway, ordering the strings in isolated attributes is conceivable.

Given the accompanying archive:

{
  "name": "Headquarters",
  "address": [
    "Suite 250",
    "Mountain View"
  ],
  "cp": "CA 94040"
}

Before you can record the cluster under the location key, you need to make two fields:

FT.CREATE orgIdx ON JSON SCHEMA $.address[0] AS a1 TEXT $.address[1] AS a2 TEXT
OK

You can now record the report:

JSON.SET org:1 $ '{"name": "Headquarters","address": ["Suite 250","Mountain View"],"cp": "CA 94040"}'
OK

You can now look through in the location:

FT.SEARCH orgIdx "suite 250"
1) (integer) 1
2) "org:1"
3) 1) "$"
   2) "{\"name\":\"Headquarters\",\"address\":[\"Suite 250\",\"Mountain View\"],\"cp\":\"CA 94040\"}"

Record JSON strings and numbers as TEXT and NUMERIC

  • You can list JSON strings as TEXT, TAG, or GEO (utilizing the correct punctuation).
  • You can list JSON numbers as NUMERIC.
  • JSON booleans must be recorded as TAG.
  • Invalid qualities are overlooked.

SORTABLE isn't upheld on TAG

FT.CREATE orgIdx ON JSON SCHEMA $.cp[0] AS cp TAG SORTABLE
(error) On JSON, cannot set tag field to sortable - cp

With hashes, you can utilize SORTABLE (as a secondary effect) to work on the exhibition of FT.AGGREGATE on TAGs. This is conceivable because the worth in the hash is a string, for example, "foo, bar."

Searching

To look for records, utilize the FT.SEARCH order. You can look through any trait referenced in the pattern.

Following our model, find the client called John:

FT.SEARCH userIdx '@name:(John)'
1) (integer) 1
2) "myDoc"
3) 1) "$"
   2) "{\"user\":{\"name\":\"John Smith\",\"tag\":\"foo,bar\",\"hp\":1000,\"dmg\":150}}"
Indexing JSON arrays with tags

Field projection

FT.SEARCH returns the entire record, of course.

You can likewise return just a particular trait (name for instance):

FT.SEARCH userIdx '@name:(John)' RETURN 1 name
1) (integer) 1
2) "myDoc"
3) 1) "name"
   2) "\"John Smith\""

Projecting utilizing JSON Path articulations

The RETURN boundary likewise acknowledges a JSON Path articulation which allows you to remove any piece of the JSON report.

The accompanying model returns the consequence of the JSON Path articulation $.user.hp.

FT.SEARCH userIdx '@name:(John)' RETURN 1 $.user.hp
1) (integer) 1
2) "myDoc"
3) 1) "$.user.hp"
   2) "1000"

Note that the property name is the JSON articulation itself: 3) 1) "$.user.hp"

Utilizing the AS choice, it is additionally conceivable to nom de plume the brought property back.

FT.SEARCH userIdx '@name:(John)' RETURN 3 $.user.hp AS hitpoints
1) (integer) 1
2) "myDoc"
3) 1) "hitpoints"
   2) "1000"

Highlighting

You can feature any characteristic when it is filed utilizing the TEXT type.

For FT.SEARCH, you need to set the traits in the RETURN boundary and the HIGHLIGHT boundaries expressly.

FT.SEARCH userIdx '@name:(John)' RETURN 1 name HIGHLIGHT FIELDS 1 name TAGS '<b>' '</b>'
1) (integer) 1
2) "myDoc"
3) 1) "name"
   2) "\"<b>John</b> Smith\""

With JSON, you can record a variety of strings. Since there is no substantial single literary portrayal of those qualities, it is impossible for RediSearch to know how to sort the outcome.

Frequently asked questions

Could we at any point store JSON in Redis?

You can store JSON in Redis as a plain string in a committed key (or part/worth of a set/list) or a hash structure.

How might you productively store JSON in Redis?

RedisJSON stores the information in a paired organization, eliminating the capacity above from JSON and giving speedier admittance to components without de-/re-serialization times. To utilize RedisJSON, you want to introduce it in your Redis server or empower it in your Redis Enterprise data set.

How would I record a JSON document?

You can record JSON information as you would any knowledge of the sort you use to store it. Specifically, you can utilize a B-tree list or a bitmap file for SQL/JSON work json_value, and you can utilize a bitmap record for SQL/JSON conditions is JSON, isn't JSON, and json_exists.

What is a record in Redis?

Reasonably, Redis depends on the key-esteem data set worldview. Each piece of information is related with a key, either straightforwardly or by implication.

How enormous could a Redis at any point esteem be?

All string values are restricted to 512 MiB. This is as far as possible you presumably care generally about. Alter: Because keys in Redis are strings, the most extreme key size is 512 MiB.

Conclusion

In this article, we have learned that you can file JSON information as you would any knowledge of the sort you use to store it. Specifically, you can utilize a B-tree list or a bitmap file for SQL/JSON work json_value, and you can utilize a bitmap record for SQL/JSON conditions is JSON , isn't json , and json_exists . As well as putting away JSON archives, you can likewise record them utilizing the RediSearch module. This empowers full-text search capacities and archive recovery in light of their substance. To use this component, you should introduce two modules: RedisJSON and RediSearch.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in pygameCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc., you must look at the problems, interview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass