简体   繁体   English

Java ResiSearch FT.SEARCH 结果到 json

[英]Java ResiSearch FT.SEARCH results to json

I am new to RedisSearch.我是 RedisSearch 的新手。 I have a Java client.我有一个 Java 客户端。 What is the easiest way to parse this sample FT.SEARCH result into JSON or POJO or something more useful?将此示例 FT.SEARCH 结果解析为 JSON 或 POJO 或更有用的最简单方法是什么?

Sample result from FT.SEARCH (actually a string): 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" } ]
]

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?这是由https://regex101.com/站点生成的代码,只要我使用global标志,我就可以在他们的站点上获得正确的组 - 但似乎 Java 没有 GLOBAL 模式/标志? 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.该站点生成的代码如下,并且肯定 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));
  }
}

I could use the String.split() dance too.我也可以使用String.split()舞蹈。

However, is there an existing solution that is probably more robust for multiple FT.SEARCH results use-cases?但是,是否存在一种对多个 FT.SEARCH 结果用例可能更稳健的解决方案?

I imagined someone would have written a RedisSearch results parser by now but I cannot find one.我想现在有人会写一个 RedisSearch 结果解析器,但我找不到。

Thanks, Murray谢谢,默里

The high level Redis API for Quarkus only exposes the plain Redis commands as a set of java APIs. Quarkus 的高级 Redis API 仅将普通的 Redis 命令公开为一组 Z93F725A07423FED21。 To handle Redis extensions, you can always refer to the low-level API: https://quarkus.io/guides/redis-reference要处理 Redis 扩展,您始终可以参考低级 API: https://quarkus.io/guides/redis-reference

Once you choose the low-level API, you are, in fact, using the underlying driver that Quarkus uses.一旦您选择了低级 API,您实际上就是在使用 Quarkus 使用的底层驱动程序。 This is Vert.x Redis client.这是 Vert.x Redis 客户端。

In this mode, you can use any Redis extension and work with JSON directly, for example:在此模式下,您可以使用任何 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();
  });

While this mode is much more verbose, it gives you full control to the Redis extension you're using.虽然此模式更加冗长,但它可以让您完全控制您正在使用的 Redis 扩展。

If the response can be mapped to JSON or is JSON already, you can get the content from its holder directly without need to parse the response, for example:如果响应可以映射到 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM