Commit 079d6c8f22d83c0afb9b31c586950133c5e2702d
1 parent
70e5fd1d
Exists in
master
and in
2 other branches
Added experimental support for opensearch gem
Showing
8 changed files
with
35 additions
and
11 deletions
Show diff stats
CHANGELOG.md
gemfiles/opensearch.gemfile
lib/searchkick.rb
... | ... | @@ -34,6 +34,7 @@ module Searchkick |
34 | 34 | class Error < StandardError; end |
35 | 35 | class MissingIndexError < Error; end |
36 | 36 | class UnsupportedVersionError < Error; end |
37 | + # TODO switch to Error | |
37 | 38 | class InvalidQueryError < Elasticsearch::Transport::Transport::Errors::BadRequest; end |
38 | 39 | class DangerousOperation < Error; end |
39 | 40 | class ImportError < Error; end |
... | ... | @@ -278,6 +279,18 @@ module Searchkick |
278 | 279 | !Mongoid::Threaded.current_scope(klass).nil? |
279 | 280 | end |
280 | 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 | 294 | end |
282 | 295 | |
283 | 296 | require "active_model/callbacks" | ... | ... |
lib/searchkick/index.rb
... | ... | @@ -84,7 +84,8 @@ module Searchkick |
84 | 84 | old_indices = |
85 | 85 | begin |
86 | 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 | 90 | end |
90 | 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 | 110 | else |
110 | 111 | client.indices.get_aliases |
111 | 112 | end |
112 | - rescue Elasticsearch::Transport::Transport::Errors::NotFound | |
113 | + rescue => e | |
114 | + raise e unless Searchkick.not_found_error?(e) | |
113 | 115 | {} |
114 | 116 | end |
115 | 117 | indices = indices.select { |_k, v| v.empty? || v["aliases"].empty? } if unaliased | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -113,7 +113,7 @@ module Searchkick |
113 | 113 | |
114 | 114 | # no easy way to tell which host the client will use |
115 | 115 | host = |
116 | - if Gem::Version.new(Elasticsearch::VERSION) >= Gem::Version.new("7.14.0") | |
116 | + if Searchkick.client.transport.respond_to?(:transport) | |
117 | 117 | Searchkick.client.transport.transport.hosts.first |
118 | 118 | else |
119 | 119 | Searchkick.client.transport.hosts.first | ... | ... |
lib/searchkick/record_indexer.rb
... | ... | @@ -64,8 +64,9 @@ module Searchkick |
64 | 64 | if record.destroyed? || !record.persisted? || !record.should_index? |
65 | 65 | begin |
66 | 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 | 70 | end |
70 | 71 | else |
71 | 72 | if method_name | ... | ... |
lib/searchkick/results.rb
... | ... | @@ -195,8 +195,8 @@ module Searchkick |
195 | 195 | begin |
196 | 196 | # TODO Active Support notifications for this scroll call |
197 | 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 | 200 | raise Searchkick::Error, "Scroll id has expired" |
201 | 201 | else |
202 | 202 | raise e |
... | ... | @@ -211,8 +211,8 @@ module Searchkick |
211 | 211 | # not required as scroll will expire |
212 | 212 | # but there is a cost to open scrolls |
213 | 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 | 216 | end |
217 | 217 | end |
218 | 218 | ... | ... |
test/test_helper.rb
... | ... | @@ -12,7 +12,11 @@ ENV["ES_PATH"] ||= "#{ENV["HOME"]}/elasticsearch/#{ENV["ELASTICSEARCH_VERSION"]} |
12 | 12 | |
13 | 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 | 20 | Searchkick.client.transport.transport.logger = $logger |
17 | 21 | else |
18 | 22 | Searchkick.client.transport.logger = $logger | ... | ... |