[英]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.