From 2ec08f7da578d912e1e09ddba7741fa0b631af2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ante=20Pu=C5=A1i=C4=87?= Date: Mon, 8 Jan 2024 01:33:34 +0100 Subject: [PATCH] Add work on the search query procedure --- include/_mgp.hpp | 8 ++++++-- include/mg_procedure.h | 4 +++- query_modules/text_search.cpp | 11 ++++++++--- src/query/procedure/mg_procedure_impl.cpp | 14 +++++++++++++- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/include/_mgp.hpp b/include/_mgp.hpp index 4f6797739..1170d7ed1 100644 --- a/include/_mgp.hpp +++ b/include/_mgp.hpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -283,7 +283,7 @@ inline mgp_list *list_all_unique_constraints(mgp_graph *graph, mgp_memory *memor } // mgp_graph - + inline bool graph_is_transactional(mgp_graph *graph) { return MgInvoke(mgp_graph_is_transactional, graph); } inline bool graph_is_mutable(mgp_graph *graph) { return MgInvoke(mgp_graph_is_mutable, graph); } @@ -326,6 +326,10 @@ inline mgp_vertex *graph_get_vertex_by_id(mgp_graph *g, mgp_vertex_id id, mgp_me return MgInvoke(mgp_graph_get_vertex_by_id, g, id, memory); } +inline bool graph_has_text_index(mgp_graph *graph, const char *label) { + return MgInvoke(mgp_graph_has_text_index, graph, label); +} + inline mgp_vertices_iterator *graph_iter_vertices(mgp_graph *g, mgp_memory *memory) { return MgInvoke(mgp_graph_iter_vertices, g, memory); } diff --git a/include/mg_procedure.h b/include/mg_procedure.h index 93ef241d8..3e7819efe 100644 --- a/include/mg_procedure.h +++ b/include/mg_procedure.h @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -891,6 +891,8 @@ enum mgp_error mgp_edge_iter_properties(struct mgp_edge *e, struct mgp_memory *m enum mgp_error mgp_graph_get_vertex_by_id(struct mgp_graph *g, struct mgp_vertex_id id, struct mgp_memory *memory, struct mgp_vertex **result); +enum mgp_error mgp_graph_has_text_index(mgp_graph *graph, const char *label); + /// Creates label index for given label. /// mgp_error::MGP_ERROR_NO_ERROR is always returned. /// if label index already exists, result will be 0, otherwise 1. diff --git a/query_modules/text_search.cpp b/query_modules/text_search.cpp index 18ca1d33b..4f3530603 100644 --- a/query_modules/text_search.cpp +++ b/query_modules/text_search.cpp @@ -11,6 +11,7 @@ #include // #include "query/procedure/mg_procedure_impl.hpp" +// #include "storage/v2/indices/mgcxx_mock.hpp" #include "storage/v2/mgcxx_mock.hpp" namespace TextSearch { @@ -31,7 +32,11 @@ void Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_m // TODO antepusic: // 1. Match the label to the appropriate text index - // auto label_id = memgraph_graph->impl->NameToLabel(label); + // auto label_id = memgraph_graph->impl->NameToLabel(label); <- needs API method + + if (!mgp::graph_has_text_index(memgraph_graph, label.data())) { + return; + } // 2. Run text search of that index // * Add metadata to the return fields before search @@ -49,8 +54,8 @@ extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *mem AddProcedure(TextSearch::Search, TextSearch::kProcedureSearch, mgp::ProcedureType::Read, { - mgp::Parameter(TextSearch::kParameterLabel, {mgp::Type::String}), - mgp::Parameter(TextSearch::kParameterSearchString, {mgp::Type::String}), + mgp::Parameter(TextSearch::kParameterLabel, mgp::Type::String), + mgp::Parameter(TextSearch::kParameterSearchString, mgp::Type::String), }, {mgp::Return(TextSearch::kReturnNode, mgp::Type::Node)}, module, memory); } catch (const std::exception &e) { diff --git a/src/query/procedure/mg_procedure_impl.cpp b/src/query/procedure/mg_procedure_impl.cpp index 2eea1cecb..2824f9ab8 100644 --- a/src/query/procedure/mg_procedure_impl.cpp +++ b/src/query/procedure/mg_procedure_impl.cpp @@ -1,4 +1,4 @@ -// Copyright 2023 Memgraph Ltd. +// Copyright 2024 Memgraph Ltd. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source @@ -3322,6 +3322,18 @@ mgp_error mgp_graph_delete_edge(struct mgp_graph *graph, mgp_edge *edge) { }); } +mgp_error mgp_graph_has_text_index(mgp_graph *graph, const char *label, int *result) { + return WrapExceptions([graph, label, result]() { + std::visit( + memgraph::utils::Overloaded{ + [&](memgraph::query::DbAccessor *impl) { *result = impl->TextIndexExists(impl->NameToLabel(label)); }, + [&](memgraph::query::SubgraphDbAccessor *impl) { + *result = impl->GetAccessor()->TextIndexExists(impl->GetAccessor()->NameToLabel(label)); + }}, + graph->impl); + }); +} + #ifdef MG_ENTERPRISE namespace { void NextPermitted(mgp_vertices_iterator &it) {