Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 34 additions & 34 deletions docs/indexing_JSON.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Indexing JSON documents

In addition to (JSON) documents store, it is also possible to index JSON documents, gaining full-text search capabilities and document retrieving based on their content. To do so, you must install both modules, RedisJSON and RediSearch.
In addition to storing JSON documents, you can also index them using the RediSearch module. This enables full-text search capabilities and document retrieval based on their content. To use this feature, you must install two modules: RedisJSON and RediSearch.

## Prerequisites

What do you need to start indexing JSON documents?

- Redis 6.x or later
- RediJSON 2.0 or later
- RedisJSON 2.0 or later
- RediSearch 2.2 or later

## How to index JSON documents

Let's start by creating an index.
This section shows how to create an index.

We can now specify `ON JSON` to inform RediSearch that we want to index JSON documents.
You can now specify `ON JSON` to inform RediSearch that you want to index JSON documents.

Then, on the `SCHEMA` part, you can provide JSONPath expressions.
For the `SCHEMA`, you can provide JSONPath expressions.
The result of each `JSON Path` expression is indexed and associated with a logical name (`attribute`).
Use the attribute name in the query part.
Use the attribute name in the query.

Here is the basic syntax for indexing a JSON document:

Expand All @@ -28,15 +28,15 @@ And here's a concrete example:

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

Note: The attribute is optional in `FT.CREATE`, but it is required to use attributes in attribute modifiers in the query of `FT.SEARCH` and `FT.AGGREGATE`, and avoid using JSON Path expressions, which are not fully supported by the query parser.
Note: The attribute is optional in `FT.CREATE`, but `FT.SEARCH` and `FT.AGGREGATE` queries require attribute modifiers. You should also avoid using JSON Path expressions, which are not fully supported by the query parser.

## Adding JSON document to the index
## Adding a JSON document to the index

As soon as the index is created, any pre-existing JSON document or any new JSON document added or modified is automatically indexed.
As soon as the index is created, any pre-existing JSON document or any new JSON document, added or modified, is automatically indexed.

We are free to use any writing command from the RedisJSON module (`JSON.SET`, `JSON.ARRAPPEND`, etc).
You can use any write command from the RedisJSON module (`JSON.SET`, `JSON.ARRAPPEND`, etc.).

In this example we are going to use the following JSON document:
This example uses the following JSON document:

```JSON
{
Expand All @@ -49,21 +49,21 @@ In this example we are going to use the following JSON document:
}
```

We can use `JSON.SET` to store in our database:
Use `JSON.SET` to store the document in the database:

```sql
JSON.SET myDoc $ '{"user":{"name":"John Smith","tag":"foo,bar","hp":1000, "dmg":150}}'
```

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.
Any subsequent query that matches the indexed content will return the document.

## Searching

