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

Enable use of elasticsearch 7 #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ BikeIndex

# More score manipulation:
BikeIndex
.coord_similarity(false) # disable coord similarity (no score normalization)
.boost(0.0) { must(brand: ['Trek', 'Cannondale']) } # no score
.boost(fixed: 1.0) { should(year: 2015) } # fixed score
.boost(fixed: 2.0) { should(year: 2016) }
Expand Down Expand Up @@ -150,4 +149,3 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/platan
## License

The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).

2 changes: 1 addition & 1 deletion docker-compose.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'

services:
elasticsearch:
image: elasticsearch:6.7.0
image: elasticsearch:7.2.0
ports:
- 9200
volumes:
Expand Down
2 changes: 1 addition & 1 deletion elastic-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "elasticsearch", "~> 6.8"
spec.add_dependency "elasticsearch", "~> 7.2"
spec.add_dependency "activesupport", [">= 4.0", "< 8.0"]

spec.add_development_dependency "dotenv"
Expand Down
3 changes: 1 addition & 2 deletions lib/elastic/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class Configuration
host: '127.0.0.1',
port: 9200,
page_size: 20,
coord_similarity: true,
import_batch_size: 10_000,
whiny_indices: false,
api_client: nil, # set by method
Expand All @@ -14,7 +13,7 @@ class Configuration
disable_index_name_caching: false
}

attr_accessor :host, :port, :api_client, :index, :page_size, :coord_similarity, :logger,
attr_accessor :host, :port, :api_client, :index, :page_size, :logger,
:import_batch_size, :whiny_indices, :time_zone, :disable_indexing, :disable_index_name_caching

def initialize
Expand Down
19 changes: 8 additions & 11 deletions lib/elastic/core/connector.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
module Elastic::Core
class Connector
DEFAULT_TYPE = '_doc'

def initialize(_name, _mapping, settling_time: 10.seconds)
@name = _name
@mapping = _mapping
Expand All @@ -23,7 +21,7 @@ def status
end

def drop
api.indices.delete index: "#{index_name}:*"
api.indices.delete index: "#{index_name}_*"
nil
end

Expand Down Expand Up @@ -56,7 +54,7 @@ def index(_document)

# TODO: validate document type
operations = write_indices.map do |write_index|
{ 'index' => _document.merge('_index' => write_index, '_type' => DEFAULT_TYPE) }
{ 'index' => _document.merge('_index' => write_index) }
end

api.bulk(body: operations)
Expand All @@ -66,7 +64,7 @@ def bulk_index(_documents)
return if Elastic.config.disable_indexing

# TODO: validate documents type
body = _documents.map { |doc| { 'index' => doc.merge('_type' => DEFAULT_TYPE) } }
body = _documents.map { |doc| { 'index' => doc } }

write_indices.each do |write_index|
retry_on_temporary_error('bulk indexing') do
Expand All @@ -83,12 +81,12 @@ def delete(_document)
write_index, rolling_index = write_indices

operations = [{
'delete' => _document.merge('_index' => write_index, '_type' => DEFAULT_TYPE)
'delete' => _document.merge('_index' => write_index)
}]

if rolling_index
operations << {
'delete' => _document.merge('_index' => rolling_index, '_type' => DEFAULT_TYPE)
'delete' => _document.merge('_index' => rolling_index)
}
end

Expand Down Expand Up @@ -223,7 +221,7 @@ def resolve_actual_index_name
end

def create_index_w_mapping
new_name = "#{index_name}:#{Time.now.to_i}"
new_name = "#{index_name}_#{Time.now.to_i}"
api.indices.create index: new_name
api.cluster.health wait_for_status: 'yellow'
setup_index_mapping new_name
Expand All @@ -243,7 +241,7 @@ def create_from_scratch
end

def mapping_synchronized?(_index)
type_mappings = api.indices.get_mapping(index: _index, include_type_name: false)
type_mappings = api.indices.get_mapping(index: _index)
return false if type_mappings[_index].nil?
return false if type_mappings[_index]['mappings'].empty?

Expand All @@ -256,7 +254,7 @@ def mapping_synchronized?(_index)
end

def setup_index_mapping(_index)
api.indices.put_mapping(index: _index, type: DEFAULT_TYPE, body: @mapping)
api.indices.put_mapping(index: _index, body: @mapping)
end

