-
Notifications
You must be signed in to change notification settings - Fork 217
Expand file tree
/
Copy pathtext_search_module.cpp
More file actions
327 lines (297 loc) · 15.4 KB
/
text_search_module.cpp
File metadata and controls
327 lines (297 loc) · 15.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Copyright 2026 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 <fmt/format.h>
#include <iostream>
#include <mgp.hpp>
#include <string>
#include <string_view>
namespace TextSearch {
constexpr std::string_view kProcedureSearch = "search";
constexpr std::string_view kProcedureRegexSearch = "regex_search";
constexpr std::string_view kProcedureSearchAllProperties = "search_all";
constexpr std::string_view kProcedureAggregate = "aggregate";
constexpr std::string_view kProcedureSearchEdges = "search_edges";
constexpr std::string_view kProcedureRegexSearchEdges = "regex_search_edges";
constexpr std::string_view kProcedureSearchAllPropertiesEdges = "search_all_edges";
constexpr std::string_view kProcedureAggregateEdges = "aggregate_edges";
constexpr std::string_view kParameterIndexName = "index_name";
constexpr std::string_view kParameterSearchQuery = "search_query";
constexpr std::string_view kParameterAggregationQuery = "aggregation_query";
constexpr std::string_view kReturnNode = "node";
constexpr std::string_view kReturnEdge = "edge";
constexpr std::string_view kReturnAggregation = "aggregation";
constexpr std::string_view kReturnScore = "score";
constexpr std::string_view kSearchAllPrefix = "all";
constexpr std::string_view kParameterLimit = "limit";
constexpr std::int64_t kDefaultLimit = 1000;
void Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void RegexSearch(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void SearchAllProperties(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void SearchEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void RegexSearchEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void SearchAllPropertiesEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void Aggregate(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
void AggregateEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory);
} // namespace TextSearch
// Text search on nodes functions
void TextSearch::Search(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
const auto *index_name = arguments[0].ValueString().data();
const auto *search_query = arguments[1].ValueString().data();
const auto limit = arguments[2].ValueInt();
for (const auto &result : mgp::SearchTextIndex(
memgraph_graph, index_name, search_query, text_search_mode::SPECIFIED_PROPERTIES, limit)) {
auto record = record_factory.NewRecord();
auto result_list = result.ValueList();
record.Insert(TextSearch::kReturnNode.data(), result_list[0].ValueNode());
record.Insert(TextSearch::kReturnScore.data(), result_list[1].ValueDouble());
}
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
void TextSearch::RegexSearch(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
const auto index_name = arguments[0].ValueString();
const auto search_query = arguments[1].ValueString();
const auto limit = arguments[2].ValueInt();
for (const auto &result :
mgp::SearchTextIndex(memgraph_graph, index_name, search_query, text_search_mode::REGEX, limit)) {
auto record = record_factory.NewRecord();
auto result_list = result.ValueList();
record.Insert(TextSearch::kReturnNode.data(), result_list[0].ValueNode());
record.Insert(TextSearch::kReturnScore.data(), result_list[1].ValueDouble());
}
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
void TextSearch::SearchAllProperties(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result,
mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
auto const index_name = arguments[0].ValueString();
auto const search_query = fmt::format("{}:{}", kSearchAllPrefix, arguments[1].ValueString());
const auto limit = arguments[2].ValueInt();
for (const auto &result :
mgp::SearchTextIndex(memgraph_graph, index_name, search_query, text_search_mode::ALL_PROPERTIES, limit)) {
auto record = record_factory.NewRecord();
auto result_list = result.ValueList();
record.Insert(TextSearch::kReturnNode.data(), result_list[0].ValueNode());
record.Insert(TextSearch::kReturnScore.data(), result_list[1].ValueDouble());
}
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
void TextSearch::Aggregate(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
const auto index_name = arguments[0].ValueString();
const auto search_query = arguments[1].ValueString();
const auto aggregation_query = arguments[2].ValueString();
const auto aggregation_result =
mgp::AggregateOverTextIndex(memgraph_graph, index_name, search_query, aggregation_query);
auto record = record_factory.NewRecord();
record.Insert(TextSearch::kReturnAggregation.data(), aggregation_result.data());
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
// Text search on edges functions
void TextSearch::SearchEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
const auto *index_name = arguments[0].ValueString().data();
const auto *search_query = arguments[1].ValueString().data();
const auto limit = arguments[2].ValueInt();
for (const auto &result : mgp::SearchTextEdgeIndex(
memgraph_graph, index_name, search_query, text_search_mode::SPECIFIED_PROPERTIES, limit)) {
auto record = record_factory.NewRecord();
auto result_list = result.ValueList();
record.Insert(TextSearch::kReturnEdge.data(), result_list[0].ValueRelationship());
record.Insert(TextSearch::kReturnScore.data(), result_list[1].ValueDouble());
}
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
void TextSearch::RegexSearchEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
const auto index_name = arguments[0].ValueString();
const auto search_query = arguments[1].ValueString();
const auto limit = arguments[2].ValueInt();
for (const auto &result :
mgp::SearchTextEdgeIndex(memgraph_graph, index_name, search_query, text_search_mode::REGEX, limit)) {
auto record = record_factory.NewRecord();
auto result_list = result.ValueList();
record.Insert(TextSearch::kReturnEdge.data(), result_list[0].ValueRelationship());
record.Insert(TextSearch::kReturnScore.data(), result_list[1].ValueDouble());
}
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
void TextSearch::SearchAllPropertiesEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result,
mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
auto const index_name = arguments[0].ValueString();
auto const search_query = fmt::format("{}:{}", kSearchAllPrefix, arguments[1].ValueString());
const auto limit = arguments[2].ValueInt();
for (const auto &result :
mgp::SearchTextEdgeIndex(memgraph_graph, index_name, search_query, text_search_mode::ALL_PROPERTIES, limit)) {
auto record = record_factory.NewRecord();
auto result_list = result.ValueList();
record.Insert(TextSearch::kReturnEdge.data(), result_list[0].ValueRelationship());
record.Insert(TextSearch::kReturnScore.data(), result_list[1].ValueDouble());
}
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
void TextSearch::AggregateEdges(mgp_list *args, mgp_graph *memgraph_graph, mgp_result *result, mgp_memory *memory) {
mgp::MemoryDispatcherGuard guard{memory};
const auto record_factory = mgp::RecordFactory(result);
auto arguments = mgp::List(args);
try {
const auto index_name = arguments[0].ValueString();
const auto search_query = arguments[1].ValueString();
const auto aggregation_query = arguments[2].ValueString();
const auto aggregation_result =
mgp::AggregateOverTextEdgeIndex(memgraph_graph, index_name, search_query, aggregation_query);
auto record = record_factory.NewRecord();
record.Insert(TextSearch::kReturnAggregation.data(), aggregation_result.data());
} catch (const std::exception &e) {
record_factory.SetErrorMessage(e.what());
}
}
extern "C" int mgp_init_module(struct mgp_module *query_module, struct mgp_memory *memory) {
try {
mgp::MemoryDispatcherGuard guard{memory};
AddProcedure(TextSearch::Search,
TextSearch::kProcedureSearch,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterLimit, mgp::Type::Int, TextSearch::kDefaultLimit),
},
{mgp::Return(TextSearch::kReturnNode, mgp::Type::Node),
mgp::Return(TextSearch::kReturnScore, mgp::Type::Double)},
query_module,
memory);
AddProcedure(TextSearch::RegexSearch,
TextSearch::kProcedureRegexSearch,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterLimit, mgp::Type::Int, TextSearch::kDefaultLimit),
},
{mgp::Return(TextSearch::kReturnNode, mgp::Type::Node),
mgp::Return(TextSearch::kReturnScore, mgp::Type::Double)},
query_module,
memory);
AddProcedure(TextSearch::SearchAllProperties,
TextSearch::kProcedureSearchAllProperties,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterLimit, mgp::Type::Int, TextSearch::kDefaultLimit),
},
{mgp::Return(TextSearch::kReturnNode, mgp::Type::Node),
mgp::Return(TextSearch::kReturnScore, mgp::Type::Double)},
query_module,
memory);
AddProcedure(TextSearch::Aggregate,
TextSearch::kProcedureAggregate,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterAggregationQuery, mgp::Type::String),
},
{mgp::Return(TextSearch::kReturnAggregation, mgp::Type::String)},
query_module,
memory);
AddProcedure(TextSearch::SearchEdges,
TextSearch::kProcedureSearchEdges,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterLimit, mgp::Type::Int, TextSearch::kDefaultLimit),
},
{mgp::Return(TextSearch::kReturnEdge, mgp::Type::Relationship),
mgp::Return(TextSearch::kReturnScore, mgp::Type::Double)},
query_module,
memory);
AddProcedure(TextSearch::RegexSearchEdges,
TextSearch::kProcedureRegexSearchEdges,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterLimit, mgp::Type::Int, TextSearch::kDefaultLimit),
},
{mgp::Return(TextSearch::kReturnEdge, mgp::Type::Relationship),
mgp::Return(TextSearch::kReturnScore, mgp::Type::Double)},
query_module,
memory);
AddProcedure(TextSearch::SearchAllPropertiesEdges,
TextSearch::kProcedureSearchAllPropertiesEdges,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterLimit, mgp::Type::Int, TextSearch::kDefaultLimit),
},
{mgp::Return(TextSearch::kReturnEdge, mgp::Type::Relationship),
mgp::Return(TextSearch::kReturnScore, mgp::Type::Double)},
query_module,
memory);
AddProcedure(TextSearch::AggregateEdges,
TextSearch::kProcedureAggregateEdges,
mgp::ProcedureType::Read,
{
mgp::Parameter(TextSearch::kParameterIndexName, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterSearchQuery, mgp::Type::String),
mgp::Parameter(TextSearch::kParameterAggregationQuery, mgp::Type::String),
},
{mgp::Return(TextSearch::kReturnAggregation, mgp::Type::String)},
query_module,
memory);
} catch (const std::exception &e) {
std::cerr << "Error while initializing query module: " << e.what() << '\n';
return 1;
}
return 0;
}
extern "C" int mgp_shutdown_module() { return 0; }