[英]Java ResiSearch FT.SEARCH results to json
我是 RedisSearch 的新手。 我有一个 Java 客户端。 将此示例 FT.SEARCH 结果解析为 JSON 或 POJO 或更有用的最简单方法是什么?
FT.SEARCH
的示例结果(实际上是一个字符串):
[
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" } ]
]
像这样的东西会很有用:
{
"count": 3,
"docs": [
{ "id": 1, "title": "Game of Thrones" },
{ "id": 3, "title": "Looking for Sugarman" },
{ "id": 2, "title": "Inception" }
]
}
最明显的是下面的正则表达式匹配器(我不是正则表达式专家)。
这是由https://regex101.com/
站点生成的代码,只要我使用global
标志,我就可以在他们的站点上获得正确的组 - 但似乎 Java 没有 GLOBAL 模式/标志? 真的吗?
该站点生成的代码如下,并且肯定 matcher.find() 显示不匹配,可能是由于缺少全局标志。
final String regex = "(?<=\\[\\$, ).*?(?= \\])";
final String string = respContent; // The rediSearch result string shown above
final 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));
}
}
我也可以使用String.split()
舞蹈。
但是,是否存在一种对多个 FT.SEARCH 结果用例可能更稳健的解决方案?
我想现在有人会写一个 RedisSearch 结果解析器,但我找不到。
谢谢,默里
Quarkus 的高级 Redis API 仅将普通的 Redis 命令公开为一组 Z93F725A07423FED21。 要处理 Redis 扩展,您始终可以参考低级 API: https://quarkus.io/guides/redis-reference
一旦您选择了低级 API,您实际上就是在使用 Quarkus 使用的底层驱动程序。 这是 Vert.x Redis 客户端。
在此模式下,您可以使用任何 Redis 扩展并直接使用 JSON,例如:
// set a JSON value
lowLevelClient
.send(cmd(Command.create("JSON.SET")).arg("foo").arg(".").arg("\"bar\""))
.compose(response -> {
// OK
// get a JSON value
return lowLevelClient.send(cmd(Command.create("JSON.GET")).arg("foo"));
})
.compose(response -> {
// verify that it is correct
should.assertEquals("\"bar\"", response.toString());
// do another call...
return lowLevelClient.send(cmd(Command.create("JSON.TYPE")).arg("foo").arg("."));
})
.compose(response -> {
should.assertEquals("string", response.toString());
return Future.succeededFuture();
})
.onFailure(should::fail)
.onSuccess(v -> {
test.complete();
});
虽然此模式更加冗长,但它可以让您完全控制您正在使用的 Redis 扩展。
如果响应可以映射到 JSON 或者已经是 JSON,则可以直接从其持有者获取内容,而无需解析响应,例如:
response.getKeys(); // returns the set of keys
response.get("key1"); // returns the JSON value for key "key1"
response.get(0); // returns the JSON value for array index 0
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.