Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly report indices and aliases #757

Merged
merged 4 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

### Changes

* [#757](https://github.com/toptal/chewy/pull/757): **(Breaking)** Fix `Chewy::Index.index` & `Chewy::Index.aliases` to correctly report indexes and aliases ([@mpeychich][], [@dalthon][])
* [#761](https://github.com/toptal/chewy/pull/761): Avoid fetching scope data to check if it is blank ([@dalthon][])

### Bugs Fixed
Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/index/actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def #{method}(*args)
def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
result = if suffix.present?
start_time = Time.now
indexes = self.indexes
indexes = self.indexes - [index_name]
create! suffix, alias: false

general_name = index_name
Expand Down
21 changes: 16 additions & 5 deletions lib/chewy/index/aliases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@ module Aliases

module ClassMethods
def indexes
client.indices.get_alias(name: index_name).keys
rescue Elasticsearch::Transport::Transport::Errors::NotFound
[]
get_args = {index: index_name}
get_args[:include_type_name] = true if Runtime.version >= '6.7.0'
indexes = empty_if_not_found { client.indices.get(**get_args).keys }
indexes += empty_if_not_found { client.indices.get_alias(name: index_name).keys }
indexes.compact.uniq
end

def aliases
name = index_name
client.indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || []
empty_if_not_found do
client.indices.get_alias(index: index_name, name: '*').values.flat_map do |aliases|
aliases['aliases'].keys
end
end.compact.uniq
end

private

def empty_if_not_found
yield
rescue Elasticsearch::Transport::Transport::Errors::NotFound
[]
end
Expand Down
Loading