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

169395632 v7 2 changes #69

Merged
merged 20 commits into from
Nov 12, 2019
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
82 changes: 82 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
version: 2

workflows:
version: 2
main:
jobs:
- build_2_4
- build_2_5
- build_2_6
jobs:
build_2_4:
resource_class: small
docker:
- image: ruby:2.4
- image: elastic/elasticsearch:6.8.2
environment:
- xpack.security.enabled=false
steps:
- restore_cache:
keys:
- gem-cache-2_4-v2-{{ checksum "Gemfile.lock" }}
- gem-cache-2_4-v2-
- checkout
- run:
name: Install Ruby Dependencies
command: bundle install
- run:
name: Run Tests
command: bundle exec rspec
- save_cache:
key: gem-cache-2_4-v2-{{ checksum "Gemfile.lock" }}
paths:
- ./vendor/bundle
- ./vendor/cache
build_2_5:
resource_class: small
docker:
- image: ruby:2.5
- image: elastic/elasticsearch:6.8.2
environment:
- xpack.security.enabled=false
steps:
- restore_cache:
keys:
- gem-cache-2_5-v2-{{ checksum "Gemfile.lock" }}
- gem-cache-2_5-v2-
- checkout
- run:
name: Install Ruby Dependencies
command: bundle install
- run:
name: Run Tests
command: bundle exec rspec
- save_cache:
key: gem-cache-2_5-v2-{{ checksum "Gemfile.lock" }}
paths:
- ./vendor/bundle
- ./vendor/cache
build_2_6:
resource_class: small
docker:
- image: ruby:2.5
- image: elastic/elasticsearch:6.8.2
environment:
- xpack.security.enabled=false
steps:
- restore_cache:
keys:
- gem-cache-2_6-v2-{{ checksum "Gemfile.lock" }}
- gem-cache-2_6-v2-
- checkout
- run:
name: Install Ruby Dependencies
command: bundle install
- run:
name: Run Tests
command: bundle exec rspec
- save_cache:
key: gem-cache-2_6-v2-{{ checksum "Gemfile.lock" }}
paths:
- ./vendor/bundle
- ./vendor/cache
17 changes: 0 additions & 17 deletions .travis.yml

This file was deleted.

7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v0.13.0
- changes needed for use with Elasticsearch v7
- handle the v7 "total" as an object rather than a scalar
- use timestamp with no colons
- update elasticsearch gem version for consistency with target ES version
- expose refresh_index to force write (because in v7, flush no longer forces writes)
- allow for optional 'include_type_name_on_create' arg so that the :include_type_name can be passed
v0.12.1
- use Arel.sql to avoid unsafe sql and eliminate deprecation warnings when used in Rails projects
v0.12.0
Expand Down
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ source 'https://rubygems.org'
# Specify your gem's dependencies in elasticity.gemspec
gemspec

