Improve the text search query module

This commit is contained in:
Ante Pušić 2024-02-04 02:25:33 +01:00
parent ecbe385e58
commit 5cea4d0c40

View File

@ -9,6 +9,11 @@
// by the Apache License, Version 2.0, included in the file // by the Apache License, Version 2.0, included in the file
// licenses/APL.txt. // licenses/APL.txt.
#include <string>
#include <string_view>
#include <fmt/format.h>
#include <mgp.hpp> #include <mgp.hpp>
namespace TextSearch { namespace TextSearch {
@ -24,19 +29,27 @@ void TextSearch::Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *r
mgp::MemoryDispatcherGuard guard{memory}; mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result); const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args); auto arguments = mgp::List(args);
auto label = arguments[0].ValueString();
auto search_query = arguments[1].ValueString();
// 1. See if the given label is text-indexed try {
if (!mgp::graph_has_text_index(memgraph_graph, label.data())) { auto label = arguments[0].ValueString().data();
record_factory.SetErrorMessage("The given text index doesnt exist in Memgraph"); auto search_query = arguments[1].ValueString().data();
}
// 2. Run a text search of that index and return the search results // 1. See if the given label is text-indexed
for (const auto &node : if (!mgp::graph_has_text_index(memgraph_graph, label)) {
mgp::List(mgp::graph_search_text_index(memgraph_graph, memory, label.data(), search_query.data()))) { record_factory.SetErrorMessage(fmt::format("Text index \"{}\" doesnt exist.", label));
auto record = record_factory.NewRecord(); return;
record.Insert(TextSearch::kReturnNode.data(), node); }
// 2. Run a text search of that index and return the search results
const auto results = mgp::graph_search_text_index(memgraph_graph, memory, label, search_query);
if (!results) return;
for (const auto &node : mgp::List(results)) {
auto record = record_factory.NewRecord();
record.Insert(TextSearch::kReturnNode.data(), node);
}
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
} }
} }