Skip to content

Commit f87c93d

Browse files
oshadminermiller
andauthored
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>
1 parent c4483f9 commit f87c93d

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

docs/docs/path.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,23 @@ You can use an array slice to select a range of elements from an array. This exa
154154
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\"]"
155155
```
156156

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.
158158

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:
160174

161175
```sh
162176
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
170184
"[\"Noise-cancelling Bluetooth headphones\",\"Wireless earbuds\",\"Wireless keyboard\"]"
171185
```
172186

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"`:
176188

177189
```sh
178190
127.0.0.1:6379> JSON.GET store '$.inventory.keyboards[?(@.connection =~ "(?i)usb")]'
179191
"[{\"id\":22346,\"name\":\"USB-C keyboard\",\"description\":\"Wired USB-C keyboard\",\"wireless\":false,\"connection\":\"USB-C\",\"price\":29.99,\"stock\":30,\"free-shipping\":false}]"
180192
```
181-
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.
182194

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`:
184196

185197
```sh
186198
127.0.0.1:6379> JSON.SET store '$.inventory.keyboards[0].regex_pat' '"(?i)bluetooth"'
@@ -189,7 +201,7 @@ OK
189201
OK
190202
```
191203

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:
193205

194206
```sh
195207
127.0.0.1:6379> JSON.GET store '$.inventory.keyboards[?(@.connection =~ @.regex_pat)]'

0 commit comments

Comments
 (0)