diff --git a/src/query/db_accessor.hpp b/src/query/db_accessor.hpp index d47621811..48b3195b0 100644 --- a/src/query/db_accessor.hpp +++ b/src/query/db_accessor.hpp @@ -576,9 +576,7 @@ class DbAccessor final { void TextIndexAddVertex(const VertexAccessor &vertex) { accessor_->TextIndexAddVertex(vertex.impl_); } - void TextIndexUpdateVertex(const VertexAccessor &vertex) { accessor_->TextIndexUpdateVertex(vertex.impl_); } - - void TextIndexUpdateVertex(const VertexAccessor &vertex, const std::vector &removed_labels) { + void TextIndexUpdateVertex(const VertexAccessor &vertex, const std::vector &removed_labels = {}) { accessor_->TextIndexUpdateVertex(vertex.impl_, removed_labels); } diff --git a/src/storage/v2/indices/text_index.cpp b/src/storage/v2/indices/text_index.cpp index 54597f530..a995c0674 100644 --- a/src/storage/v2/indices/text_index.cpp +++ b/src/storage/v2/indices/text_index.cpp @@ -94,6 +94,10 @@ std::vector TextIndex::GetApplicableTextIndices(c void TextIndex::LoadNodeToTextIndices(const std::int64_t gid, const nlohmann::json &properties, const std::vector &applicable_text_indices) { + if (applicable_text_indices.empty()) { + return; + } + // NOTE: Text indexes are presently all-property indices. If we allow text indexes restricted to specific properties, // an indexable document should be created for each applicable index. nlohmann::json document = {}; @@ -128,24 +132,14 @@ void TextIndex::CommitLoadedNodes(mgcxx::text_search::Context &index_context) { } void TextIndex::AddNode(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, - const std::vector &applicable_text_indices) { + std::optional> applicable_text_indices) { if (!flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { throw query::TextSearchDisabledException(); } auto vertex_properties = vertex_after_update->properties.Properties(); LoadNodeToTextIndices(vertex_after_update->gid.AsInt(), SerializeProperties(vertex_properties, name_id_mapper), - applicable_text_indices); -} - -void TextIndex::AddNode(Vertex *vertex_after_update, NameIdMapper *name_id_mapper) { - if (!flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { - throw query::TextSearchDisabledException(); - } - - auto applicable_text_indices = GetApplicableTextIndices(vertex_after_update->labels); - if (applicable_text_indices.empty()) return; - AddNode(vertex_after_update, name_id_mapper, applicable_text_indices); + applicable_text_indices.value_or(GetApplicableTextIndices(vertex_after_update->labels))); } void TextIndex::UpdateNode(Vertex *vertex_after_update, NameIdMapper *name_id_mapper, @@ -166,7 +160,7 @@ void TextIndex::UpdateNode(Vertex *vertex_after_update, NameIdMapper *name_id_ma } void TextIndex::RemoveNode(Vertex *vertex_after_update, - const std::vector &applicable_text_indices) { + std::optional> applicable_text_indices) { if (!flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { throw query::TextSearchDisabledException(); } @@ -174,7 +168,7 @@ void TextIndex::RemoveNode(Vertex *vertex_after_update, auto search_node_to_be_deleted = mgcxx::text_search::SearchInput{.search_query = fmt::format("metadata.gid:{}", vertex_after_update->gid.AsInt())}; - for (auto *index_context : applicable_text_indices) { + for (auto *index_context : applicable_text_indices.value_or(GetApplicableTextIndices(vertex_after_update->labels))) { try { mgcxx::text_search::delete_document(*index_context, search_node_to_be_deleted, kDoSkipCommit); } catch (const std::exception &e) { @@ -183,17 +177,11 @@ void TextIndex::RemoveNode(Vertex *vertex_after_update, } } -void TextIndex::RemoveNode(Vertex *vertex_after_update) { +void TextIndex::CreateIndex(const std::string &index_name, LabelId label, memgraph::query::DbAccessor *db) { if (!flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { throw query::TextSearchDisabledException(); } - auto applicable_text_indices = GetApplicableTextIndices(vertex_after_update->labels); - if (applicable_text_indices.empty()) return; - RemoveNode(vertex_after_update, applicable_text_indices); -} - -void TextIndex::CreateIndex(const std::string &index_name, LabelId label, memgraph::query::DbAccessor *db) { CreateEmptyIndex(index_name, label); for (const auto &v : db->Vertices(View::NEW)) { @@ -211,6 +199,10 @@ void TextIndex::CreateIndex(const std::string &index_name, LabelId label, memgra void TextIndex::RecoverIndex(const std::string &index_name, LabelId label, memgraph::utils::SkipList::Accessor vertices, NameIdMapper *name_id_mapper) { + if (!flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { + throw query::TextSearchDisabledException(); + } + CreateEmptyIndex(index_name, label); for (const auto &v : vertices) { @@ -218,7 +210,6 @@ void TextIndex::RecoverIndex(const std::string &index_name, LabelId label, continue; } - nlohmann::json document = {}; auto vertex_properties = v.properties.Properties(); LoadNodeToTextIndices(v.gid.AsInt(), SerializeProperties(vertex_properties, name_id_mapper), {&index_.at(index_name).context_}); @@ -286,12 +277,20 @@ std::vector TextIndex::Search(const std::string &index_name, const std::str } void TextIndex::Commit() { + if (!flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { + throw query::TextSearchDisabledException(); + } + for (auto &[_, index_data] : index_) { mgcxx::text_search::commit(index_data.context_); } } void TextIndex::Rollback() { + if (!flags::AreExperimentsEnabled(flags::Experiments::TEXT_SEARCH)) { + throw query::TextSearchDisabledException(); + } + for (auto &[_, index_data] : index_) { mgcxx::text_search::rollback(index_data.context_); } diff --git a/src/storage/v2/indices/text_index.hpp b/src/storage/v2/indices/text_index.hpp index b57a6ebd5..178e40669 100644 --- a/src/storage/v2/indices/text_index.hpp +++ b/src/storage/v2/indices/text_index.hpp @@ -43,11 +43,6 @@ class TextIndex { void CommitLoadedNodes(mgcxx::text_search::Context &index_context); - void AddNode(Vertex *vertex, NameIdMapper *name_id_mapper, - const std::vector &applicable_text_indices); - - void RemoveNode(Vertex *vertex, const std::vector &applicable_text_indices); - public: TextIndex() = default; @@ -61,11 +56,13 @@ class TextIndex { std::map index_; std::map label_to_index_; - void AddNode(Vertex *vertex, NameIdMapper *name_id_mapper); + void AddNode(Vertex *vertex, NameIdMapper *name_id_mapper, + std::optional> applicable_text_indices = std::nullopt); void UpdateNode(Vertex *vertex, NameIdMapper *name_id_mapper, const std::vector &removed_labels = {}); - void RemoveNode(Vertex *vertex); + void RemoveNode(Vertex *vertex, + std::optional> applicable_text_indices = std::nullopt); void CreateIndex(const std::string &index_name, LabelId label, memgraph::query::DbAccessor *db); diff --git a/src/storage/v2/storage.hpp b/src/storage/v2/storage.hpp index 86020ff25..25a2e836d 100644 --- a/src/storage/v2/storage.hpp +++ b/src/storage/v2/storage.hpp @@ -236,11 +236,7 @@ class Storage { storage_->indices_.text_index_.AddNode(vertex.vertex_, storage_->name_id_mapper_.get()); } - void TextIndexUpdateVertex(const VertexAccessor &vertex) { - storage_->indices_.text_index_.UpdateNode(vertex.vertex_, storage_->name_id_mapper_.get()); - } - - void TextIndexUpdateVertex(const VertexAccessor &vertex, std::vector removed_labels) { + void TextIndexUpdateVertex(const VertexAccessor &vertex, const std::vector &removed_labels = {}) { storage_->indices_.text_index_.UpdateNode(vertex.vertex_, storage_->name_id_mapper_.get(), removed_labels); }