memgraph/query_modules/text_search.cpp

69 lines
2.6 KiB
C++
Raw Normal View History

2024-01-04 17:34:58 +08:00
// 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
// License, and you may not use this file except in compliance with the Business Source License.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
#include <mgp.hpp>
2024-01-05 20:32:45 +08:00
// #include "query/procedure/mg_procedure_impl.hpp"
2024-01-08 08:33:34 +08:00
// #include "storage/v2/indices/mgcxx_mock.hpp"
2024-01-05 20:54:30 +08:00
#include "storage/v2/mgcxx_mock.hpp"
2024-01-04 17:34:58 +08:00
namespace TextSearch {
constexpr std::string_view kProcedureSearch = "search";
constexpr std::string_view kParameterLabel = "label";
constexpr std::string_view kParameterSearchString = "search_string";
constexpr std::string_view kReturnNode = "node";
void Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
} // namespace TextSearch
void Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
2024-01-10 03:46:30 +08:00
// CALL text_search.search("Label", "nekiQuery", searchFields, returnFields) RETURN node, score
2024-01-04 17:34:58 +08:00
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
auto label = arguments[0].ValueString();
auto search_string = arguments[1].ValueString();
2024-01-10 03:46:30 +08:00
// 1. See if the given label is text-indexed
2024-01-08 08:33:34 +08:00
if (!mgp::graph_has_text_index(memgraph_graph, label.data())) {
return;
}
2024-01-05 20:32:45 +08:00
2024-01-04 17:34:58 +08:00
// 2. Run text search of that index
2024-01-10 03:46:30 +08:00
mgp::graph_search_text_index(memgraph_graph, label.data());
2024-01-05 20:32:45 +08:00
// text_index.search(label, search_string);
2024-01-04 17:34:58 +08:00
// 3. Get the graph elements from their IDs in the search results
2024-01-05 20:32:45 +08:00
2024-01-04 17:34:58 +08:00
// 4. Return records (one per element)
}
extern "C" int mgp_init_module(struct mgp_module *module, struct mgp_memory *memory) {
try {
mgp::MemoryDispatcherGuard guard{memory};
AddProcedure(TextSearch::Search, TextSearch::kProcedureSearch, mgp::ProcedureType::Read,
{
2024-01-08 08:33:34 +08:00
mgp::Parameter(TextSearch::kParameterLabel, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchString, mgp::Type::String),
2024-01-04 17:34:58 +08:00
},
{mgp::Return(TextSearch::kReturnNode, mgp::Type::Node)}, module, memory);
} catch (const std::exception &e) {
std::cerr << "Error while initializing query module: " << e.what() << std::endl;
return 1;
}
return 0;
}
extern "C" int mgp_shutdown_module() { return 0; }