From dd10a7d0bd58731c91bdaa2eeb1afa0f3194c1ce Mon Sep 17 00:00:00 2001 From: PragmaTwice Date: Sun, 2 Jun 2024 17:06:20 +0900 Subject: [PATCH] feat(search): improve the response format of FT.SEARCH|SEARCHSQL --- src/commands/cmd_search.cc | 34 ++++++++++++++++------------------ src/search/index_manager.h | 2 +- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/commands/cmd_search.cc b/src/commands/cmd_search.cc index 0ca8f5dd7a5..bf6444dc3ae 100644 --- a/src/commands/cmd_search.cc +++ b/src/commands/cmd_search.cc @@ -25,6 +25,7 @@ #include "commands/command_parser.h" #include "search/index_info.h" #include "search/ir.h" +#include "search/plan_executor.h" #include "search/redis_query_transformer.h" #include "search/search_encoding.h" #include "search/sql_transformer.h" @@ -138,6 +139,19 @@ class CommandFTCreate : public Commander { std::unique_ptr index_info_; }; +static void DumpQueryResult(const std::vector &rows, std::string *output) { + output->append(MultiLen(rows.size() * 2 + 1)); + output->append(Integer(rows.size())); + for (const auto &[key, fields, _] : rows) { + output->append(redis::BulkString(key)); + output->append(MultiLen(fields.size() * 2)); + for (const auto &[info, field] : fields) { + output->append(redis::BulkString(info->name)); + output->append(redis::BulkString(field)); + } + } +} + class CommandFTSearchSQL : public Commander { Status Execute(Server *srv, Connection *conn, std::string *output) override { const auto &sql = args_[1]; @@ -146,15 +160,7 @@ class CommandFTSearchSQL : public Commander { auto results = GET_OR_RET(srv->index_mgr.Search(std::move(ir), conn->GetNamespace())); - output->append(MultiLen(results.size())); - for (const auto &[key, fields, _] : results) { - output->append(MultiLen(2)); - output->append(redis::BulkString(key)); - output->append(MultiLen(fields.size())); - for (const auto &[_, field] : fields) { - output->append(redis::BulkString(field)); - } - } + DumpQueryResult(results, output); return Status::OK(); }; @@ -211,15 +217,7 @@ class CommandFTSearch : public Commander { CHECK(ir_); auto results = GET_OR_RET(srv->index_mgr.Search(std::move(ir_), conn->GetNamespace())); - output->append(MultiLen(results.size())); - for (const auto &[key, fields, _] : results) { - output->append(MultiLen(2)); - output->append(redis::BulkString(key)); - output->append(MultiLen(fields.size())); - for (const auto &[_, field] : fields) { - output->append(redis::BulkString(field)); - } - } + DumpQueryResult(results, output); return Status::OK(); }; diff --git a/src/search/index_manager.h b/src/search/index_manager.h index 222c1440ca8..a4174dbdf77 100644 --- a/src/search/index_manager.h +++ b/src/search/index_manager.h @@ -188,7 +188,7 @@ struct IndexManager { auto plan_ir = kqir::PassManager::Execute(kqir::PassManager::Default(), std::move(ir)); std::unique_ptr plan_op; if (plan_op = kqir::Node::As(std::move(plan_ir)); !plan_op) { - return {Status::NotOK, "failed to convert the SQL query to plan operators"}; + return {Status::NotOK, "failed to convert the query to plan operators"}; } kqir::ExecutorContext executor_ctx(plan_op.get(), storage);