From 5cea4d0c40c209d89ad83a7133212f568d1a6126 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Pu=C5=A1i=C4=87?= Date: Sun, 4 Feb 2024 02:25:33 +0100 Subject: [PATCH] Improve the text search query module --- query_modules/text_search_module.cpp | 35 +++++++++++++++++++--------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/query_modules/text_search_module.cpp b/query_modules/text_search_module.cpp index a5c0b346b..b68ed8268 100644 --- a/query_modules/text_search_module.cpp +++ b/query_modules/text_search_module.cpp @@ -9,6 +9,11 @@ // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. +#include +#include + +#include + #include namespace TextSearch { @@ -24,19 +29,27 @@ void TextSearch::Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *r mgp::MemoryDispatcherGuard guard{memory}; const auto record_factory = mgp::RecordFactory(result); 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 - if (!mgp::graph_has_text_index(memgraph_graph, label.data())) { - record_factory.SetErrorMessage("The given text index doesn’t exist in Memgraph"); - } + try { + auto label = arguments[0].ValueString().data(); + auto search_query = arguments[1].ValueString().data(); - // 2. Run a text search of that index and return the search results - for (const auto &node : - mgp::List(mgp::graph_search_text_index(memgraph_graph, memory, label.data(), search_query.data()))) { - auto record = record_factory.NewRecord(); - record.Insert(TextSearch::kReturnNode.data(), node); + // 1. See if the given label is text-indexed + if (!mgp::graph_has_text_index(memgraph_graph, label)) { + record_factory.SetErrorMessage(fmt::format("Text index \"{}\" doesn’t exist.", label)); + return; + } + + // 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()); } }