Skip to content

Commit

Permalink
Correctly report indices and aliases (#757)
Browse files Browse the repository at this point in the history
* Correctly report indices and aliases
* Fix rubocop offense
* Fix specs & Changelog
* Add `include_type_name` parameter for ES 6.7-6.8 for `client.indices.get` request

Co-authored-by: mpeychich <mpeychich@mac.com>
Co-authored-by: Ivan Rabotyaga <ivan.rabotyaga@toptal.com>
  • Loading branch information
3 people authored Feb 15, 2021
1 parent d5ffa79 commit 47ad295
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 40 deletions.
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

0 comments on commit 47ad295

Please sign in to comment.