I'm using the modules RedisJSON and RediSearch together to perform search queries on JSON data. For every JSON object, I need to index all the string elements in an array field to be able to get this object by querying one of the strings in the array (i.e. get some book data by searching for one of the authors contained in a string array in the book JSON). However, it seems to be currently not possible. Is there any possible workaround or am I stuck?
Asked
Active
Viewed 2,250 times
1 Answers
3
You need to try the latest version of RediSearch+RedisJSON.
The example from the github issue you are referring to is working for me with RedisJSON 2.0.5 and RediSearch 2.2.5
127.0.0.1:6379> ft.create index on json schema $.names[0:].first as first tag
OK
127.0.0.1:6379> json.set pserson:1 $ '{"names":[{"first": "fname1","last": "lname1"},{"first": "fname2","last": "lname2"},{"first": "fname3", "last": "lname3"}]}'
OK
127.0.0.1:6379> ft.search index @first:{fname1}
1) (integer) 1
2) "pserson:1"
3) 1) "$"
2) "{\"names\":[{\"first\":\"fname1\",\"last\":\"lname1\"},{\"first\":\"fname2\",\"last\":\"lname2\"},{\"first\":\"fname3\",\"last\":\"lname3\"}]}"
127.0.0.1:6379> ft.search index @first:{fname2}
1) (integer) 1
2) "pserson:1"
3) 1) "$"
2) "{\"names\":[{\"first\":\"fname1\",\"last\":\"lname1\"},{\"first\":\"fname2\",\"last\":\"lname2\"},{\"first\":\"fname3\",\"last\":\"lname3\"}]}"
Here is the specific redis-cli info modules
:
module:name=ReJSON,ver=20005,api=1,filters=0,usedby=[search],using=[],options=[handle-io-errors]
module:name=search,ver=20205,api=1,filters=0,usedby=[],using=[ReJSON],options=[handle-io-errors]

Anton
- 3,587
- 2
- 12
- 27
-
As the last comment of the issue stated, storing the array fields as tags isn't possible for some use cases. In my case it's not possible to use tags because I need to perform search on all these array elements at once, so they should stay TEXT fields in the schema. – Elf Dec 29 '21 at 08:32