Commit 079d6c8f22d83c0afb9b31c586950133c5e2702d

Authored by Andrew Kane
1 parent 70e5fd1d

Added experimental support for opensearch gem

  1 +## 4.6.3 (unreleased)
  2 +
  3 +- Added experimental support for `opensearch` gem
  4 +
1 ## 4.6.2 (2021-11-15) 5 ## 4.6.2 (2021-11-15)
2 6
3 - Added support for beginless ranges to `where` option 7 - Added support for beginless ranges to `where` option
gemfiles/opensearch.gemfile
@@ -13,5 +13,5 @@ gem "typhoeus" @@ -13,5 +13,5 @@ gem "typhoeus"
13 gem "redis" 13 gem "redis"
14 gem "connection_pool" 14 gem "connection_pool"
15 gem "kaminari" 15 gem "kaminari"
16 -gem "elasticsearch-xpack", ">= 7.8", "< 7.14" 16 +gem "opensearch", github: "opensearch-project/opensearch-ruby"
17 gem "parallel_tests" 17 gem "parallel_tests"
lib/searchkick.rb
@@ -34,6 +34,7 @@ module Searchkick @@ -34,6 +34,7 @@ module Searchkick
34 class Error < StandardError; end 34 class Error < StandardError; end
35 class MissingIndexError < Error; end 35 class MissingIndexError < Error; end
36 class UnsupportedVersionError < Error; end 36 class UnsupportedVersionError < Error; end
  37 + # TODO switch to Error
37 class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end 38 class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end
38 class DangerousOperation < Error; end 39 class DangerousOperation < Error; end
39 class ImportError < Error; end 40 class ImportError < Error; end
@@ -278,6 +279,18 @@ module Searchkick @@ -278,6 +279,18 @@ module Searchkick
278 !Mongoid::Threaded.current_scope(klass).nil? 279 !Mongoid::Threaded.current_scope(klass).nil?
279 end 280 end
280 end 281 end
  282 +
  283 + # private
  284 + def self.not_found_error?(e)
  285 + (defined?(Elasticsearch) && e.is_a?(Elasticsearch::Transport::Transport::Errors::NotFound)) ||
  286 + (defined?(OpenSearch) && e.is_a?(OpenSearch::Transport::Transport::Errors::NotFound))
  287 + end
  288 +
  289 + # private
  290 + def self.transport_error?(e)
  291 + (defined?(Elasticsearch) && e.is_a?(Elasticsearch::Transport::Transport::Error)) ||
  292 + (defined?(OpenSearch) && e.is_a?(OpenSearch::Transport::Transport::Error))
  293 + end
281 end 294 end
282 295
283 require "active_model/callbacks" 296 require "active_model/callbacks"
lib/searchkick/index.rb
@@ -84,7 +84,8 @@ module Searchkick @@ -84,7 +84,8 @@ module Searchkick
84 old_indices = 84 old_indices =
85 begin 85 begin
86 client.indices.get_alias(name: name).keys 86 client.indices.get_alias(name: name).keys
87 - rescue Elasticsearch::Transport::Transport::Errors::NotFound 87 + rescue => e
  88 + raise e unless Searchkick.not_found_error?(e)
88 {} 89 {}
89 end 90 end
90 actions = old_indices.map { |old_name| {remove: {index: old_name, alias: name}} } + [{add: {index: new_name, alias: name}}] 91 actions = old_indices.map { |old_name| {remove: {index: old_name, alias: name}} } + [{add: {index: new_name, alias: name}}]
@@ -109,7 +110,8 @@ module Searchkick @@ -109,7 +110,8 @@ module Searchkick
109 else 110 else
110 client.indices.get_aliases 111 client.indices.get_aliases
111 end 112 end
112 - rescue Elasticsearch::Transport::Transport::Errors::NotFound 113 + rescue => e
  114 + raise e unless Searchkick.not_found_error?(e)
