diff --git a/.travis.yml b/.travis.yml index 99a2571de..51766c1e1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/elasticsearch-model/Rakefile b/elasticsearch-model/Rakefile index 1efad46da..f80c46b23 100644 --- a/elasticsearch-model/Rakefile +++ b/elasticsearch-model/Rakefile @@ -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 diff --git a/elasticsearch-model/elasticsearch-model.gemspec b/elasticsearch-model/elasticsearch-model.gemspec index e058ac43d..5cd1bcec1 100644 --- a/elasticsearch-model/elasticsearch-model.gemspec +++ b/elasticsearch-model/elasticsearch-model.gemspec @@ -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" diff --git a/elasticsearch-model/test/integration/active_record_associations_parent_child_test.rb b/elasticsearch-model/test/integration/active_record_associations_parent_child_test.rb index de5db34b0..fb3b07e1d 100644 --- a/elasticsearch-model/test/integration/active_record_associations_parent_child_test.rb +++ b/elasticsearch-model/test/integration/active_record_associations_parent_child_test.rb @@ -11,7 +11,11 @@ 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 @@ -19,6 +23,12 @@ class Question < ActiveRecord::Base 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 @@ -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 @@ -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 @@ -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 diff --git a/elasticsearch-persistence/elasticsearch-persistence.gemspec b/elasticsearch-persistence/elasticsearch-persistence.gemspec index 311325fdc..38ba93594 100644 --- a/elasticsearch-persistence/elasticsearch-persistence.gemspec +++ b/elasticsearch-persistence/elasticsearch-persistence.gemspec @@ -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' diff --git a/elasticsearch-rails/elasticsearch-rails.gemspec b/elasticsearch-rails/elasticsearch-rails.gemspec index 71b4c1abb..aa976c675 100644 --- a/elasticsearch-rails/elasticsearch-rails.gemspec +++ b/elasticsearch-rails/elasticsearch-rails.gemspec @@ -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"