gem "elasticsearch", "5.0.4"
gem "elasticsearch", "7.2.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Or install it yourself as:
### Version Support
This gem has [elasticsearch-ruby](https://github.com/elastic/elasticsearch-ruby) as a dependency. In order to use different versions of elasticsearch you will need to match your version of elasticsearch-ruby to the version of elasticsearch you want to use ([see here](https://github.com/elastic/elasticsearch-ruby#compatibility). Elasticity should work across all versions of elastisearch-ruby, although they have not all been tested so there are likely edge cases.

Currently tests are run on travis ci against elasticsearch 5.1.1 with elasticsearch-ruby 5.0.3.
Currently tests are run on CirlceCI against elasticsearch 6.8.2 with elasticsearch-ruby 7.2.0.

### Configuration

Expand Down
1 change: 1 addition & 0 deletions elasticity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "byebug"
spec.add_development_dependency "codeclimate-test-reporter"
spec.add_development_dependency "redis"
spec.add_development_dependency "timecop"

spec.add_dependency "activesupport", ">= 4.0.0", "< 6"
spec.add_dependency "activemodel", ">= 4.0.0", "< 6"
Expand Down
Binary file removed es-elasticity-0.12.0.pre.rc1.gem
Binary file not shown.
5 changes: 3 additions & 2 deletions lib/elasticity/index_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class SubclassError < StandardError; end
VERSION_FOR_SUBCLASS_ERROR = "7.0.0".freeze
ATTRS = [
:index_base_name, :document_type, :mapping, :strategy, :subclasses,
:settings
:settings, :use_new_timestamp_format, :include_type_name_on_create
].freeze
VALIDATABLE_ATTRS = [:index_base_name, :document_type, :strategy].freeze

Expand Down Expand Up @@ -39,7 +39,8 @@ def client
def definition
return @definition if defined?(@definition)
@definition = {
settings: merge_settings, mappings: { @document_type => @mapping.nil? ? {} : @mapping.deep_stringify_keys }
settings: merge_settings,
mappings: { @document_type => @mapping.nil? ? {} : @mapping.deep_stringify_keys }
}
subclasses.each do |doc_type, subclass|
@definition[:mappings][doc_type] = subclass.constantize.mapping
Expand Down
9 changes: 8 additions & 1 deletion lib/elasticity/index_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ def self.set_delegates(obj, to)
:index_exists?,
:remap!,
:flush_index,
:refresh_index,
:index_document,
:search,
:get,
Expand All @@ -27,7 +28,7 @@ def self.set_delegates(obj, to)
def initialize(document_klass, index_config)
@document_klass = document_klass
@index_config = index_config
@strategy = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type)
@strategy = @index_config.strategy.new(@index_config.client, @index_config.fq_index_base_name, @index_config.document_type, @index_config.use_new_timestamp_format, @index_config.include_type_name_on_create)
end

