|
| 1 | +# Copyright 2024. Couchbase, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +module Couchbase |
| 16 | + module Management |
| 17 | + # @api volatile |
| 18 | + class ScopeSearchIndexManager |
| 19 | + alias inspect to_s |
| 20 | + |
| 21 | + # @param [Couchbase::Backend] backend |
| 22 | + # @param [String] bucket_name |
| 23 | + # @param [String] scope_name |
| 24 | + def initialize(backend, bucket_name, scope_name) |
| 25 | + @backend = backend |
| 26 | + @bucket_name = bucket_name |
| 27 | + @scope_name = scope_name |
| 28 | + end |
| 29 | + |
| 30 | + # Fetches an index from the server if it exists |
| 31 | + # |
| 32 | + # @param [String] index_name name of the index |
| 33 | + # @param [GetIndexOptions] options |
| 34 | + # |
| 35 | + # @return [SearchIndex] |
| 36 | + # |
| 37 | + # @raise [ArgumentError] |
| 38 | + # @raise [Error::IndexNotFound] |
| 39 | + def get_index(index_name, options = GetIndexOptions.new) |
| 40 | + res = @backend.search_index_get(@bucket_name, @scope_name, index_name, options.timeout) |
| 41 | + SearchIndexManager.extract_search_index(res) |
| 42 | + end |
| 43 | + |
| 44 | + # Fetches all indexes from the server |
| 45 | + # |
| 46 | + # @param [GetAllIndexesOptions] options |
| 47 | + # |
| 48 | + # @return [Array<SearchIndex>] |
| 49 | + def get_all_indexes(options = GetAllIndexesOptions.new) |
| 50 | + res = @backend.search_index_get_all(@bucket_name, @scope_name, options.timeout) |
| 51 | + res[:indexes].map { |idx| SearchIndexManager.extract_search_index(idx) } |
| 52 | + end |
| 53 | + |
| 54 | + # Creates or updates the index |
| 55 | + # |
| 56 | + # @param [SearchIndex] index_definition the index definition |
| 57 | + # @param [UpsertIndexOptions] options |
| 58 | + # |
| 59 | + # @return void |
| 60 | + # |
| 61 | + # @raise [ArgumentError] if name, type or source_type is empty |
| 62 | + def upsert_index(index_definition, options = UpsertIndexOptions.new) |
| 63 | + @backend.search_index_upsert(@bucket_name, @scope_name, index_definition.to_backend, options.timeout) |
| 64 | + end |
| 65 | + |
| 66 | + # Drops the index |
| 67 | + # |
| 68 | + # @param [String] index_name name of the index |
| 69 | + # @param [DropIndexOptions] options |
| 70 | + # |
| 71 | + # @return void |
| 72 | + # |
| 73 | + # @raise [ArgumentError] |
| 74 | + # @raise [Error::IndexNotFound] |
| 75 | + def drop_index(index_name, options = DropIndexOptions.new) |
| 76 | + @backend.search_index_drop(@bucket_name, @scope_name, index_name, options.timeout) |
| 77 | + end |
| 78 | + |
| 79 | + # Retrieves the number of documents that have been indexed for an index |
| 80 | + # |
| 81 | + # @param [String] index_name name of the index |
| 82 | + # @param [GetIndexedDocumentsCountOptions] options |
| 83 | + # |
| 84 | + # @return [Integer] |
| 85 | + # |
| 86 | + # @raise [ArgumentError] |
| 87 | + # @raise [Error::IndexNotFound] |
| 88 | + def get_indexed_documents_count(index_name, options = GetIndexedDocumentsCountOptions.new) |
| 89 | + res = @backend.search_index_get_documents_count(@bucket_name, @scope_name, index_name, options.timeout) |
| 90 | + res[:count] |
| 91 | + end |
| 92 | + |
| 93 | + # Pauses updates and maintenance for the index |
| 94 | + # |
| 95 | + # @param [String] index_name name of the index |
| 96 | + # @param [PauseIngestOptions] options |
| 97 | + # |
| 98 | + # @return void |
| 99 | + # |
| 100 | + # @raise [ArgumentError] |
| 101 | + # @raise [Error::IndexNotFound] |
| 102 | + def pause_ingest(index_name, options = PauseIngestOptions.new) |
| 103 | + @backend.search_index_pause_ingest(@bucket_name, @scope_name, index_name, options.timeout) |
| 104 | + end |
| 105 | + |
| 106 | + # Resumes updates and maintenance for an index |
| 107 | + # |
| 108 | + # @param [String] index_name name of the index |
| 109 | + # @param [ResumeIngestOptions] options |
| 110 | + # |
| 111 | + # @return void |
| 112 | + # |
| 113 | + # @raise [ArgumentError] |
| 114 | + # @raise [Error::IndexNotFound] |
| 115 | + def resume_ingest(index_name, options = ResumeIngestOptions.new) |
| 116 | + @backend.search_index_resume_ingest(@bucket_name, @scope_name, index_name, options.timeout) |
| 117 | + end |
| 118 | + |
| 119 | + # Allows querying against the index |
| 120 | + # |
| 121 | + # @param [String] index_name name of the index |
| 122 | + # @param [AllowQueryingOptions] options |
| 123 | + # |
| 124 | + # @return void |
| 125 | + # |
| 126 | + # @raise [ArgumentError] |
| 127 | + # @raise [Error::IndexNotFound] |
| 128 | + def allow_querying(index_name, options = AllowQueryingOptions.new) |
| 129 | + @backend.search_index_allow_querying(@bucket_name, @scope_name, index_name, options.timeout) |
| 130 | + end |
| 131 | + |
| 132 | + # Disallows querying against the index |
| 133 | + # |
| 134 | + # @param [String] index_name name of the index |
| 135 | + # @param [DisallowQueryingOptions] options |
| 136 | + # |
| 137 | + # @return void |
| 138 | + # |
| 139 | + # @raise [ArgumentError] |
| 140 | + # @raise [Error::IndexNotFound] |
| 141 | + def disallow_querying(index_name, options = DisallowQueryingOptions.new) |
| 142 | + @backend.search_index_disallow_querying(@bucket_name, @scope_name, index_name, options.timeout) |
| 143 | + end |
| 144 | + |
| 145 | + # Freeze the assignment of index partitions to nodes |
| 146 | + # |
| 147 | + # @param [String] index_name name of the index |
| 148 | + # @param [FreezePlanOptions] options |
| 149 | + # |
| 150 | + # @return void |
| 151 | + # |
| 152 | + # @raise [ArgumentError] |
| 153 | + # @raise [Error::IndexNotFound] |
| 154 | + def freeze_plan(index_name, options = FreezePlanOptions.new) |
| 155 | + @backend.search_index_freeze_plan(@bucket_name, @scope_name, index_name, options.timeout) |
| 156 | + end |
| 157 | + |
| 158 | + # Unfreeze the assignment of index partitions to nodes |
| 159 | + # |
| 160 | + # @param [String] index_name name of the index |
| 161 | + # @param [UnfreezePlanOptions] options |
| 162 | + # |
| 163 | + # @return void |
| 164 | + # |
| 165 | + # @raise [ArgumentError] |
| 166 | + # @raise [Error::IndexNotFound] |
| 167 | + def unfreeze_plan(index_name, options = UnfreezePlanOptions.new) |
| 168 | + @backend.search_index_unfreeze_plan(@bucket_name, @scope_name, index_name, options.timeout) |
| 169 | + end |
| 170 | + |
| 171 | + # Allows to see how a document is analyzed against a specific index |
| 172 | + # |
| 173 | + # @param [String] index_name name of the index |
| 174 | + # @param [Hash] document the document to be analyzed |
| 175 | + # |
| 176 | + # @return [Array<Hash>] |
| 177 | + # |
| 178 | + # @raise [ArgumentError] |
| 179 | + # @raise [Error::IndexNotFound] |
| 180 | + def analyze_document(index_name, document, options = AnalyzeDocumentOptions.new) |
| 181 | + res = @backend.search_index_analyze_document(@bucket_name, @scope_name, index_name, JSON.generate(document), options.timeout) |
| 182 | + JSON.parse(res[:analysis]) |
| 183 | + end |
| 184 | + |
| 185 | + GetIndexOptions = SearchIndexManager::GetIndexOptions |
| 186 | + GetAllIndexesOptions = SearchIndexManager::GetAllIndexesOptions |
| 187 | + UpsertIndexOptions = SearchIndexManager::UpsertIndexOptions |
| 188 | + DropIndexOptions = SearchIndexManager::DropIndexOptions |
| 189 | + GetIndexedDocumentsCountOptions = SearchIndexManager::GetIndexedDocumentsCountOptions |
| 190 | + PauseIngestOptions = SearchIndexManager::PauseIngestOptions |
| 191 | + ResumeIngestOptions = SearchIndexManager::ResumeIngestOptions |
| 192 | + AllowQueryingOptions = SearchIndexManager::AllowQueryingOptions |
| 193 | + DisallowQueryingOptions = SearchIndexManager::DisallowQueryingOptions |
| 194 | + FreezePlanOptions = SearchIndexManager::FreezePlanOptions |
| 195 | + UnfreezePlanOptions = SearchIndexManager::UnfreezePlanOptions |
| 196 | + AnalyzeDocumentOptions = SearchIndexManager::AnalyzeDocumentOptions |
| 197 | + end |
| 198 | + end |
| 199 | +end |
0 commit comments