[QUESTION] How to handle the reply of FT.SEARCH
vuongnp opened this issue · 4 comments
vuongnp commented
Command: FT.SEARCH "idx:joined_ids_tmp1" "*"
Result:
- (integer) 8
- "joined_ids_tmp1:1_163_0"
-
- "$"
- "{"person_ois":"1_163","id":"1_174"}"
- "joined_ids_tmp1:1_163_1"
-
- "$"
- "{"person_ois":"1_163","id":"1_189"}"
.............
- "joined_ids_tmp1:2_199_0"
-
- "$"
- "{"person_ois":"2_199","id":"2_217"}"
- "joined_ids_tmp1:3_55_0"
-
- "$"
- "{"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!
sewenew commented
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]);
}
}
vuongnp commented
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", "*");
sewenew commented
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
sewenew commented
Since there's no update, I'll close this issue.
Regards