Skip to content

[MODEL] Update child-parent integration test to use single index type for ES 6.3 #805

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

Merged
merged 4 commits into from
Jun 28, 2018
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
16 changes: 12 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,27 @@ branches:

matrix:
include:
- rvm: 2.2.10
- rvm: 2.2
jdk: oraclejdk8
env: TEST_SUITE=unit

- rvm: 2.3.7
- rvm: 2.3
jdk: oraclejdk8
env: TEST_SUITE=unit

- rvm: 2.5.1
- rvm: 2.4
jdk: oraclejdk8
env: TEST_SUITE=unit

- rvm: 2.5.1
- rvm: 2.5
jdk: oraclejdk8
env: TEST_SUITE=unit

# - rvm: jruby-9.1
# jdk: oraclejdk8
# env: TEST_SUITE=unit

- rvm: 2.5
jdk: oraclejdk8
env: TEST_SUITE=integration QUIET=y

Expand Down
6 changes: 3 additions & 3 deletions elasticsearch-model/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ namespace :test do
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
end

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

Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-model/elasticsearch-model.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "sqlite3"
s.add_development_dependency "activemodel", "> 3"

s.add_development_dependency "oj"
s.add_development_dependency "oj" unless defined?(JRUBY_VERSION)
s.add_development_dependency "kaminari"
s.add_development_dependency "will_paginate"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,24 @@ class Question < ActiveRecord::Base

has_many :answers, dependent: :destroy

index_name 'questions_and_answers'
JOIN_TYPE = 'question'.freeze
JOIN_METADATA = { join_field: JOIN_TYPE}.freeze

index_name 'questions_and_answers'.freeze
document_type 'doc'.freeze

mapping do
indexes :title
indexes :text
indexes :author
end

def as_indexed_json(options={})
# This line is necessary for differences between ActiveModel::Serializers::JSON#as_json versions
json = as_json(options)[JOIN_TYPE] || as_json(options)
json.merge(JOIN_METADATA)
end

after_commit lambda { __elasticsearch__.index_document }, on: :create
after_commit lambda { __elasticsearch__.update_document }, on: :update
after_commit lambda { __elasticsearch__.delete_document }, on: :destroy
Expand All @@ -29,32 +39,55 @@ class Answer < ActiveRecord::Base

belongs_to :question

index_name 'questions_and_answers'
JOIN_TYPE = 'answer'.freeze

index_name 'questions_and_answers'.freeze
document_type 'doc'.freeze

before_create :randomize_id

def randomize_id
begin
self.id = SecureRandom.random_number(1_000_000)
end while Answer.where(id: self.id).exists?
end

mapping _parent: { type: 'question' }, _routing: { required: true } do
mapping do
indexes :text
indexes :author
end

after_commit lambda { __elasticsearch__.index_document(parent: question_id) }, on: :create
after_commit lambda { __elasticsearch__.update_document(parent: question_id) }, on: :update
after_commit lambda { __elasticsearch__.delete_document(parent: question_id) }, on: :destroy
def as_indexed_json(options={})
# This line is necessary for differences between ActiveModel::Serializers::JSON#as_json versions
json = as_json(options)[JOIN_TYPE] || as_json(options)
json.merge(join_field: { name: JOIN_TYPE, parent: question_id })
end

after_commit lambda { __elasticsearch__.index_document(routing: (question_id || 1)) }, on: :create
after_commit lambda { __elasticsearch__.update_document(routing: (question_id || 1)) }, on: :update
after_commit lambda {__elasticsearch__.delete_document(routing: (question_id || 1)) }, on: :destroy
end

module ParentChildSearchable
INDEX_NAME = 'questions_and_answers'
INDEX_NAME = 'questions_and_answers'.freeze
JOIN = 'join'.freeze

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

settings = Question.settings.to_hash.merge Answer.settings.to_hash
mappings = Question.mappings.to_hash.merge Answer.mappings.to_hash
mapping_properties = { join_field: { type: JOIN,
relations: { Question::JOIN_TYPE => Answer::JOIN_TYPE } } }

merged_properties = mapping_properties.merge(Question.mappings.to_hash[:doc][:properties]).merge(
Answer.mappings.to_hash[:doc][:properties])
mappings = { doc: { properties: merged_properties }}

client.indices.create index: INDEX_NAME,
body: {
settings: settings.to_hash,
mappings: mappings.to_hash }
settings: settings.to_hash,
mappings: mappings }
end

extend self
Expand Down Expand Up @@ -100,34 +133,34 @@ class ActiveRecordAssociationsParentChildIntegrationTest < Elasticsearch::Test::

should "find questions by matching answers" do
response = Question.search(
{ query: {
has_child: {
type: 'answer',
query: {
match: {
author: 'john'
}
}
}
}
})
{ query: {
has_child: {
type: 'answer',
query: {
match: {
author: 'john'
}
}
}
}
})

assert_equal 'Second Question', response.records.first.title
end

should "find answers for matching questions" do
response = Answer.search(
{ query: {
has_parent: {
parent_type: 'question',
query: {
match: {
author: 'john'
}
}
}
}
})
{ query: {
has_parent: {
parent_type: 'question',
query: {
match: {
author: 'john'
}
}
}
}
})

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

response = Answer.search query: { match_all: {} }
response = Answer.search(
{ query: {
has_parent: {
parent_type: 'question',
query: {
match_all: {}
}
}
}
})

assert_equal 1, response.results.total
end
end

end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "bundler", "~> 1.5"
s.add_development_dependency "rake", "~> 11.1"

s.add_development_dependency "oj"
s.add_development_dependency "oj" unless defined?(JRUBY_VERSION)

s.add_development_dependency "rails", '> 4'

Expand Down
2 changes: 1 addition & 1 deletion elasticsearch-rails/elasticsearch-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "elasticsearch-extensions"
s.add_development_dependency "elasticsearch-model"

s.add_development_dependency "oj"
s.add_development_dependency "oj" unless defined?(JRUBY_VERSION)
s.add_development_dependency "rails", ">= 3.1"

s.add_development_dependency "lograge"
Expand Down