sewenew/redis-plus-plus

[QUESTION] How to handle the reply of FT.SEARCH

Closed this issue · 4 comments

Command: FT.SEARCH "idx:joined_ids_tmp1" "*"
Result:

  1. (integer) 8
  2. "joined_ids_tmp1:1_163_0"
    1. "$"
    2. "{"person_ois":"1_163","id":"1_174"}"
  3. "joined_ids_tmp1:1_163_1"
    1. "$"
    2. "{"person_ois":"1_163","id":"1_189"}"
      .............
  4. "joined_ids_tmp1:2_199_0"
    1. "$"
    2. "{"person_ois":"2_199","id":"2_217"}"
  5. "joined_ids_tmp1:3_55_0"
    1. "$"
    2. "{"person_ois":"3_55","id":"3_140"}"

The result schema:

  • First line: number of documents (integer)
  • Remain lines: returned documents (A document is a map)

The problem: If the number of documents is fixed, we can use tuple to handle the results. But we don't know how many documents will be returned.

Thank you!

I have to say it's really an annoying response...

However, you can use the variant feature to parse it.

using Result = vector<variant<long long, string, unordered_map<string, string>>>;

auto res = redis.command<Result>("FT.SEARCH", "idx:joined_ids_tmp1", "*");
if (res.empty()) {
    // empty result.
} else {
    auto num = std::get<0>(res[0]);
    for (auto idx = 0; idx < num; ++idx) {
        auto key = std::get<string>(res[idx * 2 + 1]);
        auto val = std::get<unordered_map<string, string>>(res[idx * 2 + 2]);
    }
}

Thank you very much!
I can also parse this response by this way:

using Var = Variant<long long, string, std::tuple<string, string>>;
auto res = redis.command<std::vector<Var>>("FT.SEARCH", "idx:joined_ids_tmp1", "*");

I'd suggest you use unordered_map instead of tuple, since the result might contain multiple key-value pairs as the attributes of your document.

Regards

Since there's no update, I'll close this issue.

Regards