Skip to content

Commit e873a2e

Browse files
committed
[MODEL] Update child-parent integration test to use single index type for ES 6.3 (elastic#805)
* [MODEL] Update child-parent integration test to use single index type for ES 6.3 * [CI] Expand unit test matrix and only test integration on latest stable Rails, latest stable Ruby * [CI] Don't test JRuby in Travis until newer version of elasticsearch-extensions gem is released without oj/patron dependencies
1 parent c5f5515 commit e873a2e

File tree

6 files changed

+93
-44
lines changed

6 files changed

+93
-44
lines changed

.travis.yml

+12-4
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,27 @@ branches:
2121

2222
matrix:
2323
include:
24-
- rvm: 2.2.10
24+
- rvm: 2.2
2525
jdk: oraclejdk8
2626
env: TEST_SUITE=unit
2727

28-
- rvm: 2.3.7
28+
- rvm: 2.3
2929
jdk: oraclejdk8
3030
env: TEST_SUITE=unit
3131

32-
- rvm: 2.5.1
32+
- rvm: 2.4
3333
jdk: oraclejdk8
3434
env: TEST_SUITE=unit
3535

36-
- rvm: 2.5.1
36+
- rvm: 2.5
37+
jdk: oraclejdk8
38+
env: TEST_SUITE=unit
39+
40+
# - rvm: jruby-9.1
41+
# jdk: oraclejdk8
42+
# env: TEST_SUITE=unit
43+
44+
- rvm: 2.5
3745
jdk: oraclejdk8
3846
env: TEST_SUITE=integration QUIET=y
3947

elasticsearch-model/Rakefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ namespace :test do
3838
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
3939
end
4040

41-
desc "Run integration tests against ActiveModel 3, 4 and 5"
41+
desc "Run integration tests against latest stable ActiveModel (5)"
4242
task :integration do
43-
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
44-
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
43+
#sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
44+
#sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
4545
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
4646
end
4747

elasticsearch-model/elasticsearch-model.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Gem::Specification.new do |s|
3535
s.add_development_dependency "sqlite3"
3636
s.add_development_dependency "activemodel", "> 3"
3737

38-
s.add_development_dependency "oj"
38+
s.add_development_dependency "oj" unless defined?(JRUBY_VERSION)
3939
s.add_development_dependency "kaminari"
4040
s.add_development_dependency "will_paginate"
4141

elasticsearch-model/test/integration/active_record_associations_parent_child_test.rb

+75-34
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,24 @@ class Question < ActiveRecord::Base
1111

1212
has_many :answers, dependent: :destroy
1313

14-
index_name 'questions_and_answers'
14+
JOIN_TYPE = 'question'.freeze
15+
JOIN_METADATA = { join_field: JOIN_TYPE}.freeze
16+
17+
index_name 'questions_and_answers'.freeze
18+
document_type 'doc'.freeze
1519

1620
mapping do
1721
indexes :title
1822
indexes :text
1923
indexes :author
2024
end
2125

26+
def as_indexed_json(options={})
27+
# This line is necessary for differences between ActiveModel::Serializers::JSON#as_json versions
28+
json = as_json(options)[JOIN_TYPE] || as_json(options)
29+
json.merge(JOIN_METADATA)
30+
end
31+
2232
after_commit lambda { __elasticsearch__.index_document }, on: :create
2333
after_commit lambda { __elasticsearch__.update_document }, on: :update
2434
after_commit lambda { __elasticsearch__.delete_document }, on: :destroy
@@ -29,32 +39,55 @@ class Answer < ActiveRecord::Base
2939

3040
belongs_to :question
3141

32-
index_name 'questions_and_answers'
42+
JOIN_TYPE = 'answer'.freeze
43+
44+
index_name 'questions_and_answers'.freeze
45+
document_type 'doc'.freeze
46+
47+
before_create :randomize_id
48+
49+
def randomize_id
50+
begin
51+
self.id = SecureRandom.random_number(1_000_000)
52+
end while Answer.where(id: self.id).exists?
53+
end
3354

34-
mapping _parent: { type: 'question' }, _routing: { required: true } do
55+
mapping do
3556
indexes :text
3657
indexes :author
3758
end
3859

39-
after_commit lambda { __elasticsearch__.index_document(parent: question_id) }, on: :create
40-
after_commit lambda { __elasticsearch__.update_document(parent: question_id) }, on: :update
41-
after_commit lambda { __elasticsearch__.delete_document(parent: question_id) }, on: :destroy
60+
def as_indexed_json(options={})
61+
# This line is necessary for differences between ActiveModel::Serializers::JSON#as_json versions
62+
json = as_json(options)[JOIN_TYPE] || as_json(options)
63+
json.merge(join_field: { name: JOIN_TYPE, parent: question_id })
64+
end
65+
66+
after_commit lambda { __elasticsearch__.index_document(routing: (question_id || 1)) }, on: :create
67+
after_commit lambda { __elasticsearch__.update_document(routing: (question_id || 1)) }, on: :update
68+
after_commit lambda {__elasticsearch__.delete_document(routing: (question_id || 1)) }, on: :destroy
4269
end
4370

4471
module ParentChildSearchable
45-
INDEX_NAME = 'questions_and_answers'
72+
INDEX_NAME = 'questions_and_answers'.freeze
73+
JOIN = 'join'.freeze
4674

4775
def create_index!(options={})
4876
client = Question.__elasticsearch__.client
4977
client.indices.delete index: INDEX_NAME rescue nil if options[:force]
5078

5179
settings = Question.settings.to_hash.merge Answer.settings.to_hash
52-
mappings = Question.mappings.to_hash.merge Answer.mappings.to_hash
80+
mapping_properties = { join_field: { type: JOIN,
81+
relations: { Question::JOIN_TYPE => Answer::JOIN_TYPE } } }
82+
83+
merged_properties = mapping_properties.merge(Question.mappings.to_hash[:doc][:properties]).merge(
84+
Answer.mappings.to_hash[:doc][:properties])
85+
mappings = { doc: { properties: merged_properties }}
5386

5487
client.indices.create index: INDEX_NAME,
5588
body: {
56-
settings: settings.to_hash,
57-
mappings: mappings.to_hash }
89+
settings: settings.to_hash,
90+
mappings: mappings }
5891
end
5992

