'Redisearch unable to search nested JSON array

I am experimenting with use redis-search + redis-json. But facing problem querying on Nested Json with array. I created following JSON

{
   "src":{
      "location":[
         {
            "ref":"/uuid/1/xyz",
            "key":"zone"
         },
         {
            "ref":"/uuid/2/abc",
            "key":"zone"
         }
      ]
   }
}
127.0.0.1:6379> JSON.SET 300:100 $ '{"src":{"location":[{"ref":"/uuid/1/xyz", "key":"zone"},{"ref":"/uuid/2/abc", "key":"zone"}]}}'
JSON.GET 300:100
"{\"src\":{\"location\":[{\"ref\":\"/uuid/1/xyz\",\"key\":\"zone\"},{\"ref\":\"/uuid/2/abc\",\"key\":\"zone\"}]}}"

127.0.0.1:6379> JSON.GET 300:100 $.src.location[*]
"[[{\"ref\":\"/uuid/1/xyz\",\"key\":\"zone\"},{\"ref\":\"/uuid/2/abc\",\"key\":\"zone\"}]]"

Created Index

127.0.0.1:6379> FT.CREATE 300:idx6 ON JSON SCHEMA $.src.location[*].ref as ref TAG

Tried searching with Tag

127.0.0.1:6379> FT.SEARCH 300:idx6 @ref:{/uuid/1/xyz}
1) (integer) 0

But it's not working. But if I replace / in the ref with _ I get the result

127.0.0.1:6379> JSON.SET 300:100 $ '{"src":{"location":[{"ref":"_uuid_1_xyz", "key":"zone"},{"ref":"_uuid_2_abc", "key":"zone"}]}}'
127.0.0.1:6379> FT.CREATE 300:idx7 ON JSON SCHEMA $.src.location[*].ref as ref TAG
OK
127.0.0.1:6379> FT.SEARCH 300:idx7 @ref:{_uuid_1_xyz}

1) (integer) 1
2) "300:100"
3) 1) "$"
   2) "{\"src\":{\"location\":[{\"ref\":\"_uuid_1_xyz\",\"key\":\"zone\"},{\"ref\":\"_uuid_2_abc\",\"key\":\"zone\"}]}}"

Is there any issue using \ or how do I escape it ?



Solution 1:[1]

You need to escape the punctuation in the query, e.g.,

FT.SEARCH 300:idx6 @ref:{\/uuid\/1\/xyz}

See Including punctuation in tags

Which redis version are you using?

It is working when using Redis Stack docker image, e.g.,

docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest

(currently redis 6.2.6, RediSearch 2.2.10, RedisJSON 2.0.7)

Running the following commands in redis-cli

127.0.0.1:6379> JSON.SET 300:100 $ '{"src":{"location":[{"ref":"/uuid/1/xyz", "key":"zone"},{"ref":"/uuid/2/abc", "key":"zone"}]}}'
OK
127.0.0.1:6379> FT.CREATE 300:idx6 ON JSON SCHEMA $.src.location[*].ref as ref TAG
OK

And escaping the query does not require to replace /:

127.0.0.1:6379> FT.SEARCH 300:idx6 @ref:{\/uuid\/1\/xyz}
1) (integer) 1
2) "300:100"
3) 1) "$"
   2) "{\"src\":{\"location\":[{\"ref\":\"/uuid/1/xyz\",\"key\":\"zone\"},{\"ref\":\"/uuid/2/abc\",\"key\":\"zone\"}]}}"
127.0.0.1:6379> 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1