I am new to RedisSearch. I have a Java client. What is the easiest way to parse this sample FT.SEARCH result into JSON or POJO or something more useful?
Sample result from FT.SEARCH
(actually a string):
[ 3, movie_json: 1, [$, { "id": 1, "title": "Game of Thrones" } ], movie_json: 3, [$, { "id": 3, "title": "Looking for Sugarman" } ], movie_json: 2, [$, { "id": 2, "title": "Inception" } ]]
Something like this would be useful:
{"count": 3,"docs": [ { "id": 1, "title": "Game of Thrones" }, { "id": 3, "title": "Looking for Sugarman" }, { "id": 2, "title": "Inception" } ]}
The most obvious is a RegEx matcher as below (I am no regex expert).
This is the code generated by the https://regex101.com/
site where I can get the right groups on their site as long as I use a global
flag - but it seems that Java doesn't have a GLOBAL pattern / flag! Is that true?
The code the site generated is below and sure enough matcher.find() shows no match, presumably due to the absence of the global flag.
final String regex = "(?<=\\[\\$, ).*?(?= \\])";final String string = respContent; // The rediSearch result string shown abovefinal Pattern pattern = Pattern.compile(regex);final Matcher matcher = pattern.matcher(string);while (matcher.find()) { System.out.println("Full match: " + matcher.group(0)); for (int i = 1; i <= matcher.groupCount(); i++) { System.out.println("Group " + i +": " + matcher.group(i)); }}
I could use the String.split()
dance too.
However, is there an existing solution that is probably more robust for multiple FT.SEARCH results use-cases?
I imagined someone would have written a RedisSearch results parser by now but I cannot find one.
Thanks,Murray