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_custom_scheduling_last_synced(connector_id, schedule_key) ⇒ Object
- .update_connector_fields(connector_id, doc = {}, seq_no = nil, primary_term = nil) ⇒ Object
- .update_connector_status(connector_id, status, error_message = nil) ⇒ Object
- .update_connector_sync_now(connector_id, sync_now) ⇒ Object
- .update_connector_sync_start(connector_id) ⇒ 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
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/core/elastic_connector_actions.rb', line 201 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
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/core/elastic_connector_actions.rb', line 62 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, :ignore => 404).with_indifferent_access index = get_latest_index_in_alias(Utility::Constants::CONNECTORS_INDEX, alias_mappings.keys) alias_mappings.dig(index, 'mappings', '_meta') || { :extract_binary_content => true, :name => 'ent-search-generic-ingestion', :reduce_whitespace => true, :run_ml_inference => false, } end |
.convert_connector_filtering_to_job_filtering(connector_filtering) ⇒ Object
242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/core/elastic_connector_actions.rb', line 242 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
42 43 44 45 46 47 48 49 50 |
# File 'lib/core/elastic_connector_actions.rb', line 42 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
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/core/elastic_connector_actions.rb', line 218 def create_job(connector_settings:) body = { status: Connectors::SyncStatus::PENDING, created_at: Time.now, last_seen: Time.now, trigger_method: connector_settings.sync_now? ? Connectors::JobTriggerMethod::ON_DEMAND : Connectors::JobTriggerMethod::SCHEDULED, 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, configuration: connector_settings.configuration } } 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
107 108 109 |
# File 'lib/core/elastic_connector_actions.rb', line 107 def delete_indices(indices) client.indices.delete(:index => indices, :ignore_unavailable => true) end |
.delete_jobs_by_query(query) ⇒ Object
100 101 102 103 104 105 |
# File 'lib/core/elastic_connector_actions.rb', line 100 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
120 121 122 123 |
# File 'lib/core/elastic_connector_actions.rb', line 120 def disable_connector_scheduling(connector_id) payload = { :enabled => false } update_connector_fields(connector_id, :scheduling => payload) end |
.document_count(index_name) ⇒ Object
546 547 548 549 |
# File 'lib/core/elastic_connector_actions.rb', line 546 def document_count(index_name) client.indices.refresh(:index => index_name, :ignore_unavailable => true) client.count(:index => index_name, :ignore_unavailable => true)['count'] end |
.enable_connector_scheduling(connector_id, cron_expression) ⇒ Object
115 116 117 118 |
# File 'lib/core/elastic_connector_actions.rb', line 115 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
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 |
# File 'lib/core/elastic_connector_actions.rb', line 345 def ensure_connectors_index_exists mappings = { :dynamic => false, :properties => { :api_key_id => { :type => :keyword }, :configuration => { :type => :object }, :custom_schedule => { :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
300 301 302 303 304 305 306 |
# File 'lib/core/elastic_connector_actions.rb', line 300 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
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/core/elastic_connector_actions.rb', line 308 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
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 |
# File 'lib/core/elastic_connector_actions.rb', line 467 def ensure_job_index_exists mappings = { :dynamic => false, :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
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 |
# File 'lib/core/elastic_connector_actions.rb', line 267 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
38 39 40 |
# File 'lib/core/elastic_connector_actions.rb', line 38 def force_sync(connector_id) update_connector_fields(connector_id, :scheduling => { :enabled => true }, :sync_now => true) end |
.get_connector(connector_id) ⇒ Object
52 53 54 55 |
# File 'lib/core/elastic_connector_actions.rb', line 52 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
57 58 59 60 |
# File 'lib/core/elastic_connector_actions.rb', line 57 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
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/core/elastic_connector_actions.rb', line 74 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
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/core/elastic_connector_actions.rb', line 87 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
125 126 127 128 |
# File 'lib/core/elastic_connector_actions.rb', line 125 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
328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
# File 'lib/core/elastic_connector_actions.rb', line 328 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
111 112 113 |
# File 'lib/core/elastic_connector_actions.rb', line 111 def update_connector_configuration(connector_id, configuration) update_connector_fields(connector_id, :configuration => configuration) end |
.update_connector_custom_scheduling_last_synced(connector_id, schedule_key) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/core/elastic_connector_actions.rb', line 182 def update_connector_custom_scheduling_last_synced(connector_id, schedule_key) doc = connector_with_concurrency_control(connector_id) body = { :custom_scheduling => { schedule_key => { :last_synced => Time.now } } } update_connector_fields( connector_id, body, doc[:seq_no], doc[:primary_term] ) end |
.update_connector_fields(connector_id, doc = {}, seq_no = nil, primary_term = nil) ⇒ Object
538 539 540 |
# File 'lib/core/elastic_connector_actions.rb', line 538 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_status(connector_id, status, error_message = nil) ⇒ Object
256 257 258 259 260 261 262 263 264 265 |
# File 'lib/core/elastic_connector_actions.rb', line 256 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
152 153 154 155 156 157 158 159 160 161 162 163 |
# File 'lib/core/elastic_connector_actions.rb', line 152 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_connector_sync_start(connector_id) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/core/elastic_connector_actions.rb', line 165 def update_connector_sync_start(connector_id) doc = connector_with_concurrency_control(connector_id) body = { last_sync_status: Connectors::SyncStatus::IN_PROGRESS, last_sync_error: nil, status: Connectors::ConnectorStatus::CONNECTED } update_connector_fields( connector_id, body, doc[:seq_no], doc[:primary_term] ) end |
.update_filtering_validation(connector_id, filter_validation_results) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/core/elastic_connector_actions.rb', line 130 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
542 543 544 |
# File 'lib/core/elastic_connector_actions.rb', line 542 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 |