From 079d6c8f22d83c0afb9b31c586950133c5e2702d Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 19 Nov 2021 10:18:10 -0800 Subject: [PATCH] Added experimental support for opensearch gem --- CHANGELOG.md | 4 ++++ gemfiles/opensearch.gemfile | 2 +- lib/searchkick.rb | 13 +++++++++++++ lib/searchkick/index.rb | 6 ++++-- lib/searchkick/query.rb | 2 +- lib/searchkick/record_indexer.rb | 5 +++-- lib/searchkick/results.rb | 8 ++++---- test/test_helper.rb | 6 +++++- 8 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6113a77..c0c44bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 4.6.3 (unreleased) + +- Added experimental support for `opensearch` gem + ## 4.6.2 (2021-11-15) - Added support for beginless ranges to `where` option diff --git a/gemfiles/opensearch.gemfile b/gemfiles/opensearch.gemfile index 4eea1cc..e317fe5 100644 --- a/gemfiles/opensearch.gemfile +++ b/gemfiles/opensearch.gemfile @@ -13,5 +13,5 @@ gem "typhoeus" gem "redis" gem "connection_pool" gem "kaminari" -gem "elasticsearch-xpack", ">= 7.8", "< 7.14" +gem "opensearch", github: "opensearch-project/opensearch-ruby" gem "parallel_tests" diff --git a/lib/searchkick.rb b/lib/searchkick.rb index 42431b0..b7bf2b5 100644 --- a/lib/searchkick.rb +++ b/lib/searchkick.rb @@ -34,6 +34,7 @@ module Searchkick class Error < StandardError; end class MissingIndexError < Error; end class UnsupportedVersionError < Error; end + # TODO switch to Error class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end class DangerousOperation < Error; end class ImportError < Error; end @@ -278,6 +279,18 @@ module Searchkick !Mongoid::Threaded.current_scope(klass).nil? end end + + # private + def self.not_found_error?(e) + (defined?(Elasticsearch) && e.is_a?(Elasticsearch::Transport::Transport::Errors::NotFound)) || + (defined?(OpenSearch) && e.is_a?(OpenSearch::Transport::Transport::Errors::NotFound)) + end + + # private + def self.transport_error?(e) + (defined?(Elasticsearch) && e.is_a?(Elasticsearch::Transport::Transport::Error)) || + (defined?(OpenSearch) && e.is_a?(OpenSearch::Transport::Transport::Error)) + end end require "active_model/callbacks" diff --git a/lib/searchkick/index.rb b/lib/searchkick/index.rb index 990f005..14d7633 100644 --- a/lib/searchkick/index.rb +++ b/lib/searchkick/index.rb @@ -84,7 +84,8 @@ module Searchkick old_indices = begin client.indices.get_alias(name: name).keys - rescue Elasticsearch::Transport::Transport::Errors::NotFound + rescue => e + raise e unless Searchkick.not_found_error?(e) {} end actions = old_indices.map { |old_name| {remove: {index: old_name, alias: name}} } + [{add: {index: new_name, alias: name}}] @@ -109,7 +110,8 @@ module Searchkick else client.indices.get_aliases end - rescue Elasticsearch::Transport::Transport::Errors::NotFound + rescue => e + raise e unless Searchkick.not_found_error?(e) {} end indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index fb717b0..e6a53dd 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -113,7 +113,7 @@ module Searchkick # no easy way to tell which host the client will use host = - if Gem::Version.new(Elasticsearch::VERSION) >= Gem::Version.new("7.14.0") + if Searchkick.client.transport.respond_to?(:transport) Searchkick.client.transport.transport.hosts.first else Searchkick.client.transport.hosts.first diff --git a/lib/searchkick/record_indexer.rb b/lib/searchkick/record_indexer.rb index 7b5842e..000e1f4 100644 --- a/lib/searchkick/record_indexer.rb +++ b/lib/searchkick/record_indexer.rb @@ -64,8 +64,9 @@ module Searchkick if record.destroyed? || !record.persisted? || !record.should_index? begin index.remove(record) - rescue Elasticsearch::Transport::Transport::Errors::NotFound - # do nothing + rescue => e + raise e unless Searchkick.not_found_error?(e) + # do nothing if not found end else if method_name diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 977baf5..c02480d 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -195,8 +195,8 @@ module Searchkick begin # TODO Active Support notifications for this scroll call Searchkick::Results.new(@klass, Searchkick.client.scroll(scroll: options[:scroll], body: {scroll_id: scroll_id}), @options) - rescue Elasticsearch::Transport::Transport::Errors::NotFound => e - if e.class.to_s =~ /NotFound/ && e.message =~ /search_context_missing_exception/i + rescue => e + if Searchkick.not_found_error?(e) && e.message =~ /search_context_missing_exception/i raise Searchkick::Error, "Scroll id has expired" else raise e @@ -211,8 +211,8 @@ module Searchkick # not required as scroll will expire # but there is a cost to open scrolls Searchkick.client.clear_scroll(scroll_id: scroll_id) - rescue Elasticsearch::Transport::Transport::Error - # do nothing + rescue => e + raise e unless Searchkick.transport_error?(e) end end diff --git a/test/test_helper.rb b/test/test_helper.rb index b34cd0b..7b473e0 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -12,7 +12,11 @@ ENV["ES_PATH"] ||= "#{ENV["HOME"]}/elasticsearch/#{ENV["ELASTICSEARCH_VERSION"]} $logger = ActiveSupport::Logger.new(ENV["VERBOSE"] ? STDOUT : nil) -if Gem::Version.new(Elasticsearch::VERSION) >= Gem::Version.new("7.14.0") +if defined?(OpenSearch) + Searchkick.client = OpenSearch::Client.new +end + +if defined?(OpenSearch) || Gem::Version.new(Elasticsearch::VERSION) >= Gem::Version.new("7.14.0") Searchkick.client.transport.transport.logger = $logger else Searchkick.client.transport.logger = $logger -- libgit2 0.21.0