Skip to content

Commit a0f14d9

Browse files
authored
7.0 support (#875)
* [MODEL] Updates for 7.0 * [STORE] Updates for 7.0 * [MODEL] Test against Rails 6.0.beta3 * Delete all gemfile.lock files in bundle clean task * [STORE] Account for no types being accepted for mappings in 7.0 * Update travis config * Update script to download and start elasticsearch * Use wait_for_green script before running tests * Handle downloading and installing ES 6.7.1 nad 7.0.0 on Travis * Update travis script * Leave testing against ES 6.x to the 6.x branch * [MODEL] Add test for handling different total hits format in search response * Correct merge conflicts * Update dependency versions for 7.0 release of client
1 parent cff4848 commit a0f14d9

File tree

24 files changed

+620
-332
lines changed

24 files changed

+620
-332
lines changed

.travis.yml

+4-6
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ services:
1414
branches:
1515
only:
1616
- master
17-
- travis
18-
- 5.x
1917
- 6.x
20-
- 2.x
2118

2219
matrix:
2320
include:
@@ -29,7 +26,7 @@ matrix:
2926
jdk: oraclejdk8
3027
env: RAILS_VERSIONS=5.0
3128

32-
- rvm: 2.6.1
29+
- rvm: 2.6.2
3330
jdk: oraclejdk8
3431
env: RAILS_VERSIONS=4.0,5.0,6.0
3532

@@ -39,12 +36,13 @@ matrix:
3936

4037
env:
4138
global:
42-
- ELASTICSEARCH_VERSION=7.0.0-beta1
39+
- ELASTICSEARCH_VERSION=7.0.0
40+
- TEST_ES_SERVER=http://localhost:9250
4341
- TEST_CLUSTER_PORT=9250
4442
- QUIET=true
4543

4644
before_install:
47-
- TEST_CLUSTER_PORT=9250 source ./travis_before_script.sh
45+
- source ./travis_before_script.sh
4846
- gem update --system
4947
- gem update bundler
5048
- gem --version

Rakefile

+55-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,33 @@ subprojects << 'elasticsearch-model' unless defined?(JRUBY_VERSION)
2222

2323
__current__ = Pathname( File.expand_path('..', __FILE__) )
2424

25+
def admin_client
26+
$admin_client ||= begin
27+
transport_options = {}
28+
test_suite = ENV['TEST_SUITE'].freeze
29+
30+
if hosts = ENV['TEST_ES_SERVER'] || ENV['ELASTICSEARCH_HOSTS']
31+
split_hosts = hosts.split(',').map do |host|
32+
/(http\:\/\/)?(\S+)/.match(host)[2]
33+
end
34+
35+
host, port = split_hosts.first.split(':')
36+
end
37+
38+
if test_suite == 'security'
39+
transport_options.merge!(:ssl => { verify: false,
40+
ca_path: CERT_DIR })
41+
42+
password = ENV['ELASTIC_PASSWORD']
43+
user = ENV['ELASTIC_USER'] || 'elastic'
44+
url = "https://#{user}:#{password}@#{host}:#{port}"
45+
else
46+
url = "http://#{host || 'localhost'}:#{port || 9200}"
47+
end
48+
Elasticsearch::Client.new(host: url, transport_options: transport_options)
49+
end
50+
end
51+
2552
task :default do
2653
system "rake --tasks"
2754
end
@@ -53,9 +80,7 @@ namespace :bundle do
5380
subprojects.each do |project|
5481
sh "rm -f #{__current__.join(project)}/Gemfile.lock"
5582
end
56-
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/3.0.gemfile.lock"
57-
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/4.0.gemfile.lock"
58-
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/5.0.gemfile.lock"
83+
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/*.lock"
5984
end
6085
end
6186

@@ -132,7 +157,7 @@ namespace :test do
132157
end
133158

134159
desc "Run all tests in all subprojects"
135-
task :all do
160+
task :all => :wait_for_green do
136161
subprojects.each do |project|
137162
puts '-'*80
138163
sh "cd #{project} && " +
@@ -163,6 +188,32 @@ namespace :test do
163188
end
164189
end
165190

191+
192+
desc "Wait for elasticsearch cluster to be in green state"
193+
task :wait_for_green do
194+
require 'elasticsearch'
195+
196+
ready = nil
197+
5.times do |i|
198+
begin
199+
puts "Attempting to wait for green status: #{i + 1}"
200+
if admin_client.cluster.health(wait_for_status: 'green', timeout: '50s')
201+
ready = true
202+
break
203+
end
204+
rescue Elasticsearch::Transport::Transport::Errors::RequestTimeout => ex
205+
puts "Couldn't confirm green status.\n#{ex.inspect}."
206+
rescue Faraday::ConnectionFailed => ex
207+
puts "Couldn't connect to Elasticsearch.\n#{ex.inspect}."
208+
sleep(30)
209+
end
210+
end
211+
unless ready
212+
puts "Couldn't connect to Elasticsearch, aborting program."
213+
exit(1)
214+
end
215+
end
216+
166217
desc "Generate documentation for all subprojects"
167218
task :doc do
168219
subprojects.each do |project|

elasticsearch-model/.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ test/tmp
1616
test/version_tmp
1717
tmp
1818

19-
gemfiles/*.gemfile.lock
19+
20+
gemfiles/*.lock
21+

elasticsearch-model/gemfiles/6.0.gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
# $ BUNDLE_GEMFILE=./gemfiles/6.0.gemfile bundle install
2121
# $ BUNDLE_GEMFILE=./gemfiles/6.0.gemfile bundle exec rake test:integration
2222

23+
2324
source 'https://rubygems.org'
2425

2526
gemspec path: '../'

elasticsearch-model/lib/elasticsearch/model/adapters/multiple.rb

+12-4
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def __records_for_klass(klass, ids)
7575
klass.where(klass.primary_key => ids)
7676
when Elasticsearch::Model::Adapter::Mongoid.equal?(adapter)
7777
klass.where(:id.in => ids)
78-
else
79-
klass.find(ids)
78+
else
79+
klass.find(ids)
8080
end
8181
end
8282

@@ -108,13 +108,21 @@ def __ids_by_type
108108
def __type_for_hit(hit)
109109
@@__types ||= {}
110110

111-
@@__types[ "#{hit[:_index]}::#{hit[:_type]}" ] ||= begin
111+
key = "#{hit[:_index]}::#{hit[:_type]}" if hit[:_type] && hit[:_type] != '_doc'
112+
key = hit[:_index] unless key
113+
114+
@@__types[key] ||= begin
112115
Registry.all.detect do |model|
113-
model.index_name == hit[:_index] && model.document_type == hit[:_type]
116+
(model.index_name == hit[:_index] && __no_type?(hit)) ||
117+
(model.index_name == hit[:_index] && model.document_type == hit[:_type])
114118
end
115119
end
116120
end
117121

122+
def __no_type?(hit)
123+
hit[:_type].nil? || hit[:_type] == '_doc'
124+
end
125+
118126
# Returns the adapter registered for a particular `klass` or `nil` if not available
119127
#
120128
# @api private

elasticsearch-model/lib/elasticsearch/model/indexing.rb

+12-8
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ class Mappings
5656
# @private
5757
TYPES_WITH_EMBEDDED_PROPERTIES = %w(object nested)
5858

59-
def initialize(type, options={})
60-
raise ArgumentError, "`type` is missing" if type.nil?
61-
59+
def initialize(type = nil, options={})
6260
@type = type
6361
@options = options
6462
@mapping = {}
@@ -89,7 +87,11 @@ def indexes(name, options={}, &block)
8987
end
9088

9189
def to_hash
92-
{ @type.to_sym => @options.merge( properties: @mapping ) }
90+
if @type
91+
{ @type.to_sym => @options.merge( properties: @mapping ) }
92+
else
93+
@options.merge( properties: @mapping )
94+
end
9395
end
9496

9597
def as_json(options={})
@@ -246,10 +248,12 @@ def create_index!(options={})
246248
delete_index!(options.merge index: target_index) if options[:force]
247249

248250
unless index_exists?(index: target_index)
249-
self.client.indices.create index: target_index,
250-
body: {
251-
settings: settings,
252-
mappings: mappings }
251+
options.delete(:force)
252+
self.client.indices.create({ index: target_index,
253+
body: {
254+
settings: settings,
255+
mappings: mappings }
256+
}.merge(options))
253257
end
254258
end
255259

elasticsearch-model/lib/elasticsearch/model/naming.rb

+1-4
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,7 @@ def default_index_name
108108
self.model_name.collection.gsub(/\//, '-')
109109
end
110110

111-
def default_document_type
112-
DEFAULT_DOC_TYPE
113-
end
114-
111+
def default_document_type; end
115112
end
116113

117114
module InstanceMethods

0 commit comments

Comments
 (0)