forked from dragonflydb/dragonfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_family.cc
1031 lines (837 loc) · 32.7 KB
/
search_family.cc
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//
#include "server/search/search_family.h"
#include <absl/container/flat_hash_map.h>
#include <absl/strings/ascii.h>
#include <absl/strings/match.h>
#include <absl/strings/str_format.h>
#include <atomic>
#include <variant>
#include <vector>
#include "base/logging.h"
#include "core/search/search.h"
#include "core/search/vector_utils.h"
#include "facade/cmd_arg_parser.h"
#include "facade/error.h"
#include "facade/reply_builder.h"
#include "server/acl/acl_commands_def.h"
#include "server/command_registry.h"
#include "server/conn_context.h"
#include "server/container_utils.h"
#include "server/engine_shard_set.h"
#include "server/search/aggregator.h"
#include "server/search/doc_index.h"
#include "server/transaction.h"
#include "src/core/overloaded.h"
namespace dfly {
using namespace std;
using namespace facade;
namespace {
static const set<string_view> kIgnoredOptions = {"WEIGHT", "SEPARATOR"};
bool IsValidJsonPath(string_view path) {
error_code ec;
MakeJsonPathExpr(path, ec);
return !ec;
}
search::SchemaField::VectorParams ParseVectorParams(CmdArgParser* parser) {
search::SchemaField::VectorParams params{};
params.use_hnsw = parser->MapNext("HNSW", true, "FLAT", false);
const size_t num_args = parser->Next<size_t>();
for (size_t i = 0; i * 2 < num_args; i++) {
if (parser->Check("DIM", ¶ms.dim)) {
} else if (parser->Check("DISTANCE_METRIC")) {
params.sim = parser->MapNext("L2", search::VectorSimilarity::L2, "COSINE",
search::VectorSimilarity::COSINE);
} else if (parser->Check("INITIAL_CAP", ¶ms.capacity)) {
} else if (parser->Check("M", ¶ms.hnsw_m)) {
} else if (parser->Check("EF_CONSTRUCTION", ¶ms.hnsw_ef_construction)) {
} else if (parser->Check("EF_RUNTIME")) {
parser->Next<size_t>();
LOG(WARNING) << "EF_RUNTIME not supported";
} else if (parser->Check("EPSILON")) {
parser->Next<double>();
LOG(WARNING) << "EPSILON not supported";
} else {
parser->Skip(2);
}
}
return params;
}
std::optional<search::SchemaField::TagParams> ParseTagParams(CmdArgParser* parser,
SinkReplyBuilder* builder) {
search::SchemaField::TagParams params{};
while (parser->HasNext()) {
if (parser->Check("SEPARATOR")) {
std::string_view separator = parser->NextOrDefault();
if (separator.size() != 1) {
builder->SendError(
absl::StrCat("Tag separator must be a single character. Got `", separator, "`"),
kSyntaxErrType);
return std::nullopt;
}
params.separator = separator.front();
continue;
}
if (parser->Check("CASESENSITIVE")) {
params.case_sensitive = true;
continue;
}
break;
}
return params;
}
// breaks on ParamsVariant initialization
#ifndef __clang__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif
optional<search::Schema> ParseSchemaOrReply(DocIndex::DataType type, CmdArgParser parser,
SinkReplyBuilder* builder) {
search::Schema schema;
while (parser.HasNext()) {
string_view field = parser.Next();
string_view field_alias = field;
// Verify json path is correct
if (type == DocIndex::JSON && !IsValidJsonPath(field)) {
builder->SendError("Bad json path: " + string{field});
return nullopt;
}
// AS [alias]
parser.Check("AS", &field_alias);
// Determine type
using search::SchemaField;
auto type = parser.MapNext("TAG", SchemaField::TAG, "TEXT", SchemaField::TEXT, "NUMERIC",
SchemaField::NUMERIC, "VECTOR", SchemaField::VECTOR);
if (auto err = parser.Error(); err) {
builder->SendError(err->MakeReply());
return nullopt;
}
// Tag fields include: [separator char] [casesensitive]
// Vector fields include: {algorithm} num_args args...
search::SchemaField::ParamsVariant params(monostate{});
if (type == search::SchemaField::TAG) {
auto tag_params = ParseTagParams(&parser, builder);
if (!tag_params) {
return std::nullopt;
}
params = tag_params.value();
} else if (type == search::SchemaField::VECTOR) {
auto vector_params = ParseVectorParams(&parser);
if (parser.HasError()) {
auto err = *parser.Error();
VLOG(1) << "Could not parse vector param " << err.index;
builder->SendError("Parse error of vector parameters", kSyntaxErrType);
return nullopt;
}
if (vector_params.dim == 0) {
builder->SendError("Knn vector dimension cannot be zero", kSyntaxErrType);
return nullopt;
}
params = vector_params;
}
// Flags: check for SORTABLE and NOINDEX
uint8_t flags = 0;
while (parser.HasNext()) {
if (parser.Check("NOINDEX")) {
flags |= search::SchemaField::NOINDEX;
continue;
}
if (parser.Check("SORTABLE")) {
flags |= search::SchemaField::SORTABLE;
continue;
}
break;
}
// Skip all trailing ignored parameters
while (kIgnoredOptions.count(parser.Peek()) > 0)
parser.Skip(2);
schema.fields[field] = {type, flags, string{field_alias}, std::move(params)};
}
// Build field name mapping table
for (const auto& [field_ident, field_info] : schema.fields)
schema.field_names[field_info.short_name] = field_ident;
if (auto err = parser.Error(); err) {
builder->SendError(err->MakeReply());
return nullopt;
}
return schema;
}
#ifndef __clang__
#pragma GCC diagnostic pop
#endif
std::string_view ParseField(CmdArgParser* parser) {
std::string_view field = parser->Next();
if (absl::StartsWith(field, "@"sv)) {
field.remove_prefix(1); // remove leading @ if exists
}
return field;
}
std::string_view ParseFieldWithAtSign(CmdArgParser* parser) {
std::string_view field = parser->Next();
if (absl::StartsWith(field, "@"sv)) {
field.remove_prefix(1); // remove leading @
} else {
// Temporary warning until we can throw an error
LOG(WARNING) << "bad arguments: Field name '" << field << "' should start with '@'. '@" << field
<< "' is expected";
}
return field;
}
void ParseLoadFields(CmdArgParser* parser, std::optional<SearchFieldsList>* load_fields) {
// TODO: Change to num_strings. In Redis strings number is expected. For example: LOAD 3 $.a AS a
size_t num_fields = parser->Next<size_t>();
if (!load_fields->has_value()) {
load_fields->emplace();
}
while (num_fields--) {
string_view str = parser->Next();
if (absl::StartsWith(str, "@"sv)) {
str.remove_prefix(1); // remove leading @
}
StringOrView name = StringOrView::FromString(std::string{str});
if (parser->Check("AS")) {
load_fields->value().emplace_back(name, true,
StringOrView::FromString(parser->Next<std::string>()));
} else {
load_fields->value().emplace_back(name, true);
}
}
}
search::QueryParams ParseQueryParams(CmdArgParser* parser) {
search::QueryParams params;
size_t num_args = parser->Next<size_t>();
while (parser->HasNext() && params.Size() * 2 < num_args) {
auto [k, v] = parser->Next<string_view, string_view>();
params[k] = v;
}
return params;
}
optional<SearchParams> ParseSearchParamsOrReply(CmdArgParser* parser, SinkReplyBuilder* builder) {
SearchParams params;
while (parser->HasNext()) {
// [LIMIT offset total]
if (parser->Check("LIMIT")) {
params.limit_offset = parser->Next<size_t>();
params.limit_total = parser->Next<size_t>();
} else if (parser->Check("LOAD")) {
if (params.return_fields) {
builder->SendError("LOAD cannot be applied after RETURN");
return std::nullopt;
}
ParseLoadFields(parser, ¶ms.load_fields);
} else if (parser->Check("RETURN")) {
if (params.load_fields) {
builder->SendError("RETURN cannot be applied after LOAD");
return std::nullopt;
}
// RETURN {num} [{ident} AS {name}...]
/* TODO: Change to num_strings. In Redis strings number is expected. For example: RETURN 3 $.a
* AS a */
size_t num_fields = parser->Next<size_t>();
params.return_fields.emplace();
while (params.return_fields->size() < num_fields) {
StringOrView name = StringOrView::FromString(parser->Next<std::string>());
if (parser->Check("AS")) {
params.return_fields->emplace_back(std::move(name), true,
StringOrView::FromString(parser->Next<std::string>()));
} else {
params.return_fields->emplace_back(std::move(name), true);
}
}
} else if (parser->Check("NOCONTENT")) { // NOCONTENT
params.load_fields.emplace();
params.return_fields.emplace();
} else if (parser->Check("PARAMS")) { // [PARAMS num(ignored) name(ignored) knn_vector]
params.query_params = ParseQueryParams(parser);
} else if (parser->Check("SORTBY")) {
params.sort_option =
search::SortOption{parser->Next<std::string>(), bool(parser->Check("DESC"))};
} else {
// Unsupported parameters are ignored for now
parser->Skip(1);
}
}
if (auto err = parser->Error(); err) {
builder->SendError(err->MakeReply());
return nullopt;
}
return params;
}
optional<AggregateParams> ParseAggregatorParamsOrReply(CmdArgParser parser,
SinkReplyBuilder* builder) {
AggregateParams params;
tie(params.index, params.query) = parser.Next<string_view, string_view>();
// Parse LOAD count field [field ...]
// LOAD options are at the beginning of the query, so we need to parse them first
while (parser.HasNext() && parser.Check("LOAD")) {
ParseLoadFields(&parser, ¶ms.load_fields);
}
while (parser.HasNext()) {
// GROUPBY nargs property [property ...]
if (parser.Check("GROUPBY")) {
vector<string_view> fields(parser.Next<size_t>());
for (string_view& field : fields) {
auto parsed_field = ParseFieldWithAtSign(&parser);
/*
TODO: Throw an error if the field has no '@' sign at the beginning
if (!parsed_field) {
builder->SendError(absl::StrCat("bad arguments for GROUPBY: Unknown property '", field,
"'. Did you mean '@", field, "`?"));
return nullopt;
} */
field = parsed_field;
}
vector<aggregate::Reducer> reducers;
while (parser.Check("REDUCE")) {
using RF = aggregate::ReducerFunc;
auto func_name =
parser.TryMapNext("COUNT", RF::COUNT, "COUNT_DISTINCT", RF::COUNT_DISTINCT, "SUM",
RF::SUM, "AVG", RF::AVG, "MAX", RF::MAX, "MIN", RF::MIN);
if (!func_name) {
builder->SendError(absl::StrCat("reducer function ", parser.Next(), " not found"));
return nullopt;
}
auto func = aggregate::FindReducerFunc(*func_name);
auto nargs = parser.Next<size_t>();
string source_field;
if (nargs > 0) {
source_field = ParseField(&parser);
}
parser.ExpectTag("AS");
string result_field = parser.Next<string>();
reducers.push_back(
aggregate::Reducer{std::move(source_field), std::move(result_field), std::move(func)});
}
params.steps.push_back(aggregate::MakeGroupStep(fields, std::move(reducers)));
continue;
}
// SORTBY nargs
if (parser.Check("SORTBY")) {
parser.ExpectTag("1");
string_view field = parser.Next();
bool desc = bool(parser.Check("DESC"));
params.steps.push_back(aggregate::MakeSortStep(field, desc));
continue;
}
// LIMIT
if (parser.Check("LIMIT")) {
auto [offset, num] = parser.Next<size_t, size_t>();
params.steps.push_back(aggregate::MakeLimitStep(offset, num));
continue;
}
// PARAMS
if (parser.Check("PARAMS")) {
params.params = ParseQueryParams(&parser);
continue;
}
if (parser.Check("LOAD")) {
builder->SendError("LOAD cannot be applied after projectors or reducers");
return nullopt;
}
builder->SendError(absl::StrCat("Unknown clause: ", parser.Peek()));
return nullopt;
}
if (auto err = parser.Error(); err) {
builder->SendError(err->MakeReply());
return nullopt;
}
return params;
}
auto SortableValueSender(RedisReplyBuilder* rb) {
return Overloaded{
[rb](monostate) { rb->SendNull(); },
[rb](double d) { rb->SendDouble(d); },
[rb](const string& s) { rb->SendBulkString(s); },
};
}
void SendSerializedDoc(const SerializedSearchDoc& doc, SinkReplyBuilder* builder) {
auto* rb = static_cast<RedisReplyBuilder*>(builder);
auto sortable_value_sender = SortableValueSender(rb);
rb->SendBulkString(doc.key);
rb->StartCollection(doc.values.size(), RedisReplyBuilder::MAP);
for (const auto& [k, v] : doc.values) {
rb->SendBulkString(k);
visit(sortable_value_sender, v);
}
}
void ReplyWithResults(const SearchParams& params, absl::Span<SearchResult> results,
SinkReplyBuilder* builder) {
size_t total_count = 0;
for (const auto& shard_docs : results)
total_count += shard_docs.total_hits;
size_t result_count =
min(total_count - min(total_count, params.limit_offset), params.limit_total);
facade::SinkReplyBuilder::ReplyAggregator agg{builder};
bool ids_only = params.IdsOnly();
size_t reply_size = ids_only ? (result_count + 1) : (result_count * 2 + 1);
auto* rb = static_cast<RedisReplyBuilder*>(builder);
rb->StartArray(reply_size);
rb->SendLong(total_count);
size_t sent = 0;
size_t to_skip = params.limit_offset;
for (const auto& shard_docs : results) {
for (const auto& serialized_doc : shard_docs.docs) {
// Scoring is not implemented yet, so we just cut them in the order they were retrieved
if (to_skip > 0) {
to_skip--;
continue;
}
if (sent++ >= result_count)
return;
if (ids_only)
rb->SendBulkString(serialized_doc.key);
else
SendSerializedDoc(serialized_doc, builder);
}
}
}
void ReplySorted(search::AggregationInfo agg, const SearchParams& params,
absl::Span<SearchResult> results, SinkReplyBuilder* builder) {
size_t total = 0;
vector<SerializedSearchDoc*> docs;
for (auto& shard_results : results) {
total += shard_results.total_hits;
for (auto& doc : shard_results.docs) {
docs.push_back(&doc);
}
}
size_t agg_limit = agg.limit.value_or(total);
size_t prefix = min(params.limit_offset + params.limit_total, agg_limit);
partial_sort(docs.begin(), docs.begin() + min(docs.size(), prefix), docs.end(),
[desc = agg.descending](const auto* l, const auto* r) {
return desc ? (*l >= *r) : (*l < *r);
});
docs.resize(min(docs.size(), agg_limit));
size_t start_idx = min(params.limit_offset, docs.size());
size_t result_count = min(docs.size() - start_idx, params.limit_total);
bool ids_only = params.IdsOnly();
size_t reply_size = ids_only ? (result_count + 1) : (result_count * 2 + 1);
// Clear score alias if it's excluded from return values
if (!params.ShouldReturnField(agg.alias))
agg.alias = "";
facade::SinkReplyBuilder::ReplyAggregator agg_reply{builder};
auto* rb = static_cast<RedisReplyBuilder*>(builder);
rb->StartArray(reply_size);
rb->SendLong(min(total, agg_limit));
for (auto* doc : absl::MakeSpan(docs).subspan(start_idx, result_count)) {
if (ids_only) {
rb->SendBulkString(doc->key);
continue;
}
if (!agg.alias.empty() && holds_alternative<float>(doc->score))
doc->values[agg.alias] = absl::StrCat(get<float>(doc->score));
SendSerializedDoc(*doc, builder);
}
}
} // namespace
void SearchFamily::FtCreate(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder,
ConnectionContext* cntx) {
if (cntx->conn_state.db_index != 0) {
return builder->SendError("Cannot create index on db != 0"sv);
}
DocIndex index{};
CmdArgParser parser{args};
string_view idx_name = parser.Next();
while (parser.HasNext()) {
// ON HASH | JSON
if (parser.Check("ON")) {
index.type = parser.MapNext("HASH"sv, DocIndex::HASH, "JSON"sv, DocIndex::JSON);
continue;
}
// PREFIX count prefix [prefix ...]
if (parser.Check("PREFIX")) {
if (size_t num = parser.Next<size_t>(); num != 1)
return builder->SendError("Multiple prefixes are not supported");
index.prefix = string(parser.Next());
continue;
}
// STOWORDS count [words...]
if (parser.Check("STOPWORDS")) {
index.options.stopwords.clear();
for (size_t num = parser.Next<size_t>(); num > 0; num--)
index.options.stopwords.emplace(parser.Next());
continue;
}
// SCHEMA
if (parser.Check("SCHEMA")) {
auto schema = ParseSchemaOrReply(index.type, parser.Tail(), builder);
if (!schema)
return;
index.schema = std::move(*schema);
break; // SCHEMA always comes last
}
// Unsupported parameters are ignored for now
parser.Skip(1);
}
if (auto err = parser.Error(); err)
return builder->SendError(err->MakeReply());
// Check if index already exists
atomic_uint exists_cnt = 0;
tx->Execute(
[idx_name, &exists_cnt](auto* tx, auto* es) {
if (es->search_indices()->GetIndex(idx_name) != nullptr)
exists_cnt.fetch_add(1, std::memory_order_relaxed);
return OpStatus::OK;
},
false);
DCHECK(exists_cnt == 0u || exists_cnt == shard_set->size());
if (exists_cnt.load(memory_order_relaxed) > 0) {
tx->Conclude();
return builder->SendError("Index already exists");
}
auto idx_ptr = make_shared<DocIndex>(std::move(index));
tx->Execute(
[idx_name, idx_ptr](auto* tx, auto* es) {
es->search_indices()->InitIndex(tx->GetOpArgs(es), idx_name, idx_ptr);
return OpStatus::OK;
},
true);
builder->SendOk();
}
void SearchFamily::FtAlter(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
CmdArgParser parser{args};
string_view idx_name = parser.Next();
parser.ExpectTag("SCHEMA");
parser.ExpectTag("ADD");
if (auto err = parser.Error(); err)
return builder->SendError(err->MakeReply());
// First, extract existing index info
shared_ptr<DocIndex> index_info;
auto idx_cb = [idx_name, &index_info](auto* tx, EngineShard* es) {
if (es->shard_id() > 0) // all shards have the same data, fetch from first
return OpStatus::OK;
if (auto* idx = es->search_indices()->GetIndex(idx_name); idx != nullptr)
index_info = make_shared<DocIndex>(idx->GetInfo().base_index);
return OpStatus::OK;
};
tx->Execute(idx_cb, false);
if (!index_info) {
tx->Conclude();
return builder->SendError("Index not found");
}
// Parse additional schema
optional<search::Schema> new_fields = ParseSchemaOrReply(index_info->type, parser, builder);
if (!new_fields) {
tx->Conclude();
return;
}
LOG(INFO) << "Adding "
<< DocIndexInfo{.base_index = DocIndex{.schema = *new_fields}}.BuildRestoreCommand();
// Merge schemas
search::Schema& schema = index_info->schema;
schema.fields.insert(new_fields->fields.begin(), new_fields->fields.end());
schema.field_names.insert(new_fields->field_names.begin(), new_fields->field_names.end());
// Rebuild index
// TODO: Introduce partial rebuild
auto upd_cb = [idx_name, index_info](Transaction* tx, EngineShard* es) {
es->search_indices()->DropIndex(idx_name);
es->search_indices()->InitIndex(tx->GetOpArgs(es), idx_name, index_info);
return OpStatus::OK;
};
tx->Execute(upd_cb, true);
builder->SendOk();
}
void SearchFamily::FtDropIndex(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
string_view idx_name = ArgS(args, 0);
// TODO: Handle optional DD param
atomic_uint num_deleted{0};
tx->ScheduleSingleHop([&](Transaction* t, EngineShard* es) {
if (es->search_indices()->DropIndex(idx_name))
num_deleted.fetch_add(1);
return OpStatus::OK;
});
DCHECK(num_deleted == 0u || num_deleted == shard_set->size());
if (num_deleted == 0u)
return builder->SendError("-Unknown Index name");
return builder->SendOk();
}
void SearchFamily::FtInfo(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
string_view idx_name = ArgS(args, 0);
atomic_uint num_notfound{0};
vector<DocIndexInfo> infos(shard_set->size());
tx->ScheduleSingleHop([&](Transaction* t, EngineShard* es) {
auto* index = es->search_indices()->GetIndex(idx_name);
if (index == nullptr)
num_notfound.fetch_add(1);
else
infos[es->shard_id()] = index->GetInfo();
return OpStatus::OK;
});
DCHECK(num_notfound == 0u || num_notfound == shard_set->size());
if (num_notfound > 0u)
return builder->SendError("Unknown Index name");
DCHECK(infos.front().base_index.schema.fields.size() ==
infos.back().base_index.schema.fields.size());
size_t total_num_docs = 0;
for (const auto& info : infos)
total_num_docs += info.num_docs;
const auto& info = infos.front();
const auto& schema = info.base_index.schema;
auto* rb = static_cast<RedisReplyBuilder*>(builder);
rb->StartCollection(4, RedisReplyBuilder::MAP);
rb->SendSimpleString("index_name");
rb->SendSimpleString(idx_name);
rb->SendSimpleString("index_definition");
{
rb->StartCollection(2, RedisReplyBuilder::MAP);
rb->SendSimpleString("key_type");
rb->SendSimpleString(info.base_index.type == DocIndex::JSON ? "JSON" : "HASH");
rb->SendSimpleString("prefix");
rb->SendSimpleString(info.base_index.prefix);
}
rb->SendSimpleString("attributes");
rb->StartArray(schema.fields.size());
for (const auto& [field_ident, field_info] : schema.fields) {
vector<string> info;
string_view base[] = {"identifier"sv, string_view{field_ident},
"attribute", field_info.short_name,
"type"sv, SearchFieldTypeToString(field_info.type)};
info.insert(info.end(), base, base + ABSL_ARRAYSIZE(base));
if (field_info.flags & search::SchemaField::NOINDEX)
info.push_back("NOINDEX");
if (field_info.flags & search::SchemaField::SORTABLE)
info.push_back("SORTABLE");
rb->SendSimpleStrArr(info);
}
rb->SendSimpleString("num_docs");
rb->SendLong(total_num_docs);
}
void SearchFamily::FtList(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
atomic_int first{0};
vector<string> names;
tx->ScheduleSingleHop([&](Transaction* t, EngineShard* es) {
// Using `first` to assign `names` only once without a race
if (first.fetch_add(1) == 0)
names = es->search_indices()->GetIndexNames();
return OpStatus::OK;
});
auto* rb = static_cast<RedisReplyBuilder*>(builder);
rb->SendBulkStrArr(names);
}
void SearchFamily::FtSearch(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
CmdArgParser parser{args};
string_view index_name = parser.Next();
string_view query_str = parser.Next();
auto params = ParseSearchParamsOrReply(&parser, builder);
if (!params.has_value())
return;
search::SearchAlgorithm search_algo;
search::SortOption* sort_opt = params->sort_option.has_value() ? &*params->sort_option : nullptr;
if (!search_algo.Init(query_str, ¶ms->query_params, sort_opt))
return builder->SendError("Query syntax error");
// Because our coordinator thread may not have a shard, we can't check ahead if the index exists.
atomic<bool> index_not_found{false};
vector<SearchResult> docs(shard_set->size());
tx->ScheduleSingleHop([&](Transaction* t, EngineShard* es) {
if (auto* index = es->search_indices()->GetIndex(index_name); index)
docs[es->shard_id()] = index->Search(t->GetOpArgs(es), *params, &search_algo);
else
index_not_found.store(true, memory_order_relaxed);
return OpStatus::OK;
});
if (index_not_found.load())
return builder->SendError(string{index_name} + ": no such index");
for (const auto& res : docs) {
if (res.error)
return builder->SendError(*res.error);
}
if (auto agg = search_algo.HasAggregation(); agg)
ReplySorted(*agg, *params, absl::MakeSpan(docs), builder);
else
ReplyWithResults(*params, absl::MakeSpan(docs), builder);
}
void SearchFamily::FtProfile(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
CmdArgParser parser{args};
string_view index_name = parser.Next();
if (!parser.Check("SEARCH") && !parser.Check("AGGREGATE")) {
return builder->SendError("no `SEARCH` or `AGGREGATE` provided");
}
parser.Check("LIMITED"); // TODO: Implement limited profiling
parser.ExpectTag("QUERY");
string_view query_str = parser.Next();
optional<SearchParams> params = ParseSearchParamsOrReply(&parser, builder);
if (!params.has_value())
return;
search::SearchAlgorithm search_algo;
search::SortOption* sort_opt = params->sort_option.has_value() ? &*params->sort_option : nullptr;
if (!search_algo.Init(query_str, ¶ms->query_params, sort_opt))
return builder->SendError("query syntax error");
search_algo.EnableProfiling();
absl::Time start = absl::Now();
const size_t shards_count = shard_set->size();
// Because our coordinator thread may not have a shard, we can't check ahead if the index exists.
std::atomic<bool> index_not_found{false};
std::vector<SearchResult> search_results(shards_count);
std::vector<absl::Duration> profile_results(shards_count);
tx->ScheduleSingleHop([&](Transaction* t, EngineShard* es) {
auto* index = es->search_indices()->GetIndex(index_name);
if (!index) {
index_not_found.store(true, memory_order_relaxed);
return OpStatus::OK;
}
const ShardId shard_id = es->shard_id();
auto shard_start = absl::Now();
search_results[shard_id] = index->Search(t->GetOpArgs(es), *params, &search_algo);
profile_results[shard_id] = {absl::Now() - shard_start};
return OpStatus::OK;
});
if (index_not_found.load())
return builder->SendError(std::string{index_name} + ": no such index");
auto took = absl::Now() - start;
bool result_is_empty = false;
size_t total_docs = 0;
size_t total_serialized = 0;
for (const auto& result : search_results) {
if (!result.error) {
total_docs += result.total_hits;
total_serialized += result.docs.size();
} else {
result_is_empty = true;
}
}
auto* rb = static_cast<RedisReplyBuilder*>(builder);
// First element -> Result of the search command
// Second element -> Profile information
rb->StartArray(2);
// Result of the search command
if (!result_is_empty) {
auto agg = search_algo.HasAggregation();
if (agg) {
ReplySorted(*agg, *params, absl::MakeSpan(search_results), builder);
} else {
ReplyWithResults(*params, absl::MakeSpan(search_results), builder);
}
} else {
rb->StartArray(1);
rb->SendLong(0);
}
// Profile information
rb->StartArray(shards_count + 1);
// General stats
rb->StartCollection(3, RedisReplyBuilder::MAP);
rb->SendBulkString("took");
rb->SendLong(absl::ToInt64Microseconds(took));
rb->SendBulkString("hits");
rb->SendLong(static_cast<long>(total_docs));
rb->SendBulkString("serialized");
rb->SendLong(static_cast<long>(total_serialized));
// Per-shard stats
for (size_t shard_id = 0; shard_id < shards_count; shard_id++) {
rb->StartCollection(2, RedisReplyBuilder::MAP);
rb->SendBulkString("took");
rb->SendLong(absl::ToInt64Microseconds(profile_results[shard_id]));
rb->SendBulkString("tree");
const auto& search_result = search_results[shard_id];
if (search_result.error || !search_result.profile || search_result.profile->events.empty()) {
rb->SendEmptyArray();
continue;
}
const auto& events = search_result.profile->events;
for (size_t i = 0; i < events.size(); i++) {
const auto& event = events[i];
size_t children = 0;
for (size_t j = i + 1; j < events.size(); j++) {
if (events[j].depth == event.depth)
break;
if (events[j].depth == event.depth + 1)
children++;
}
if (children > 0)
rb->StartArray(2);
rb->SendSimpleString(
absl::StrFormat("t=%-10u n=%-10u %s", event.micros, event.num_processed, event.descr));
if (children > 0)
rb->StartArray(children);
}
}
}
void SearchFamily::FtTagVals(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
string_view index_name = ArgS(args, 0);
string_view field_name = ArgS(args, 1);
VLOG(1) << "FtTagVals: " << index_name << " " << field_name;
vector<io::Result<StringVec, ErrorReply>> shard_results(shard_set->size(), StringVec{});
tx->ScheduleSingleHop([&](Transaction* t, EngineShard* es) {
if (auto* index = es->search_indices()->GetIndex(index_name); index)
shard_results[es->shard_id()] = index->GetTagVals(field_name);
else
shard_results[es->shard_id()] = nonstd::make_unexpected(ErrorReply("-Unknown Index name"));
return OpStatus::OK;
});
absl::flat_hash_set<string> result_set;
// Check first if either shard had errors. Also merge the results into a single set.
for (auto& res : shard_results) {
if (res) {
result_set.insert(make_move_iterator(res->begin()), make_move_iterator(res->end()));
} else {
res.error().kind = facade::kSearchErrType;
return builder->SendError(res.error());
}
}
shard_results.clear();
vector<string> vec(result_set.begin(), result_set.end());
auto* rb = static_cast<RedisReplyBuilder*>(builder);
rb->SendBulkStrArr(vec, RedisReplyBuilder::SET);
}
void SearchFamily::FtAggregate(CmdArgList args, Transaction* tx, SinkReplyBuilder* builder) {
const auto params = ParseAggregatorParamsOrReply(args, builder);
if (!params)
return;
search::SearchAlgorithm search_algo;
if (!search_algo.Init(params->query, ¶ms->params, nullptr))
return builder->SendError("Query syntax error");
using ResultContainer = decltype(declval<ShardDocIndex>().SearchForAggregator(
declval<OpArgs>(), params.value(), &search_algo));
vector<ResultContainer> query_results(shard_set->size());
tx->ScheduleSingleHop([&](Transaction* t, EngineShard* es) {
if (auto* index = es->search_indices()->GetIndex(params->index); index) {
query_results[es->shard_id()] =
index->SearchForAggregator(t->GetOpArgs(es), params.value(), &search_algo);
}
return OpStatus::OK;
});
vector<aggregate::DocValues> values;
for (auto& sub_results : query_results) {
values.insert(values.end(), make_move_iterator(sub_results.begin()),
make_move_iterator(sub_results.end()));
}
auto agg_results = aggregate::Process(std::move(values), params->steps);
if (!agg_results.has_value())
return builder->SendError(agg_results.error());
size_t result_size = agg_results->size();
auto* rb = static_cast<RedisReplyBuilder*>(builder);
auto sortable_value_sender = SortableValueSender(rb);
rb->StartArray(result_size + 1);
rb->SendLong(result_size);
for (const auto& result : agg_results.value()) {
rb->StartArray(result.size() * 2);
for (const auto& [k, v] : result) {
rb->SendBulkString(k);
std::visit(sortable_value_sender, v);
}
}