113 {} 115 {}
114 end 116 end
115 indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased 117 indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased
lib/searchkick/query.rb
@@ -113,7 +113,7 @@ module Searchkick @@ -113,7 +113,7 @@ module Searchkick
113 113
114 # no easy way to tell which host the client will use 114 # no easy way to tell which host the client will use
115 host = 115 host =
116 - if Gem::Version.new(Elasticsearch::VERSION) >= Gem::Version.new("7.14.0") 116 + if Searchkick.client.transport.respond_to?(:transport)
117 Searchkick.client.transport.transport.hosts.first 117 Searchkick.client.transport.transport.hosts.first
118 else 118 else
119 Searchkick.client.transport.hosts.first 119 Searchkick.client.transport.hosts.first
lib/searchkick/record_indexer.rb
@@ -64,8 +64,9 @@ module Searchkick @@ -64,8 +64,9 @@ module Searchkick
64 if record.destroyed? || !record.persisted? || !record.should_index? 64 if record.destroyed? || !record.persisted? || !record.should_index?
65 begin 65 begin
66 index.remove(record) 66 index.remove(record)
67 - rescue Elasticsearch::Transport::Transport::Errors::NotFound  
68 - # do nothing 67 + rescue => e
  68 + raise e unless Searchkick.not_found_error?(e)
  69 + # do nothing if not found
69 end 70 end
70 else 71 else
71 if method_name 72 if method_name
lib/searchkick/results.rb
@@ -195,8 +195,8 @@ module Searchkick @@ -195,8 +195,8 @@ module Searchkick
195 begin 195 begin
196 # TODO Active Support notifications for this scroll call 196 # TODO Active Support notifications for this scroll call
197 Searchkick::Results.new(@klass, Searchkick.client.scroll(scroll: options[:scroll], body: {scroll_id: scroll_id}), @options) 197 Searchkick::Results.new(@klass, Searchkick.client.scroll(scroll: options[:scroll], body: {scroll_id: scroll_id}), @options)
198 - rescue Elasticsearch::Transport::Transport::Errors::NotFound => e  
199 - if e.class.to_s =~ /NotFound/ && e.message =~ /search_context_missing_exception/i 198 + rescue => e
  199 + if Searchkick.not_found_error?(e) && e.message =~ /search_context_missing_exception/i
200 raise Searchkick::Error, "Scroll id has expired" 200 raise Searchkick::Error, "Scroll id has expired"
201 else 201 else
202 raise e 202 raise e
@@ -211,8 +211,8 @@ module Searchkick @@ -211,8 +211,8 @@ module Searchkick
211 # not required as scroll will expire 211 # not required as scroll will expire
212 # but there is a cost to open scrolls 212 # but there is a cost to open scrolls
213 Searchkick.client.clear_scroll(scroll_id: scroll_id) 213 Searchkick.client.clear_scroll(scroll_id: scroll_id)
214 - rescue Elasticsearch::Transport::Transport::Error  
215 - # do nothing 214 + rescue => e
  215 + raise e unless Searchkick.transport_error?(e)
216 end 216 end
217 end 217 end
218 218
test/test_helper.rb
@@ -12,7 +12,11 @@ ENV[&quot;ES_PATH&quot;] ||= &quot;#{ENV[&quot;HOME&quot;]}/elasticsearch/#{ENV[&quot;ELASTICSEARCH_VERSION&quot;]} @@ -12,7 +12,11 @@ ENV[&quot;ES_PATH&quot;] ||= &quot;#{ENV[&quot;HOME&quot;]}/elasticsearch/#{ENV[&quot;ELASTICSEARCH_VERSION&quot;]}
12 12
13 $logger = ActiveSupport::Logger.new(ENV["VERBOSE"] ? STDOUT : nil) 13 $logger = ActiveSupport::Logger.new(ENV["VERBOSE"] ? STDOUT : nil)
14 14
15 -if Gem::Version.new(Elasticsearch::VERSION) >= Gem::Version.new("7.14.0") 15 +if defined?(OpenSearch)
  16 + Searchkick.client = OpenSearch::Client.new
  17 +end
  18 +
  19 +if defined?(OpenSearch) || Gem::Version.new(Elasticsearch::VERSION) >= Gem::Version.new("7.14.0")
16 Searchkick.client.transport.transport.logger = $logger 20 Searchkick.client.transport.transport.logger = $logger
17 else 21 else
18 Searchkick.client.transport.logger = $logger 22 Searchkick.client.transport.logger = $logger