6093
extend self
@@ -100,34 +133,34 @@ class ActiveRecordAssociationsParentChildIntegrationTest < Elasticsearch::Test::
100133

101134
should "find questions by matching answers" do
102135
response = Question.search(
103-
{ query: {
104-
has_child: {
105-
type: 'answer',
106-
query: {
107-
match: {
108-
author: 'john'
109-
}
110-
}
111-
}
112-
}
113-
})
136+
{ query: {
137+
has_child: {
138+
type: 'answer',
139+
query: {
140+
match: {
141+
author: 'john'
142+
}
143+
}
144+
}
145+
}
146+
})
114147

115148
assert_equal 'Second Question', response.records.first.title
116149
end
117150

118151
should "find answers for matching questions" do
119152
response = Answer.search(
120-
{ query: {
121-
has_parent: {
122-
parent_type: 'question',
123-
query: {
124-
match: {
125-
author: 'john'
126-
}
127-
}
128-
}
129-
}
130-
})
153+
{ query: {
154+
has_parent: {
155+
parent_type: 'question',
156+
query: {
157+
match: {
158+
author: 'john'
159+
}
160+
}
161+
}
162+
}
163+
})
131164

132165
assert_same_elements ['Adam', 'Ryan'], response.records.map(&:author)
133166
end
@@ -136,12 +169,20 @@ class ActiveRecordAssociationsParentChildIntegrationTest < Elasticsearch::Test::
136169
Question.where(title: 'First Question').each(&:destroy)
137170
Question.__elasticsearch__.refresh_index!
138171

139-
response = Answer.search query: { match_all: {} }
172+
response = Answer.search(
173+
{ query: {
174+
has_parent: {
175+
parent_type: 'question',
176+
query: {
177+
match_all: {}
178+
}
179+
}
180+
}
181+
})
140182

141183
assert_equal 1, response.results.total
142184
end
143185
end
144-
145186
end
146187
end
147188
end

elasticsearch-persistence/elasticsearch-persistence.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Gem::Specification.new do |s|
3333
s.add_development_dependency "bundler", "~> 1.5"
3434
s.add_development_dependency "rake", "~> 11.1"
3535

36-
s.add_development_dependency "oj"
36+
s.add_development_dependency "oj" unless defined?(JRUBY_VERSION)
3737

3838
s.add_development_dependency "rails", '> 4'
3939

elasticsearch-rails/elasticsearch-rails.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Gem::Specification.new do |s|
2929
s.add_development_dependency "elasticsearch-extensions"
3030
s.add_development_dependency "elasticsearch-model"
3131

32-
s.add_development_dependency "oj"
32+
s.add_development_dependency "oj" unless defined?(JRUBY_VERSION)
3333
s.add_development_dependency "rails", ">= 3.1"
3434

3535
s.add_development_dependency "lograge"

0 commit comments

Comments
 (0)