Class: Core::ElasticConnectorActions
- Inherits:
-
Object
- Object
- Core::ElasticConnectorActions
- Defined in:
- lib/core/elastic_connector_actions.rb
Class Method Summary collapse
- .connector_with_concurrency_control(connector_id) ⇒ Object
- .connectors_meta ⇒ Object
- .convert_connector_filtering_to_job_filtering(connector_filtering) ⇒ Object
- .create_connector(index_name, service_type) ⇒ Object
- .create_job(connector_settings:) ⇒ Object
- .delete_indices(indices) ⇒ Object
- .delete_jobs_by_query(query) ⇒ Object
- .disable_connector_scheduling(connector_id) ⇒ Object
- .document_count(index_name) ⇒ Object
- .enable_connector_scheduling(connector_id, cron_expression) ⇒ Object
-
.ensure_connectors_index_exists ⇒ Object
DO NOT USE this method Creation of connector index should be handled by Kibana, this method is only used by ftest.rb.
- .ensure_content_index_exists(index_name, use_icu_locale = false, language_code = nil) ⇒ Object
- .ensure_index_exists(index_name, body = {}) ⇒ Object
-
.ensure_job_index_exists ⇒ Object
DO NOT USE this method Creation of job index should be handled by Kibana, this method is only used by ftest.rb.
- .fetch_document_ids(index_name) ⇒ Object
- .force_sync(connector_id) ⇒ Object
- .get_connector(connector_id) ⇒ Object
- .get_job(job_id) ⇒ Object
- .search_connectors(query, page_size, offset) ⇒ Object
- .search_jobs(query, page_size, offset) ⇒ Object
- .set_configurable_field(connector_id, field_name, label, value) ⇒ Object
- .system_index_body(alias_name: nil, mappings: nil) ⇒ Object
- .update_connector_configuration(connector_id, configuration) ⇒ Object
- .update_connector_fields(connector_id, doc = {}, seq_no = nil, primary_term = nil) ⇒ Object
- .update_connector_last_sync_status(connector_id, last_sync_status) ⇒ Object
- .update_connector_status(connector_id, status, error_message = nil) ⇒ Object
- .update_connector_sync_now(connector_id, sync_now) ⇒ Object
- .update_filtering_validation(connector_id, filter_validation_results) ⇒ Object
- .update_job_fields(job_id, doc = {}, seq_no = nil, primary_term = nil) ⇒ Object
Class Method Details
.connector_with_concurrency_control(connector_id) ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/core/elastic_connector_actions.rb', line 170 def connector_with_concurrency_control(connector_id) seq_no = nil primary_term = nil doc = client.get( :index => Utility::Constants::CONNECTORS_INDEX, :id => connector_id, :ignore => 404, :refresh => true ).tap do |response| seq_no = response['_seq_no'] primary_term = response['_primary_term'] end { doc: doc, seq_no: seq_no, primary_term: primary_term } end |
.connectors_meta ⇒ Object
61 62 63 64 65 66 |
# File 'lib/core/elastic_connector_actions.rb', line 61 def # TODO: remove the usage of with_indifferent_access. Ideally this should return a hash or nil if not found alias_mappings = client.indices.get_mapping(:index => Utility::Constants::CONNECTORS_INDEX).with_indifferent_access index = get_latest_index_in_alias(Utility::Constants::CONNECTORS_INDEX, alias_mappings.keys) alias_mappings.dig(index, 'mappings', '_meta') || {} end |
.convert_connector_filtering_to_job_filtering(connector_filtering) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/core/elastic_connector_actions.rb', line 209 def convert_connector_filtering_to_job_filtering(connector_filtering) return [] unless connector_filtering connector_filtering = [connector_filtering] unless connector_filtering.is_a?(Array) connector_filtering.each_with_object([]) do |filtering_domain, job_filtering| snippet = filtering_domain.dig('active', 'advanced_snippet') || {} job_filtering << { 'domain' => filtering_domain['domain'], 'rules' => filtering_domain.dig('active', 'rules'), 'advanced_snippet' => snippet['value'] || snippet, 'warnings' => [] # TODO: in https://github.com/elastic/enterprise-search-team/issues/3174 } end end |
.create_connector(index_name, service_type) ⇒ Object
41 42 43 44 45 46 47 48 49 |
# File 'lib/core/elastic_connector_actions.rb', line 41 def create_connector(index_name, service_type) body = { :scheduling => { :enabled => true }, :index_name => index_name, :service_type => service_type } response = client.index(:index => Utility::Constants::CONNECTORS_INDEX, :body => body) response['_id'] end |
.create_job(connector_settings:) ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/core/elastic_connector_actions.rb', line 187 def create_job(connector_settings:) body = { status: Connectors::SyncStatus::PENDING, created_at: Time.now, last_seen: Time.now, connector: { id: connector_settings.id, filtering: convert_connector_filtering_to_job_filtering(connector_settings.filtering), index_name: connector_settings.index_name, language: connector_settings[:language], pipeline: connector_settings[:pipeline], service_type: connector_settings.service_type } } index_response = client.index(index: Utility::Constants::JOB_INDEX, body: body, refresh: true) return index_response if index_response['result'] == 'created' raise JobNotCreatedError.new(connector_settings.id, index_response) end |
.delete_indices(indices) ⇒ Object
101 102 103 |
# File 'lib/core/elastic_connector_actions.rb', line 101 def delete_indices(indices) client.indices.delete(:index => indices, :ignore_unavailable => true) end |
.delete_jobs_by_query(query) ⇒ Object
94 95 96 97 98 99 |
# File 'lib/core/elastic_connector_actions.rb', line 94 def delete_jobs_by_query(query) client.delete_by_query( :index => Utility::Constants::JOB_INDEX, :body => { :query => query } ) end |
.disable_connector_scheduling(connector_id) ⇒ Object
114 115 116 117 |
# File 'lib/core/elastic_connector_actions.rb', line 114 def disable_connector_scheduling(connector_id) payload = { :enabled => false } update_connector_fields(connector_id, :scheduling => payload) end |
.document_count(index_name) ⇒ Object
510 511 512 513 |
# File 'lib/core/elastic_connector_actions.rb', line 510 def document_count(index_name) client.indices.refresh(:index => index_name) client.count(:index => index_name)['count'] end |
.enable_connector_scheduling(connector_id, cron_expression) ⇒ Object
109 110 111 112 |
# File 'lib/core/elastic_connector_actions.rb', line 109 def enable_connector_scheduling(connector_id, cron_expression) payload = { :enabled => true, :interval => cron_expression } update_connector_fields(connector_id, :scheduling => payload) end |
.ensure_connectors_index_exists ⇒ Object
DO NOT USE this method Creation of connector index should be handled by Kibana, this method is only used by ftest.rb
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 |
# File 'lib/core/elastic_connector_actions.rb', line 312 def ensure_connectors_index_exists mappings = { :properties => { :api_key_id => { :type => :keyword }, :configuration => { :type => :object }, :description => { :type => :text }, :error => { :type => :keyword }, :features => { :properties => { :filtering_advanced_config => { :type => :boolean }, :filtering_rules => { :type => :boolean } } }, :filtering => { :properties => { :domain => { :type => :keyword }, :active => { :properties => { :rules => { :properties => { :id => { :type => :keyword }, :policy => { :type => :keyword }, :field => { :type => :keyword }, :rule => { :type => :keyword }, :value => { :type => :keyword }, :order => { :type => :short }, :created_at => { :type => :date }, :updated_at => { :type => :date } } }, :advanced_snippet => { :properties => { :value => { :type => :object }, :created_at => { :type => :date }, :updated_at => { :type => :date } } }, :validation => { :properties => { :state => { :type => :keyword }, :errors => { :properties => { :ids => { :type => :keyword }, :messages => { :type => :text } } } } } } }, :draft => { :properties => { :rules => { :properties => { :id => { :type => :keyword }, :policy => { :type => :keyword }, :field => { :type => :keyword }, :rule => { :type => :keyword }, :value => { :type => :keyword }, :order => { :type => :short }, :created_at => { :type => :date }, :updated_at => { :type => :date } } }, :advanced_snippet => { :properties => { :value => { :type => :object }, :created_at => { :type => :date }, :updated_at => { :type => :date } } }, :validation => { :properties => { :state => { :type => :keyword }, :errors => { :properties => { :ids => { :type => :keyword }, :messages => { :type => :text } } } } } } } } }, :index_name => { :type => :keyword }, :is_native => { :type => :boolean }, :language => { :type => :keyword }, :last_seen => { :type => :date }, :last_sync_error => { :type => :keyword }, :last_sync_status => { :type => :keyword }, :last_synced => { :type => :date }, :last_deleted_document_count => { :type => :long }, :last_indexed_document_count => { :type => :long }, :name => { :type => :keyword }, :pipeline => { :properties => { :extract_binary_content => { :type => :boolean }, :name => { :type => :keyword }, :reduce_whitespace => { :type => :boolean }, :run_ml_inference => { :type => :boolean } } }, :scheduling => { :properties => { :enabled => { :type => :boolean }, :interval => { :type => :text } } }, :service_type => { :type => :keyword }, :status => { :type => :keyword }, :sync_now => { :type => :boolean } } } ensure_index_exists("#{Utility::Constants::CONNECTORS_INDEX}-v1", system_index_body(:alias_name => Utility::Constants::CONNECTORS_INDEX, :mappings => mappings)) end |
.ensure_content_index_exists(index_name, use_icu_locale = false, language_code = nil) ⇒ Object
267 268 269 270 271 272 273 |
# File 'lib/core/elastic_connector_actions.rb', line 267 def ensure_content_index_exists(index_name, use_icu_locale = false, language_code = nil) settings = Utility::Elasticsearch::Index::TextAnalysisSettings.new(:language_code => language_code, :analysis_icu => use_icu_locale).to_h mappings = Utility::Elasticsearch::Index::Mappings.default_text_fields_mappings(:connectors_index => true) body_payload = { settings: settings, mappings: mappings } ensure_index_exists(index_name, body_payload) end |
.ensure_index_exists(index_name, body = {}) ⇒ Object
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/core/elastic_connector_actions.rb', line 275 def ensure_index_exists(index_name, body = {}) if client.indices.exists?(:index => index_name) return unless body[:mappings] Utility::Logger.debug("Index #{index_name} already exists. Checking mappings...") Utility::Logger.debug("New mappings: #{body[:mappings]}") response = client.indices.get_mapping(:index => index_name) existing = response[index_name]['mappings'] if existing.empty? Utility::Logger.debug("Index #{index_name} has no mappings. Adding mappings...") client.indices.put_mapping(:index => index_name, :body => body[:mappings], :expand_wildcards => 'all') Utility::Logger.debug("Index #{index_name} mappings added.") else Utility::Logger.debug("Index #{index_name} already has mappings: #{existing}. Skipping...") end else client.indices.create(:index => index_name, :body => body) Utility::Logger.debug("Created index #{index_name}") end end |
.ensure_job_index_exists ⇒ Object
DO NOT USE this method Creation of job index should be handled by Kibana, this method is only used by ftest.rb
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 |
# File 'lib/core/elastic_connector_actions.rb', line 432 def ensure_job_index_exists mappings = { :properties => { :cancelation_requested_at => { :type => :date }, :canceled_at => { :type => :date }, :completed_at => { :type => :date }, :connector => { :properties => { :configuration => { :type => :object }, :filtering => { :properties => { :domain => { :type => :keyword }, :rules => { :properties => { :id => { :type => :keyword }, :policy => { :type => :keyword }, :field => { :type => :keyword }, :rule => { :type => :keyword }, :value => { :type => :keyword }, :order => { :type => :short }, :created_at => { :type => :date }, :updated_at => { :type => :date } } }, :advanced_snippet => { :properties => { :value => { :type => :object }, :created_at => { :type => :date }, :updated_at => { :type => :date } } }, :warnings => { :properties => { :ids => { :type => :keyword }, :messages => { :type => :text } } } } }, :id => { :type => :keyword }, :index_name => { :type => :keyword }, :language => { :type => :keyword }, :pipeline => { :properties => { :extract_binary_content => { :type => :boolean }, :name => { :type => :keyword }, :reduce_whitespace => { :type => :boolean }, :run_ml_inference => { :type => :boolean } } }, :service_type => { :type => :keyword } } }, :created_at => { :type => :date }, :deleted_document_count => { :type => :integer }, :error => { :type => :text }, :indexed_document_count => { :type => :integer }, :indexed_document_volume => { :type => :integer }, :last_seen => { :type => :date }, :metadata => { :type => :object }, :started_at => { :type => :date }, :status => { :type => :keyword }, :total_document_count => { :type => :integer }, :trigger_method => { :type => :keyword }, :worker_hostname => { :type => :keyword } } } ensure_index_exists("#{Utility::Constants::JOB_INDEX}-v1", system_index_body(:alias_name => Utility::Constants::JOB_INDEX, :mappings => mappings)) end |
.fetch_document_ids(index_name) ⇒ Object
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 |
# File 'lib/core/elastic_connector_actions.rb', line 234 def fetch_document_ids(index_name) page_size = 1000 result = [] begin pit_id = client.open_point_in_time(:index => index_name, :keep_alive => '1m', :expand_wildcards => 'all')['id'] body = { :query => { :match_all => {} }, :sort => [{ :id => { :order => :asc } }], :pit => { :id => pit_id, :keep_alive => '1m' }, :size => page_size, :_source => false } loop do response = client.search(:body => body) hits = response.dig('hits', 'hits') || [] ids = hits.map { |h| h['_id'] } result += ids break if hits.size < page_size body[:search_after] = hits.last['sort'] body[:pit][:id] = response['pit_id'] end ensure client.close_point_in_time(:index => index_name, :body => { :id => pit_id }) end result end |
.force_sync(connector_id) ⇒ Object
37 38 39 |
# File 'lib/core/elastic_connector_actions.rb', line 37 def force_sync(connector_id) update_connector_fields(connector_id, :scheduling => { :enabled => true }, :sync_now => true) end |
.get_connector(connector_id) ⇒ Object
51 52 53 54 |
# File 'lib/core/elastic_connector_actions.rb', line 51 def get_connector(connector_id) # TODO: remove the usage of with_indifferent_access. Ideally this should return a hash or nil if not found client.get(:index => Utility::Constants::CONNECTORS_INDEX, :id => connector_id, :ignore => 404).with_indifferent_access end |
.get_job(job_id) ⇒ Object
56 57 58 59 |
# File 'lib/core/elastic_connector_actions.rb', line 56 def get_job(job_id) # TODO: remove the usage of with_indifferent_access. Ideally this should return a hash or nil if not found client.get(:index => Utility::Constants::JOB_INDEX, :id => job_id, :ignore => 404).with_indifferent_access end |
.search_connectors(query, page_size, offset) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/core/elastic_connector_actions.rb', line 68 def search_connectors(query, page_size, offset) client.search( :index => Utility::Constants::CONNECTORS_INDEX, :ignore => 404, :body => { :size => page_size, :from => offset, :query => query, :sort => ['name'] } ) end |
.search_jobs(query, page_size, offset) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/core/elastic_connector_actions.rb', line 81 def search_jobs(query, page_size, offset) client.search( :index => Utility::Constants::JOB_INDEX, :ignore => 404, :body => { :size => page_size, :from => offset, :query => query, :sort => ['created_at'] } ) end |
.set_configurable_field(connector_id, field_name, label, value) ⇒ Object
119 120 121 122 |
# File 'lib/core/elastic_connector_actions.rb', line 119 def set_configurable_field(connector_id, field_name, label, value) payload = { field_name => { :value => value, :label => label } } update_connector_configuration(connector_id, payload) end |
.system_index_body(alias_name: nil, mappings: nil) ⇒ Object
295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/core/elastic_connector_actions.rb', line 295 def system_index_body(alias_name: nil, mappings: nil) body = { :settings => { :index => { :hidden => true, :number_of_replicas => 0, :auto_expand_replicas => '0-5' } } } body[:aliases] = { alias_name => { :is_write_index => true } } unless alias_name.nil? || alias_name.empty? body[:mappings] = mappings unless mappings.nil? body end |
.update_connector_configuration(connector_id, configuration) ⇒ Object
105 106 107 |
# File 'lib/core/elastic_connector_actions.rb', line 105 def update_connector_configuration(connector_id, configuration) update_connector_fields(connector_id, :configuration => configuration) end |
.update_connector_fields(connector_id, doc = {}, seq_no = nil, primary_term = nil) ⇒ Object
502 503 504 |
# File 'lib/core/elastic_connector_actions.rb', line 502 def update_connector_fields(connector_id, doc = {}, seq_no = nil, primary_term = nil) update_doc_fields(Utility::Constants::CONNECTORS_INDEX, connector_id, doc, seq_no, primary_term) end |
.update_connector_last_sync_status(connector_id, last_sync_status) ⇒ Object
159 160 161 162 163 164 165 166 167 168 |
# File 'lib/core/elastic_connector_actions.rb', line 159 def update_connector_last_sync_status(connector_id, last_sync_status) doc = connector_with_concurrency_control(connector_id) update_connector_fields( connector_id, { last_sync_status: last_sync_status }, doc[:seq_no], doc[:primary_term] ) end |
.update_connector_status(connector_id, status, error_message = nil) ⇒ Object
223 224 225 226 227 228 229 230 231 232 |
# File 'lib/core/elastic_connector_actions.rb', line 223 def update_connector_status(connector_id, status, = nil) if status == Connectors::ConnectorStatus::ERROR && .nil? raise ArgumentError, 'error_message is required when status is error' end body = { :status => status, :error => status == Connectors::ConnectorStatus::ERROR ? : nil } update_connector_fields(connector_id, body) end |
.update_connector_sync_now(connector_id, sync_now) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/core/elastic_connector_actions.rb', line 146 def update_connector_sync_now(connector_id, sync_now) doc = connector_with_concurrency_control(connector_id) body = { sync_now: sync_now, last_synced: Time.now } update_connector_fields( connector_id, body, doc[:seq_no], doc[:primary_term] ) end |
.update_filtering_validation(connector_id, filter_validation_results) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/core/elastic_connector_actions.rb', line 124 def update_filtering_validation(connector_id, filter_validation_results) return if filter_validation_results.empty? filtering = get_connector(connector_id).dig(:_source, :filtering) case filtering when Hash update_filter_validation(filtering, filter_validation_results) when Array return unless should_update_validations?(filter_validation_results, filtering) filtering.each do |filter| update_filter_validation(filter, filter_validation_results) end else Utility::Logger.warn("Elasticsearch returned invalid filtering format: #{filtering}. Skipping validation.") return end update_connector_fields(connector_id, { :filtering => filtering }) end |
.update_job_fields(job_id, doc = {}, seq_no = nil, primary_term = nil) ⇒ Object
506 507 508 |
# File 'lib/core/elastic_connector_actions.rb', line 506 def update_job_fields(job_id, doc = {}, seq_no = nil, primary_term = nil) update_doc_fields(Utility::Constants::JOB_INDEX, job_id, doc, seq_no, primary_term) end |