To search for documents, we use the [FT.SEARCH](Commands.md#FT.SEARCH) commands.
We can search for any attribute mentioned in the schema.
To search for documents, use the [FT.SEARCH](Commands.md#FT.SEARCH) commands.
You can search any attribute mentioned in the schema.

Following our example, let's find our user called `John`:
Following our example, find the user called `John`:

```sql
FT.SEARCH userIdx '@name:(John)'
Expand All @@ -75,9 +75,9 @@ FT.SEARCH userIdx '@name:(John)'

## Field projection

We just saw that, by default, `FT.SEARCH` returns the whole document.
`FT.SEARCH` returns the whole document by default.

We can also return only specific attributes (here `name`):
You can also return only a specific attribute (`name` for example):

```sql
FT.SEARCH userIdx '@name:(John)' RETURN 1 name
Expand All @@ -89,9 +89,9 @@ FT.SEARCH userIdx '@name:(John)' RETURN 1 name

### Projecting using JSON Path expressions

The `RETURN` parameter also accepts a `JSON Path expression` letting us extract any part of the JSON document.
The `RETURN` parameter also accepts a `JSON Path expression` which lets you extract any part of the JSON document.

In this example, we return the result of the JSON Path expression `$.user.hp`.
The following example returns the result of the JSON Path expression `$.user.hp`.

```sql
FT.SEARCH userIdx '@name:(John)' RETURN 1 $.user.hp
Expand All @@ -115,9 +115,9 @@ FT.SEARCH userIdx '@name:(John)' RETURN 3 $.user.hp AS hitpoints

### Highlighting

We can highlight any attribute as soon as it is indexed using the TEXT type.
You can highlight any attribute as soon as it is indexed using the TEXT type.

In FT.SEARCH we have to explicitly set the attributes in the `RETURN` parameter and the `HIGHLIGHT` parameters.
For FT.SEARCH, you have to explicitly set the attributes in the `RETURN` parameter and the `HIGHLIGHT` parameters.

```sql
FT.SEARCH userIdx '@name:(John)' RETURN 1 name HIGHLIGHT FIELDS 1 name TAGS '<b>' '</b>'
Expand All @@ -132,7 +132,7 @@ FT.SEARCH userIdx '@name:(John)' RETURN 1 name HIGHLIGHT FIELDS 1 name TAGS '<b>
[Aggregation](Aggregations.md) is a powerful feature. You can use it to generate statistics or build facet queries.
The LOAD parameter accepts JSON Path expressions. Any value (even not indexed) can be used in the pipeline.

In this example, we are loading two numeric values from the JSON document applying a simple operation.
This example loads two numeric values from the JSON document applying a simple operation.

```sql
FT.AGGREGATE userIdx '*' LOAD 6 $.user.hp AS hp $.user.dmg AS dmg APPLY '@hp-@dmg' AS points
Expand All @@ -147,7 +147,7 @@ FT.AGGREGATE userIdx '*' LOAD 6 $.user.hp AS hp $.user.dmg AS dmg APPLY '@hp-@dm

## Current indexing limitations

### It is not possible to index JSON objects and JSON arrays.
### It is not possible to index JSON objects or 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.
Expand All @@ -165,21 +165,21 @@ Given the following document:
}
```

If we want to index the string values part of the array under the `address` key, we must create two fields:
Before you can index the array under the `address` key, you have to create two fields:

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

We can now index the document:
You can now index the document:

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

We can now search in the address:
You can now search in the address:

```sql
FT.SEARCH orgIdx "suite 250"
Expand All @@ -189,10 +189,10 @@ FT.SEARCH orgIdx "suite 250"
2) "{\"name\":\"Headquarters\",\"address\":[\"Suite 250\",\"Mountain View\"],\"cp\":\"CA 94040\"}"
```

### JSON strings and numbers as to be indexed as TEXT and NUMERIC
### Index JSON strings and numbers as TEXT and NUMERIC

- JSON Strings can only be indexed as TEXT, TAG, and GEO (using the correct syntax).
- JSON numbers can only be indexed as NUMERIC.
- You can only index JSON strings as TEXT, TAG, or GEO (using the correct syntax).
- You can only index JSON numbers as NUMERIC.
- JSON booleans can only be indexed as TAG.
- NULL values are ignored.

Expand All @@ -203,9 +203,9 @@ FT.CREATE orgIdx ON JSON SCHEMA $.cp[0] AS cp TAG SORTABLE
(error) On JSON, cannot set tag field to sortable - cp
```

With hashes, SORTABLE can be used (as a side effect) to improve the performance of FT.AGGREGATE on TAGs.
This is possible because the value in the hash is a string. Eg.: "foo,bar".
With hashes, you can use SORTABLE (as a side effect) to improve the performance of FT.AGGREGATE on TAGs.
This is possible because the value in the hash is a string, such as "foo,bar".

With JSON it is possible to index an array of string values.
With JSON, you can index an array of strings.
Because there is no valid single textual representation of those values,
there is no way for RediSearch to know how to sort the result.
there is no way for RediSearch to know how to sort the result.
34 changes: 17 additions & 17 deletions docs/path.md
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
# RedisJSON Path

Since there does not exist a standard for path syntax, RedisJSON implements its own. RedisJSON's syntax is a subset of common best practices and resembles [JSONPath](http://goessner.net/articles/JsonPath/) not by accident.
Since no standard for path syntax exists, RedisJSON implements its own. RedisJSON's syntax is based on common best practices and intentionally resembles [JSONPath](http://goessner.net/articles/JsonPath/).

Two query syntaxes are currently supported . One is a legacy from the first version of RedisJSON. Describe below as the `legacy path syntax`.
RedisJSON currently supports two query syntaxes: JSONPath syntax and a legacy path syntax from the first version of RedisJSON.

RedisJSON decides which syntax to use depending on the first character of the path query. If the query starts with the character `$` it is considered as a JSONPath query. Otherwise it is interpreted as a legacy path syntax.
RedisJSON decides which syntax to use depending on the first character of the path query. If the query starts with the character `$`, it uses JSONPath syntax. Otherwise, it defaults to legacy path syntax.

## JSONPath support (RedisJSON v2)

RedisJSON 2.0 introduces the support of [JSONPath](http://goessner.net/articles/JsonPath/). It follows the syntax described by Goessner in his article.
RedisJSON 2.0 introduces [JSONPath](http://goessner.net/articles/JsonPath/) support. It follows the syntax described by Goessner in his article.

JSONPath query can resolve to several locations in the JSON documents. In this case, the JSON commands are applying the operation to every possible location. This is a major improvement over the legacy support, which was operating only on the first path.
A JSONPath query can resolve to several locations in the JSON documents. In this case, the JSON commands apply the operation to every possible location. This is a major improvement over the legacy query, which only operates on the first path.

Notice that the structure of the command response is most of the time different when using JSONPath. These two behaviors are described in the [Commands](/commands) page.
Notice that the structure of the command response often differs when using JSONPath. See the [Commands](/redisjson/commands) page for more details.

The new syntax supports the bracket notation, allowing the use of special characters like colon ":" of whitespaces in key names.
The new syntax supports bracket notation, which allows the use of special characters like colon ":" or whitespace in key names.

## Legacy Path syntax (RedisJSON v1)

The first version of RedisJSON came with the following implementation. It is still supported in RedisJSON v2.
The first version of RedisJSON had the following implementation. It is still supported in RedisJSON v2.

Paths always begin at the root of a RedisJSON value. The root is denoted by the period character (`.`). For paths referencing the root's children, prefixing the path with the root is optional.
Paths always begin at the root of a RedisJSON value. The root is denoted by a period character (`.`). For paths that reference the root's children, it is optional to prefix the path with the root.

Dotted- and square-bracketed, single-or-double-quoted-child notation are both supported for object keys, so the following paths all refer to _bar_, child of _foo_ under the root:
RedisJSON supports both dot notation and bracket notation for object key access. The following paths all refer to _bar_, which is a child of _foo_ under the root:

* `.foo.bar`
* `foo["bar"]`
* `['foo']["bar"]`

Array elements are accessed by their index enclosed by a pair of square brackets. The index is 0-based, with 0 being the first element of the array, 1 being the next element and so on. These offsets can also be negative numbers, indicating indices starting at the end of the array. For example, -1 is the last element in the array, -2 the penultimate, and so on.
To access an array element, enclose its index within a pair of square brackets. The index is 0-based, with 0 being the first element of the array, 1 being the next element, and so on. You can use negative offsets to access elements starting from the end of the array. For example, -1 is the last element in the array, -2 is the second to last element, and so on.

## A note about JSON key names and path compatibility
## JSON key names and path compatibility

By definition, a JSON key can be any valid JSON String. Paths, on the other hand, are traditionally based on JavaScript's (and in Java in turn) variable naming conventions. Therefore, while it is possible to have RedisJSON store objects containing arbitrary key names, accessing these keys via a path will only be possible if they respect these naming syntax rules:
By definition, a JSON key can be any valid JSON string. Paths, on the other hand, are traditionally based on JavaScript's (and Java's) variable naming conventions. Therefore, while it is possible to have RedisJSON store objects containing arbitrary key names, you can only access these keys via a path if they conform to these naming syntax rules:

1. Names must begin with a letter, a dollar (`$`) or an underscore (`_`) character
2. Names can contain letters, digits, dollar signs and underscores
1. Names must begin with a letter, a dollar sign (`$`), or an underscore (`_`) character
2. Names can contain letters, digits, dollar signs, and underscores
3. Names are case-sensitive

## Time complexity of path evaluation

The complexity of searching (navigating to) an element in the path is made of:
The time complexity of searching (navigating to) an element in the path is calculated from:

1. Child level - every level along the path adds an additional search
2. Key search - O(N)<sup>&#8224;</sup>, where N is the number of keys in the parent object
3. Array search - O(1)

This means that the overall time complexity of searching a path is _O(N*M)_, where N is the depth and M is the number of parent object keys.

<sup>&#8224;</sup> while this is acceptable for objects where N is small, access can be optimized for larger objects, and this is planned for a future version.
<sup>&#8224;</sup> while this is acceptable for objects where N is small, access can be optimized for larger objects. This optimization is planned for a future version.