You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Documentation: Add details on jsonpath filter (RedisJSON#858)
* Add details on jsonpath filter
* fix local path and some edits
* Apply suggestions from review by Nermina
Co-authored-by: Nermina Miller <102551568+nermiller@users.noreply.github.com>
Co-authored-by: Nermina Miller <102551568+nermiller@users.noreply.github.com>
Copy file name to clipboardExpand all lines: docs/docs/path.md
+20-8Lines changed: 20 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -154,9 +154,23 @@ You can use an array slice to select a range of elements from an array. This exa
154
154
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\"]"
155
155
```
156
156
157
-
Filter expressions `?()` let you select JSON elements based on certain conditions. You can use comparison operators (`==`, `!=`, `<`, `<=`, `>`, `>=`, `=~`), logical operators (`&&`, `||`), and parenthesis (`(`, `)`) within these expressions. A filter expression can be applied on an array or on an object, iterating all the elements in the array or all the key-value pair in the object, retrieving only the ones that match the filter condition. The filter condition is using `@` to denote the current array element or the current object. Use `@.key_name` to refer to a specific value. Use `$` to denote the top-level, e.g., `$.top_level_key_name`
157
+
Filter expressions `?()` let you select JSON elements based on certain conditions. You can use comparison operators (`==`, `!=`, `<`, `<=`, `>`, `>=`, `=~`), logical operators (`&&`, `||`), and parenthesis (`(`, `)`) within these expressions. A filter expression can be applied on an array or on an object, iterating over all the **elements** in the array or all the **values**in the object, retrieving only the ones that match the filter condition.
158
158
159
-
For example, this filter only returns wireless headphones with a price less than 70:
159
+
Paths within the filter condition are using the dot notation with either `@` to denote the current array element or the current object value, or `$` to denote the top-level element. For example, use `@.key_name` to refer to a nested value and `$.top_level_key_name` to refer to a top-level value.
160
+
161
+
The comparison operator `=~` is matching a path of a string value on the left side against a regular expression pattern on the right side. For more information, see the [supported regular expression syntax docs](https://docs.rs/regex/latest/regex/#syntax).
162
+
163
+
Non-string values do not match. A match can only occur when the left side is a path of a string value and the right side is either a hard-coded string, or a path of a string value. See [examples](#json-filter-examples) below.
164
+
165
+
The regex match is partial, meaning `"foo"` regex pattern matches a string such as `"barefoots"`.
166
+
To make it exact, use the regex pattern `"^foo$"`.
167
+
168
+
Other JSONPath engines may use regex pattern between slashes, e.g., `/foo/`, and their match is exact.
169
+
They can perform partial matches using a regex pattern such as `/.*foo.*/`.
170
+
171
+
#### JSON Filter examples
172
+
173
+
In the following example, the filter only returns wireless headphones with a price less than 70:
160
174
161
175
```sh
162
176
127.0.0.1:6379> JSON.GET store $..headphones[?(@.price<70&&@.wireless==true)]
@@ -170,17 +184,15 @@ This example filters the inventory for the names of items that support Bluetooth
170
184
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\",\"Wireless keyboard\"]"
171
185
```
172
186
173
-
The comparison operator `=~` is matching a string value of the left-hand side against a regular expression pattern on the right-hand side. The supported regular expression syntax is detailed [here](https://docs.rs/regex/latest/regex/#syntax).
174
-
175
-
For example, this filters only keyboards with some sort of USB connection (notice this match is case-insensitive thanks to the prefix `(?i)` in the regular expression pattern `"(?i)usb"` ):
187
+
This example filters only keyboards with some sort of USB connection using regex match. Notice this match is case-insensitive thanks to the prefix `(?i)` in the regular expression pattern `"(?i)usb"`:
176
188
177
189
```sh
178
190
127.0.0.1:6379> JSON.GET store '$.inventory.keyboards[?(@.connection =~ "(?i)usb")]'
The regular expression pattern can also be taken from a JSON string key on the right-hand side.
193
+
The regular expression pattern can also be specified using a path of a string value on the right side.
182
194
183
-
For example, let's add each keybaord object with a `regex_pat` key:
195
+
For example, let's add each keybaord object with a string value named `regex_pat`:
184
196
185
197
```sh
186
198
127.0.0.1:6379> JSON.SET store '$.inventory.keyboards[0].regex_pat''"(?i)bluetooth"'
@@ -189,7 +201,7 @@ OK
189
201
OK
190
202
```
191
203
192
-
Now we can match against this `regex_pat`key instead of a hard-coded regular expression pattern, and get the keyboard with the `Bluetooth` string in its `connection` key (notice the one with `USB-C`did not match since its regular expression pattern is case-sensitive and the regular expression pattern is using lower case):
204
+
Now we can match against the value of `regex_pat` instead of a hard-coded regular expression pattern, and get the keyboard with the `Bluetooth` string in its `connection` key. Notice the one with `USB-C`does not match since its regular expression pattern is case-sensitive and the regular expression pattern is using lowercase:
193
205
194
206
```sh
195
207
127.0.0.1:6379> JSON.GET store '$.inventory.keyboards[?(@.connection =~ @.regex_pat)]'
0 commit comments