delegate(
Expand Down Expand Up @@ -71,10 +72,16 @@ def remap!(retry_delete_on_recoverable_errors: true, retry_delay: 30, max_delay:
end

# Flushes the index, forcing any writes
# note that v7 no longer forces any writes on flush
def flush_index
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would emitting a deprecation log warning be useful here, or is flush_index still have value if it doesn't force the writes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flush is still a valid thing
https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-flush.html#flush-api-desc

it just want have the side-effect of refreshing the index which made new documents visible to searches (the side-effect that is has pre v7)

@strategy.flush
end

# Resfreshes the index, forcing any writes
def refresh_index
@strategy.refresh
end

# Index the given document
def index_document(id, document_hash)
@strategy.index_document(document_type, id, document_hash)
Expand Down
2 changes: 1 addition & 1 deletion lib/elasticity/instrumented_client.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Elasticity
class InstrumentedClient
INDICES_METHODS = %w(exists create delete get_settings get_mapping flush get_alias get_aliases put_alias delete_alias exists_alias update_aliases)
INDICES_METHODS = %w(exists create delete get_settings get_mapping flush refresh get_alias get_aliases put_alias delete_alias exists_alias update_aliases)
INDEX_METHODS = %w(index delete get mget search count msearch scroll delete_by_query bulk)

def initialize(client)
Expand Down
23 changes: 19 additions & 4 deletions lib/elasticity/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def active_records(relation)
class LazySearch
include Enumerable

delegate :each, :size, :length, :[], :+, :-, :&, :|, :total, :per_page,
delegate :each, :size, :length, :[], :+, :-, :&, :|, :total, :per_page, :to_ary,
:total_pages, :current_page, :next_page, :previous_page, :aggregations, to: :search_results

attr_accessor :search_definition
Expand Down Expand Up @@ -147,7 +147,12 @@ def blank?
end

def total
search["hits"]["total"]
res = search["hits"]["total"]
if res.is_a?(::Hash)
res["value"]
else
res
end
end

def each_batch
Expand Down Expand Up @@ -237,7 +242,12 @@ def metadata
end

def total
metadata[:total]
res = metadata[:total]
if res.is_a?(::Hash)
res["value"]
else
res
end
end

def suggestions
Expand Down Expand Up @@ -311,7 +321,12 @@ def aggregations
end

def total
@response["hits"]["total"]
res = @response["hits"]["total"]
if res.is_a?(::Hash)
res["value"]
else
res
end
end
alias_method :total_entries, :total

Expand Down
34 changes: 26 additions & 8 deletions lib/elasticity/strategies/alias_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ class AliasIndex

STATUSES = [:missing, :ok]

def initialize(client, index_base_name, document_type)
def initialize(client, index_base_name, document_type, use_new_timestamp_format = false, include_type_name_on_create = true)
@client = client
@main_alias = index_base_name
@update_alias = "#{index_base_name}_update"
@document_type = document_type

# included for compatibility with v7
@use_new_timestamp_format = use_new_timestamp_format
@include_type_name_on_create = include_type_name_on_create
end

def ref_index_name
Expand Down Expand Up @@ -186,11 +190,12 @@ def update_indexes

def create(index_def)
if missing?
index_name = create_index(index_def)
name = create_index(index_def)
@created_index_name = name
@client.index_update_aliases(body: {
actions: [
{ add: { index: index_name, alias: @main_alias } },
{ add: { index: index_name, alias: @update_alias } },
{ add: { index: name, alias: @main_alias } },
{ add: { index: name, alias: @update_alias } },
]
})
else
Expand Down Expand Up @@ -257,6 +262,10 @@ def flush
@client.index_flush(index: @update_alias)
end

def refresh
@client.index_refresh(index: @update_alias)
end

def settings
@client.index_get_settings(index: @main_alias, type: @document_type).values.first
rescue Elasticsearch::Transport::Transport::Errors::NotFound
Expand All @@ -279,11 +288,20 @@ def mapping

private

def build_index_name
ts = String.new
if @use_new_timestamp_format == true
ts = Time.now.utc.strftime("%Y%m%d%H%M%S%6N")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we could add some delimiters to make this easier to parse by humans reading the index names? Something like 2019_10_27_211410 (not sure if _ is an acceptable delimiter, but whatever is could be useful here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, we could. but this format should be familiar to rails devs
https://github.com/doximity/doximity/tree/master/db/migrate (which is why I picked it)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and I was a bit scared of some future upgrade declaring other chars off limits

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah - I was thinking that same thing about the chars too. I'm good either way, just figured I'd mention it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
fwiw, most human interaction is usually via the aliases anyway

else
ts = Time.now.utc.strftime("%Y-%m-%d_%H:%M:%S.%6N")
end
"#{@main_alias}-#{ts}"
end

def create_index(index_def)
ts = Time.now.utc.strftime("%Y-%m-%d_%H:%M:%S.%6N")
index_name = "#{@main_alias}-#{ts}"
@client.index_create(index: index_name, body: index_def)
index_name
name = build_index_name
@client.index_create(index: name, body: index_def, include_type_name: @include_type_name_on_create)
name
end

def retryable_error?(e)
Expand Down
14 changes: 12 additions & 2 deletions lib/elasticity/strategies/single_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ module Strategies
class SingleIndex
STATUSES = [:missing, :ok]

def initialize(client, index_name, document_type)
def initialize(client, index_name, document_type, use_new_timestamp_format = false, include_type_name_on_create = true)
@client = client
@index_name = index_name
@document_type = document_type

# included for compatibility with v7
@include_type_name_on_create = include_type_name_on_create

# not currently used. included for argument compatiblity with AliasStrategy
@use_new_timestamp_format = use_new_timestamp_format
end

def ref_index_name
Expand All @@ -23,7 +29,7 @@ def missing?

def create(index_def)
if missing?
@client.index_create(index: @index_name, body: index_def)
@client.index_create(index: @index_name, body: index_def, include_type_name: @include_type_name_on_create)
else
raise IndexError.new(@index_name, "index already exist")
end
Expand Down Expand Up @@ -101,6 +107,10 @@ def mapping
def flush
@client.index_flush(index: @index_name)
end

def refresh
@client.index_refresh(index: @index_name)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/elasticity/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Elasticity
VERSION = "0.12.1"
VERSION = "0.13.0"
end
Loading