3

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 ?

1 Answer 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> 

Sign up to request clarification or add additional context in comments.

2 Comments

I tried that too. It also didn't work. So I replaced / with _ and then query started working.
Which versions are you using? It is working when using Redis Stack, e.g., using docker image redis/redis-stack:latest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.