def transfer_alias(_alias, from: nil, to: nil)
Expand All @@ -274,7 +272,6 @@ def transform_hit_to_create(_hit)
{
'create' => {
'_id' => _hit['_id'],
'_type' => DEFAULT_TYPE,
'data' => _hit['_source']
}
}
Expand Down
4 changes: 0 additions & 4 deletions lib/elastic/dsl/bool_query_builder.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
module Elastic::Dsl
module BoolQueryBuilder
def coord_similarity(_enable)
with_bool_query { |query| query.disable_coord = !_enable }
end

def must(*_queries)
with_bool_query do |query|
node = build_query_from_params(_queries)
Expand Down
2 changes: 1 addition & 1 deletion lib/elastic/nested_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class NestedType < Types::BaseType
class << self
extend Forwardable

def_delegators :query, :must, :should, :segment, :coord_similarity
def_delegators :query, :must, :should, :segment
end

def self.query
Expand Down
5 changes: 1 addition & 4 deletions lib/elastic/nodes/boolean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ def self.build_or(_nodes)
new.tap { |n| n.shoulds = _nodes }
end

attr_accessor :minimum_should_match, :disable_coord
attr_accessor :minimum_should_match

def initialize
super
@musts = []
@must_nots = []
@shoulds = []
@filters = []
@disable_coord = !Elastic.config.coord_similarity
end

def must(_node)
Expand Down Expand Up @@ -82,7 +81,6 @@ def render(_options = {})
hash['should'] = @shoulds.map { |n| n.render(_options) } if [email protected]?
hash['filters'] = @filters.map { |n| n.render(_options) } if [email protected]?
hash['minimum_should_match'] = minimum_should_match unless minimum_should_match.nil?
hash['disable_coord'] = true if disable_coord
render_boost(hash)

{ "bool" => hash }
Expand Down Expand Up @@ -123,7 +121,6 @@ def prepare_clone(_clone, _musts, _must_nots, _shoulds, _filters)
_clone.shoulds = _shoulds
_clone.filters = _filters
_clone.minimum_should_match = @minimum_should_match
_clone.disable_coord = @disable_coord
_clone
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/elastic/shims/total_picking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def handle_result(_raw, _formatter)

case result
when Elastic::Results::Root
result.total
result.total['value']
when Elastic::Results::GroupedResult
result.map_to_group { |bucket| Elastic::Results::Metric.new(bucket.total) }
else
Expand Down
4 changes: 2 additions & 2 deletions lib/elastic/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ class Type < Types::BaseType
class << self
extend Forwardable

def_delegators :query, :must, :should, :segment, :stats, :maximum, :minimum, :sum, :average,
:coord_similarity, :limit, :offset, :pluck, :ids, :total
def_delegators :query, :must, :should, :segment, :stats, :maximum, :minimum, :sum,
:average, :limit, :offset, :pluck, :ids, :total
end

def self.default_suffix
Expand Down
8 changes: 3 additions & 5 deletions spec/lib/core/connector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
describe "drop" do
it "removes index" do
expect { connector.drop }
.to change { api.indices.exists? index: "#{index_name}:12345" }.to false
.to change { api.indices.exists? index: "#{index_name}_12345" }.to false
end
end

Expand Down Expand Up @@ -248,16 +248,14 @@
# Some helpers

def prepare_index(mapping: nil, records: nil)
actual_index = "#{index_name}:12345"
actual_index = "#{index_name}_12345"

api.indices.create index: actual_index
api.cluster.health wait_for_status: 'yellow'
api.indices.put_alias index: actual_index, name: index_name
api.indices.put_alias index: actual_index, name: "#{index_name}.w"

if mapping
api.indices.put_mapping index: actual_index, type: '_doc', body: mapping
end
api.indices.put_mapping index: actual_index, body: mapping if mapping

if records
records.each do |r|
Expand Down
7 changes: 0 additions & 7 deletions spec/lib/nodes/boolean_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@ def build_boolean(must: [], must_not: [], should: [], filter: [], boost: nil)
let(:single_filter) { build_boolean(filter: [child_a]) }
let(:single_must_not) { build_boolean(must_not: [child_a]) }

describe "disable_coord" do
it "is set to true by default if coord_similarity is disabled in configuration" do
expect { Elastic.configure coord_similarity: false }
.to change { build_boolean.disable_coord }.to true
end
end

describe "render" do
it "renders correctly" do
expect(node.render)
Expand Down
1 change: 0 additions & 1 deletion spec/lib/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@
describe "each_with_score" do
it "iterates over matching documents and its scores" do
results = query
.coord_similarity(false)
.boost(2.0, fixed: true) { should('tags.name' => 'baz_tag') }
.boost(3.0, fixed: true) { should('tags.name' => 'qux_tag') }

Expand Down