From 1fd6c0b087adb13bd1a94e7e50cf4dd856080e37 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Thu, 29 Jan 2015 16:18:50 +0100
Subject: [PATCH 001/412] [MODEL] Added the `:validate` option to the `save`
method for models
article.save validate: false
---
.../elasticsearch/persistence/model/store.rb | 5 ++-
.../integration/model/model_basic_test.rb | 16 ++++++++-
.../test/unit/model_store_test.rb | 34 +++++++++++++++++--
3 files changed, 50 insertions(+), 5 deletions(-)
diff --git a/elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb b/elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb
index 4c435ed6e..72326128a 100644
--- a/elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb
+++ b/elasticsearch-persistence/lib/elasticsearch/persistence/model/store.rb
@@ -44,7 +44,10 @@ module InstanceMethods
# @return [Hash,FalseClass] The Elasticsearch response as a Hash or `false`
#
def save(options={})
- return false unless valid?
+ unless options.delete(:validate) == false
+ return false unless valid?
+ end
+
run_callbacks :save do
options.update id: self.id
options.update index: self._index if self._index
diff --git a/elasticsearch-persistence/test/integration/model/model_basic_test.rb b/elasticsearch-persistence/test/integration/model/model_basic_test.rb
index 6f061322b..172d0bb2f 100644
--- a/elasticsearch-persistence/test/integration/model/model_basic_test.rb
+++ b/elasticsearch-persistence/test/integration/model/model_basic_test.rb
@@ -51,7 +51,7 @@ class ::Person
should "save and find the object" do
person = Person.new name: 'John Smith', birthday: Date.parse('1970-01-01')
- person.save
+ assert person.save
assert_not_nil person.id
document = Person.find(person.id)
@@ -63,6 +63,20 @@ class ::Person
assert_not_nil Elasticsearch::Persistence.client.get index: 'people', type: 'person', id: person.id
end
+ should "not save an invalid object" do
+ person = Person.new name: nil
+ assert ! person.save
+ end
+
+ should "save an invalid object with the :validate option" do
+ person = Person.new name: nil, salary: 100
+ assert person.save validate: false
+
+ assert_not_nil person.id
+ document = Person.find(person.id)
+ assert_equal 100, document.salary
+ end
+
should "delete the object" do
person = Person.create name: 'John Smith', birthday: Date.parse('1970-01-01')
diff --git a/elasticsearch-persistence/test/unit/model_store_test.rb b/elasticsearch-persistence/test/unit/model_store_test.rb
index 3e990a6a3..835f49428 100644
--- a/elasticsearch-persistence/test/unit/model_store_test.rb
+++ b/elasticsearch-persistence/test/unit/model_store_test.rb
@@ -80,10 +80,7 @@ class DummyStoreModel
end
.returns({'_id' => 'abc123'})
- assert ! subject.persisted?
-
assert subject.save
- assert subject.persisted?
end
should "save the model and set the ID" do
@@ -108,6 +105,37 @@ class DummyStoreModel
assert_equal Time.parse('2014-01-01T00:00:00Z'), subject.updated_at
end
+ should "not save an invalid model" do
+ @gateway
+ .expects(:save)
+ .never
+
+ subject.instance_eval do
+ def valid?; false; end;
+ end
+
+ assert ! subject.save
+ assert ! subject.persisted?
+ end
+
+ should "skip the validation with the :validate option" do
+ @gateway
+ .expects(:save)
+ .with do |object, options|
+ assert_equal subject, object
+ assert_equal nil, options[:id]
+ true
+ end
+ .returns({'_id' => 'abc123'})
+
+ subject.instance_eval do
+ def valid?; false; end;
+ end
+
+ assert subject.save validate: false
+ assert subject.persisted?
+ end
+
should "pass the options to gateway" do
@gateway
.expects(:save)
From e5c61b2123b4e1a511914acf275a9abe768236ca Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 30 Jan 2015 13:02:17 +0100
Subject: [PATCH 002/412] [RAILS] Fixed an error in the 01-pretty.rb template
Due to different capitalization of the main header, the search
- <% @articles.response.response['facets']['authors']['terms'].each do |a| %>
+ <% @articles.response.response['aggregations']['authors']['authors']['buckets'].each do |a| %>
<%=
- link_to search_path(params.except(:controller, :action).merge(a: a['term'])),
- class: "list-group-item#{' active' if params[:a] == a['term']}" do
- a['term'].titleize.html_safe + content_tag(:small, a['count'], class: 'badge').html_safe
+ link_to search_path(params.except(:controller, :action).merge(a: a['key'])),
+ class: "list-group-item#{' active' if params[:a] == a['key']}" do
+ a['key'].titleize.html_safe + content_tag(:small, a['doc_count'], class: 'badge').html_safe
end
%>
<% end %>
@@ -98,16 +98,16 @@
<%= link_to 'Any Date →'.html_safe, search_path(params.except(:controller, :action).merge(w: nil))%>
- <% @articles.response.response['facets']['published']['entries'].each do |w| %>
+ <% @articles.response.response['aggregations']['published']['published']['buckets'].each do |w| %>
<%=
- __start = Time.at(w['time']/1000)
+ __start = Time.at(w['key']/1000)
__end = __start.end_of_week
__date = __start.to_date.to_s(:iso)
link_to search_path(params.except(:controller, :action).merge(w: __date)),
class: "list-group-item#{' active' if params[:w] == __date}" do
"#{__start.to_date.to_s(:short)} — #{__end.to_date.to_s(:short)}".html_safe + \
- content_tag(:small, w['count'], class: 'badge').html_safe
+ content_tag(:small, w['doc_count'], class: 'badge').html_safe
end
%>
<% end %>
diff --git a/elasticsearch-rails/lib/rails/templates/search_controller_test.rb b/elasticsearch-rails/lib/rails/templates/search_controller_test.rb
index 7e2248219..5f041befb 100644
--- a/elasticsearch-rails/lib/rails/templates/search_controller_test.rb
+++ b/elasticsearch-rails/lib/rails/templates/search_controller_test.rb
@@ -30,7 +30,7 @@ class SearchControllerTest < ActionController::TestCase
Article.find_by_title('Article Three').comments.create body: 'One'
- Sidekiq::Queue.new("elasticsearch").clear
+ Sidekiq::Worker.clear_all
Article.__elasticsearch__.import force: true
Article.__elasticsearch__.refresh_index!
@@ -45,6 +45,7 @@ class SearchControllerTest < ActionController::TestCase
test "should return search results in comments" do
get :index, q: 'one', comments: 'y'
assert_response :success
+
assert_equal 4, assigns(:articles).size
end
@@ -67,15 +68,15 @@ class SearchControllerTest < ActionController::TestCase
get :index, q: 'one'
assert_response :success
- facets = assigns(:articles).response.response['facets']
+ aggregations = assigns(:articles).response.response['aggregations']
- assert_equal 2, facets['categories']['terms'].size
- assert_equal 2, facets['authors']['terms'].size
- assert_equal 2, facets['published']['entries'].size
+ assert_equal 2, aggregations['categories']['categories']['buckets'].size
+ assert_equal 2, aggregations['authors']['authors']['buckets'].size
+ assert_equal 2, aggregations['published']['published']['buckets'].size
- assert_equal 'One', facets['categories']['terms'][0]['term']
- assert_equal 'John Smith', facets['authors']['terms'][0]['term']
- assert_equal 1425254400000, facets['published']['entries'][0]['time']
+ assert_equal 'One', aggregations['categories']['categories']['buckets'][0]['key']
+ assert_equal 'John Smith', aggregations['authors']['authors']['buckets'][0]['key']
+ assert_equal 1425254400000, aggregations['published']['published']['buckets'][0]['key']
end
test "should sort on the published date" do
@@ -104,13 +105,13 @@ class SearchControllerTest < ActionController::TestCase
assert_equal 2, assigns(:articles).size
- facets = assigns(:articles).response.response['facets']
+ aggregations = assigns(:articles).response.response['aggregations']
- assert_equal 1, facets['authors']['terms'].size
- assert_equal 1, facets['published']['entries'].size
+ assert_equal 1, aggregations['authors']['authors']['buckets'].size
+ assert_equal 1, aggregations['published']['published']['buckets'].size
# Do NOT filter the category facet
- assert_equal 2, facets['categories']['terms'].size
+ assert_equal 2, aggregations['categories']['categories']['buckets'].size
end
test "should filter search results and the category and published date facets when user selects a category" do
@@ -119,12 +120,12 @@ class SearchControllerTest < ActionController::TestCase
assert_equal 1, assigns(:articles).size
- facets = assigns(:articles).response.response['facets']
+ aggregations = assigns(:articles).response.response['aggregations']
- assert_equal 1, facets['categories']['terms'].size
- assert_equal 1, facets['published']['entries'].size
+ assert_equal 1, aggregations['categories']['categories']['buckets'].size
+ assert_equal 1, aggregations['published']['published']['buckets'].size
# Do NOT filter the authors facet
- assert_equal 2, facets['authors']['terms'].size
+ assert_equal 2, aggregations['authors']['authors']['buckets'].size
end
end
diff --git a/elasticsearch-rails/lib/rails/templates/searchable.rb b/elasticsearch-rails/lib/rails/templates/searchable.rb
index 0ff826150..55b031532 100644
--- a/elasticsearch-rails/lib/rails/templates/searchable.rb
+++ b/elasticsearch-rails/lib/rails/templates/searchable.rb
@@ -71,15 +71,14 @@ def as_indexed_json(options={})
#
def self.search(query, options={})
- # Prefill and set the filters (top-level `filter` and `facet_filter` elements)
+ # Prefill and set the filters (top-level `post_filter` and aggregation `filter` elements)
#
__set_filters = lambda do |key, f|
+ @search_definition[:post_filter][:and] ||= []
+ @search_definition[:post_filter][:and] |= [f]
- @search_definition[:filter][:and] ||= []
- @search_definition[:filter][:and] |= [f]
-
- @search_definition[:facets][key.to_sym][:facet_filter][:and] ||= []
- @search_definition[:facets][key.to_sym][:facet_filter][:and] |= [f]
+ @search_definition[:aggregations][key.to_sym][:filter][:bool][:must] ||= []
+ @search_definition[:aggregations][key.to_sym][:filter][:bool][:must] |= [f]
end
@search_definition = {
@@ -95,27 +94,22 @@ def self.search(query, options={})
}
},
- filter: {},
+ post_filter: {},
- facets: {
+ aggregations: {
categories: {
- terms: {
- field: 'categories'
- },
- facet_filter: {}
+ filter: { bool: { must: [ match_all: {} ] } },
+ aggregations: { categories: { terms: { field: 'categories' } } }
},
authors: {
- terms: {
- field: 'authors.full_name.raw'
- },
- facet_filter: {}
+ filter: { bool: { must: [ match_all: {} ] } },
+ aggregations: { authors: { terms: { field: 'authors.full_name.raw' } } }
},
published: {
- date_histogram: {
- field: 'published_on',
- interval: 'week'
- },
- facet_filter: {}
+ filter: { bool: { must: [ match_all: {} ] } },
+ aggregations: {
+ published: { date_histogram: { field: 'published_on', interval: 'week' } }
+ }
}
}
}
@@ -174,7 +168,7 @@ def self.search(query, options={})
query: {
multi_match: {
query: query,
- fields: ['body'],
+ fields: ['comments.body'],
operator: 'and'
}
}
From eab4058eb559a6f03b75e50699261e8e3edd8ccb Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Thu, 21 Jan 2016 22:36:41 +0100
Subject: [PATCH 079/412] [RAILS] Added checks for proper launch order to the
example application templates
In order to prevent confusing errors when users try to run templates not
sequentially, added checks which ensure the templates are run properly
one after another.
---
elasticsearch-rails/lib/rails/templates/02-pretty.rb | 5 ++++-
elasticsearch-rails/lib/rails/templates/03-expert.rb | 5 ++++-
elasticsearch-rails/lib/rails/templates/04-dsl.rb | 5 ++++-
3 files changed, 12 insertions(+), 3 deletions(-)
diff --git a/elasticsearch-rails/lib/rails/templates/02-pretty.rb b/elasticsearch-rails/lib/rails/templates/02-pretty.rb
index 544d328a0..7fd6e5048 100644
--- a/elasticsearch-rails/lib/rails/templates/02-pretty.rb
+++ b/elasticsearch-rails/lib/rails/templates/02-pretty.rb
@@ -1,6 +1,9 @@
# $ rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/02-pretty.rb
-# (See: 01-basic.rb)
+unless File.read('README.rdoc').include? '== [1] Basic'
+ say_status "ERROR", "You have to run the 01-basic.rb template first.", :red
+ exit(1)
+end
puts
say_status "README", "Updating Readme...\n", :yellow
diff --git a/elasticsearch-rails/lib/rails/templates/03-expert.rb b/elasticsearch-rails/lib/rails/templates/03-expert.rb
index 6598f9319..eff2f4725 100644
--- a/elasticsearch-rails/lib/rails/templates/03-expert.rb
+++ b/elasticsearch-rails/lib/rails/templates/03-expert.rb
@@ -1,6 +1,9 @@
# $ rails new searchapp --skip --skip-bundle --template https://raw.github.com/elasticsearch/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/03-expert.rb
-# (See: 01-basic.rb, 02-pretty.rb)
+unless File.read('README.rdoc').include? '== [2] Pretty'
+ say_status "ERROR", "You have to run the 01-basic.rb and 02-pretty.rb templates first.", :red
+ exit(1)
+end
append_to_file 'README.rdoc', <<-README
diff --git a/elasticsearch-rails/lib/rails/templates/04-dsl.rb b/elasticsearch-rails/lib/rails/templates/04-dsl.rb
index 7e3609df1..232903e33 100644
--- a/elasticsearch-rails/lib/rails/templates/04-dsl.rb
+++ b/elasticsearch-rails/lib/rails/templates/04-dsl.rb
@@ -1,6 +1,9 @@
# $ rails new searchapp --skip --skip-bundle --template https://raw.githubusercontent.com/elastic/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/04-dsl.rb
-# (See: 01-basic.rb, 02-pretty.rb, 03-expert.rb)
+unless File.read('README.rdoc').include? '== [3] Expert'
+ say_status "ERROR", "You have to run the 01-basic.rb, 02-pretty.rb and 03-expert.rb templates first.", :red
+ exit(1)
+end
append_to_file 'README.rdoc', <<-README
From e9052ae077beb13296475bb0d29d8326f2c05730 Mon Sep 17 00:00:00 2001
From: nipe
Date: Sun, 25 Oct 2015 17:56:41 +0900
Subject: [PATCH 080/412] [MODEL] Added a `suggest` method to wrap the
suggestions in response
Instead of:
@articles.response.response['suggest']
let's have:
@articles.response.suggest
Closes #483
---
.../lib/elasticsearch/model/response.rb | 6 ++++++
.../integration/active_record_basic_test.rb | 20 +++++++++++++++----
.../test/unit/response_test.rb | 12 ++++++++++-
3 files changed, 33 insertions(+), 5 deletions(-)
diff --git a/elasticsearch-model/lib/elasticsearch/model/response.rb b/elasticsearch-model/lib/elasticsearch/model/response.rb
index efece7143..ec043b8b6 100644
--- a/elasticsearch-model/lib/elasticsearch/model/response.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/response.rb
@@ -71,6 +71,12 @@ def shards
def aggregations
response['aggregations'] ? Hashie::Mash.new(response['aggregations']) : nil
end
+
+ # Returns a Hashie::Mash of the suggest
+ #
+ def suggest
+ response['suggest'] ? Hashie::Mash.new(response['suggest']) : nil
+ end
end
end
end
diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb
index 6fd5467df..4ad62cff7 100644
--- a/elasticsearch-model/test/integration/active_record_basic_test.rb
+++ b/elasticsearch-model/test/integration/active_record_basic_test.rb
@@ -22,11 +22,19 @@ class ::Article < ActiveRecord::Base
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mapping do
- indexes :title, type: 'string', analyzer: 'snowball'
- indexes :body, type: 'string'
- indexes :created_at, type: 'date'
+ indexes :title, type: 'string', analyzer: 'snowball'
+ indexes :suggest_title, type: 'completion'
+ indexes :body, type: 'string'
+ indexes :created_at, type: 'date'
end
end
+
+ def as_indexed_json(options = {})
+ attributes
+ .symbolize_keys
+ .slice(:title, :body, :created_at)
+ .merge(suggest_title: title)
+ end
end
Article.delete_all
@@ -210,10 +218,14 @@ class ::Article < ActiveRecord::Base
should "allow dot access to response" do
response = Article.search query: { match: { title: { query: 'test' } } },
- aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } }
+ aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } },
+ suggest: { title_suggest: { text: 'test', completion: { field: 'suggest_title' } } }
response.response.respond_to?(:aggregations)
assert_equal 2, response.response.aggregations.dates.buckets.first.doc_count
+
+ response.response.respond_to?(:suggest)
+ assert_equal 2, response.response.suggest.title_suggest.first.options.size
end
end
diff --git a/elasticsearch-model/test/unit/response_test.rb b/elasticsearch-model/test/unit/response_test.rb
index 0bd877af0..8e04bb886 100644
--- a/elasticsearch-model/test/unit/response_test.rb
+++ b/elasticsearch-model/test/unit/response_test.rb
@@ -8,7 +8,8 @@ def self.document_type; 'bar'; end
end
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, 'hits' => { 'hits' => [] },
- 'aggregations' => {'foo' => {'bar' => 10}}}
+ 'aggregations' => {'foo' => {'bar' => 10}},
+ 'suggest' => {'my_suggest' => []}}
setup do
@search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
@@ -73,5 +74,14 @@ def self.document_type; 'bar'; end
assert_kind_of Hashie::Mash, response.aggregations.foo
assert_equal 10, response.aggregations.foo.bar
end
+
+ should "access the suggest" do
+ @search.expects(:execute!).returns(RESPONSE)
+
+ response = Elasticsearch::Model::Response::Response.new OriginClass, @search
+ assert_respond_to response, :suggest
+ assert_kind_of Hashie::Mash, response.suggest
+ assert_equal [], response.suggest.my_suggest
+ end
end
end
From 65d64c1aabb3b00d39c2d582ec4f2e11efbeca22 Mon Sep 17 00:00:00 2001
From: nipe
Date: Sun, 25 Oct 2015 20:40:11 +0900
Subject: [PATCH 081/412] [RAILS] Used the `suggest` method instead of
`response['suggest']` in the application template
Related: #483
---
elasticsearch-rails/lib/rails/templates/index.html.dsl.erb | 2 +-
elasticsearch-rails/lib/rails/templates/index.html.erb | 2 +-
.../lib/rails/templates/search_controller_test.dsl.rb | 2 +-
.../lib/rails/templates/search_controller_test.rb | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb b/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb
index 2e7ee6c67..ea9a76e68 100644
--- a/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb
+++ b/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb
@@ -47,7 +47,7 @@
-<% if @articles.size < 1 && (suggestions = @articles.response.response['suggest']) && suggestions.present? %>
+<% if @articles.size < 1 && @articles.response.suggest.present? %>
No documents have been found.
diff --git a/elasticsearch-rails/lib/rails/templates/index.html.erb b/elasticsearch-rails/lib/rails/templates/index.html.erb
index 2e7ee6c67..ea9a76e68 100644
--- a/elasticsearch-rails/lib/rails/templates/index.html.erb
+++ b/elasticsearch-rails/lib/rails/templates/index.html.erb
@@ -47,7 +47,7 @@
-<% if @articles.size < 1 && (suggestions = @articles.response.response['suggest']) && suggestions.present? %>
+<% if @articles.size < 1 && @articles.response.suggest.present? %>
No documents have been found.
diff --git a/elasticsearch-rails/lib/rails/templates/search_controller_test.dsl.rb b/elasticsearch-rails/lib/rails/templates/search_controller_test.dsl.rb
index aa2fae65a..7fe7dd955 100644
--- a/elasticsearch-rails/lib/rails/templates/search_controller_test.dsl.rb
+++ b/elasticsearch-rails/lib/rails/templates/search_controller_test.dsl.rb
@@ -58,7 +58,7 @@ class SearchControllerTest < ActionController::TestCase
get :index, q: 'one'
assert_response :success
- suggestions = assigns(:articles).response.response['suggest']
+ suggestions = assigns(:articles).response.suggest
assert_equal 'one', suggestions['suggest_title'][0]['text']
end
diff --git a/elasticsearch-rails/lib/rails/templates/search_controller_test.rb b/elasticsearch-rails/lib/rails/templates/search_controller_test.rb
index 5f041befb..472d35d75 100644
--- a/elasticsearch-rails/lib/rails/templates/search_controller_test.rb
+++ b/elasticsearch-rails/lib/rails/templates/search_controller_test.rb
@@ -59,7 +59,7 @@ class SearchControllerTest < ActionController::TestCase
get :index, q: 'one'
assert_response :success
- suggestions = assigns(:articles).response.response['suggest']
+ suggestions = assigns(:articles).response.suggest
assert_equal 'one', suggestions['suggest_title'][0]['text']
end
From 23d1ab708a473f7ee1ce89fb231a47795c11a7af Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 22 Jan 2016 09:43:33 +0100
Subject: [PATCH 082/412] [RAILS] Fixed incorrect suggestions handling in the
view
Fixes 65d64c1
Related #483
---
elasticsearch-rails/lib/rails/templates/index.html.erb | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/elasticsearch-rails/lib/rails/templates/index.html.erb b/elasticsearch-rails/lib/rails/templates/index.html.erb
index ea9a76e68..9d849ef04 100644
--- a/elasticsearch-rails/lib/rails/templates/index.html.erb
+++ b/elasticsearch-rails/lib/rails/templates/index.html.erb
@@ -47,13 +47,13 @@
-<% if @articles.size < 1 && @articles.response.suggest.present? %>
+<% if @articles.size < 1 && @articles.response.suggest.present? %>
No documents have been found.
- <% if suggestions['suggest_title'].present? || suggestions['suggest_body'].present? %>
+ <% if @articles.response.suggest['suggest_title'].present? || @articles.response.suggest['suggest_body'].present? %>
Maybe you mean
- <%= suggestions.map { |k,v| v.first['options'] }.flatten.map {|v| v['text']}.uniq.map do |term|
+ <%= @articles.response.suggest.map { |k,v| v.first['options'] }.flatten.map {|v| v['text']}.uniq.map do |term|
link_to term, search_path(params.except(:controller, :action).merge q: term)
end.to_sentence(last_word_connector: ' or ').html_safe %>?
<% end %>
From 4556f14108d689f675ec8989083ee8548db730b3 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 22 Jan 2016 10:56:13 +0100
Subject: [PATCH 083/412] [MODEL] Added the "sqlite3" gem into the Gemfiles
---
elasticsearch-model/gemfiles/3.0.gemfile | 1 +
elasticsearch-model/gemfiles/4.0.gemfile | 1 +
2 files changed, 2 insertions(+)
diff --git a/elasticsearch-model/gemfiles/3.0.gemfile b/elasticsearch-model/gemfiles/3.0.gemfile
index e2bc44b02..23cbdf53d 100644
--- a/elasticsearch-model/gemfiles/3.0.gemfile
+++ b/elasticsearch-model/gemfiles/3.0.gemfile
@@ -10,3 +10,4 @@ gemspec path: '../'
gem 'activemodel', '>= 3.0'
gem 'activerecord', '~> 3.2'
gem 'mongoid', '>= 3.0'
+gem 'sqlite3'
diff --git a/elasticsearch-model/gemfiles/4.0.gemfile b/elasticsearch-model/gemfiles/4.0.gemfile
index 069520d23..89044bb19 100644
--- a/elasticsearch-model/gemfiles/4.0.gemfile
+++ b/elasticsearch-model/gemfiles/4.0.gemfile
@@ -9,3 +9,4 @@ gemspec path: '../'
gem 'activemodel', '~> 4'
gem 'activerecord', '~> 4'
+gem 'sqlite3'
From c7a7ad97579e84aa237ef8636a97616720f05dcd Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 22 Jan 2016 14:58:50 +0100
Subject: [PATCH 084/412] [MODEL] Refactored the suggestions output wrapper
Refactored the accessor for the `suggest` part of the response introduced in #483
to allow a richer interface.
Added a `terms` helper method which just returns a flat array of
response.suggestions.terms
# => [ 'Foo', 'Bar' ]
Related: https://github.com/rubygems/rubygems.org/pull/1135
---
.../lib/elasticsearch/model.rb | 1 +
.../lib/elasticsearch/model/response.rb | 6 ++---
.../model/response/suggestions.rb | 13 ++++++++++
.../integration/active_record_basic_test.rb | 8 +++---
.../test/unit/response_test.rb | 25 ++++++++++++++++---
5 files changed, 42 insertions(+), 11 deletions(-)
create mode 100644 elasticsearch-model/lib/elasticsearch/model/response/suggestions.rb
diff --git a/elasticsearch-model/lib/elasticsearch/model.rb b/elasticsearch-model/lib/elasticsearch/model.rb
index ce05d215d..9d2b93da5 100644
--- a/elasticsearch-model/lib/elasticsearch/model.rb
+++ b/elasticsearch-model/lib/elasticsearch/model.rb
@@ -31,6 +31,7 @@
require 'elasticsearch/model/response/results'
require 'elasticsearch/model/response/records'
require 'elasticsearch/model/response/pagination'
+require 'elasticsearch/model/response/suggestions'
require 'elasticsearch/model/ext/active_record'
diff --git a/elasticsearch-model/lib/elasticsearch/model/response.rb b/elasticsearch-model/lib/elasticsearch/model/response.rb
index ec043b8b6..e85a1fd9f 100644
--- a/elasticsearch-model/lib/elasticsearch/model/response.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/response.rb
@@ -72,10 +72,10 @@ def aggregations
response['aggregations'] ? Hashie::Mash.new(response['aggregations']) : nil
end
- # Returns a Hashie::Mash of the suggest
+ # Returns a Hashie::Mash of the suggestions
#
- def suggest
- response['suggest'] ? Hashie::Mash.new(response['suggest']) : nil
+ def suggestions
+ Suggestions.new(response['suggest'])
end
end
end
diff --git a/elasticsearch-model/lib/elasticsearch/model/response/suggestions.rb b/elasticsearch-model/lib/elasticsearch/model/response/suggestions.rb
new file mode 100644
index 000000000..5088767ce
--- /dev/null
+++ b/elasticsearch-model/lib/elasticsearch/model/response/suggestions.rb
@@ -0,0 +1,13 @@
+module Elasticsearch
+ module Model
+ module Response
+
+ class Suggestions < Hashie::Mash
+ def terms
+ self.to_a.map { |k,v| v.first['options'] }.flatten.map {|v| v['text']}.uniq
+ end
+ end
+
+ end
+ end
+end
diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb
index 4ad62cff7..e6ca97d6d 100644
--- a/elasticsearch-model/test/integration/active_record_basic_test.rb
+++ b/elasticsearch-model/test/integration/active_record_basic_test.rb
@@ -23,7 +23,6 @@ class ::Article < ActiveRecord::Base
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
mapping do
indexes :title, type: 'string', analyzer: 'snowball'
- indexes :suggest_title, type: 'completion'
indexes :body, type: 'string'
indexes :created_at, type: 'date'
end
@@ -219,13 +218,14 @@ def as_indexed_json(options = {})
should "allow dot access to response" do
response = Article.search query: { match: { title: { query: 'test' } } },
aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } },
- suggest: { title_suggest: { text: 'test', completion: { field: 'suggest_title' } } }
+ suggest: { text: 'tezt', title: { term: { field: 'title', suggest_mode: 'always' } } }
response.response.respond_to?(:aggregations)
- assert_equal 2, response.response.aggregations.dates.buckets.first.doc_count
+ assert_equal 2, response.aggregations.dates.buckets.first.doc_count
response.response.respond_to?(:suggest)
- assert_equal 2, response.response.suggest.title_suggest.first.options.size
+ assert_equal 1, response.suggestions.title.first.options.size
+ assert_equal ['test'], response.suggestions.terms
end
end
diff --git a/elasticsearch-model/test/unit/response_test.rb b/elasticsearch-model/test/unit/response_test.rb
index 8e04bb886..71cfb2d6d 100644
--- a/elasticsearch-model/test/unit/response_test.rb
+++ b/elasticsearch-model/test/unit/response_test.rb
@@ -9,7 +9,7 @@ def self.document_type; 'bar'; end
RESPONSE = { 'took' => '5', 'timed_out' => false, '_shards' => {'one' => 'OK'}, 'hits' => { 'hits' => [] },
'aggregations' => {'foo' => {'bar' => 10}},
- 'suggest' => {'my_suggest' => []}}
+ 'suggest' => {'my_suggest' => [ { 'text' => 'foo', 'options' => [ { 'text' => 'Foo', 'score' => 2.0 }, { 'text' => 'Bar', 'score' => 1.0 } ] } ]}}
setup do
@search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
@@ -79,9 +79,26 @@ def self.document_type; 'bar'; end
@search.expects(:execute!).returns(RESPONSE)
response = Elasticsearch::Model::Response::Response.new OriginClass, @search
- assert_respond_to response, :suggest
- assert_kind_of Hashie::Mash, response.suggest
- assert_equal [], response.suggest.my_suggest
+
+ assert_respond_to response, :suggestions
+ assert_kind_of Hashie::Mash, response.suggestions
+ assert_equal 'Foo', response.suggestions.my_suggest.first.options.first.text
+ end
+
+ should "return array of terms from the suggestions" do
+ @search.expects(:execute!).returns(RESPONSE)
+ response = Elasticsearch::Model::Response::Response.new OriginClass, @search
+
+ assert_not_empty response.suggestions
+ assert_equal [ 'Foo', 'Bar' ], response.suggestions.terms
+ end
+
+ should "return empty array as suggest terms when there are no suggestions" do
+ @search.expects(:execute!).returns({})
+ response = Elasticsearch::Model::Response::Response.new OriginClass, @search
+
+ assert_empty response.suggestions
+ assert_equal [], response.suggestions.terms
end
end
end
From e191fa70c0bfc691d354c926eb92a2ebadb3cc9e Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 22 Jan 2016 15:07:42 +0100
Subject: [PATCH 085/412] [RAILS] Changed the view in the example application
to use the helper method for suggestions
Simplify the view code and use the `terms` helper.
---
elasticsearch-rails/lib/rails/templates/index.html.dsl.erb | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb b/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb
index ea9a76e68..4f8d512b0 100644
--- a/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb
+++ b/elasticsearch-rails/lib/rails/templates/index.html.dsl.erb
@@ -47,13 +47,13 @@
-<% if @articles.size < 1 && @articles.response.suggest.present? %>
+<% if @articles.size < 1 && @articles.response.suggestions.present? %>
No documents have been found.
- <% if suggestions['suggest_title'].present? || suggestions['suggest_body'].present? %>
+ <% if @articles.response.suggestions.terms.present? %>
Maybe you mean
- <%= suggestions.map { |k,v| v.first['options'] }.flatten.map {|v| v['text']}.uniq.map do |term|
+ <%= @articles.response.suggestions.terms.map do |term|
link_to term, search_path(params.except(:controller, :action).merge q: term)
end.to_sentence(last_word_connector: ' or ').html_safe %>?
<% end %>
From 164a2470831cc45e40fa9c2c8bc1d3724b3fdfac Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 22 Jan 2016 16:26:12 +0100
Subject: [PATCH 086/412] [RAILS] Fixed missing `git add` in the 03-expert.rb
application template
---
elasticsearch-rails/lib/rails/templates/03-expert.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/elasticsearch-rails/lib/rails/templates/03-expert.rb b/elasticsearch-rails/lib/rails/templates/03-expert.rb
index eff2f4725..267ddf2ed 100644
--- a/elasticsearch-rails/lib/rails/templates/03-expert.rb
+++ b/elasticsearch-rails/lib/rails/templates/03-expert.rb
@@ -196,7 +196,7 @@ class Article < ActiveRecord::Base
"require 'sidekiq/testing'\n\n",
before: "class ActiveSupport::TestCase\n"
-git add: "Gemfile* app/workers/"
+git add: "Gemfile* app/workers/ test/test_helper.rb"
git commit: "-m 'Added a Sidekiq indexer\n\nRun:\n\n $ bundle exec sidekiq --queue elasticsearch --verbose\n\nSee http://sidekiq.org'"
# ----- Add SearchController -----------------------------------------------------------------------
From eb48291685174be79262a9d8d804f52bde8e0862 Mon Sep 17 00:00:00 2001
From: Joe Ferris
Date: Tue, 5 Jan 2016 17:40:18 -0500
Subject: [PATCH 087/412] [MODEL] Fixed `#dup` behaviour for
Elasticsearch::Model
Previously, calling #dup for an Elasticsearch::Model instance would
retain the original __elasticsearch__ reference. Given the following
example:
user = User.create!(name: "Will")
other = user.dup
other.update!(name: "Bill")
You'd end up with two references to "Will" in Elasticsearch, and none
for "Bill," because the duplicate instance proxied to the original
instance's attributes.
With this fix, each duplicate gets its own proxy, so attributes are
saved correctly.
Closes #517
---
elasticsearch-model/lib/elasticsearch/model/proxy.rb | 9 +++++++++
elasticsearch-model/test/unit/proxy_test.rb | 11 +++++++++++
2 files changed, 20 insertions(+)
diff --git a/elasticsearch-model/lib/elasticsearch/model/proxy.rb b/elasticsearch-model/lib/elasticsearch/model/proxy.rb
index 64dd4da1f..3e37f28ec 100644
--- a/elasticsearch-model/lib/elasticsearch/model/proxy.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/proxy.rb
@@ -66,6 +66,15 @@ def __elasticsearch__ &block
end
end
+ # @overload dup
+ #
+ # Returns a copy of this object. Resets the __elasticsearch__ proxy so
+ # the duplicate will build its own proxy.
+ def initialize_dup(_)
+ @__elasticsearch__ = nil
+ super
+ end
+
# Common module for the proxy classes
#
module Base
diff --git a/elasticsearch-model/test/unit/proxy_test.rb b/elasticsearch-model/test/unit/proxy_test.rb
index 6cd745ce6..d7299f884 100644
--- a/elasticsearch-model/test/unit/proxy_test.rb
+++ b/elasticsearch-model/test/unit/proxy_test.rb
@@ -69,6 +69,17 @@ def changes
assert_equal 'insta barr', DummyProxyModel.new.__elasticsearch__.bar
end
+ should "reset the proxy target for duplicates" do
+ model = DummyProxyModel.new
+ model_target = model.__elasticsearch__.target
+ duplicate = model.dup
+ duplicate_target = duplicate.__elasticsearch__.target
+
+ assert_not_equal model, duplicate
+ assert_equal model, model_target
+ assert_equal duplicate, duplicate_target
+ end
+
should "return the proxy class from instance proxy" do
assert_equal Elasticsearch::Model::Proxy::ClassMethodsProxy, DummyProxyModel.new.__elasticsearch__.class.class
end
From 489ab24998a87a0323c3085e29aad14367bfaf89 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 22 Jan 2016 19:40:39 +0100
Subject: [PATCH 088/412] [RAILS] Added checks for Redis availability to the
03-expert.rb template
Related: #521
---
.../lib/rails/templates/03-expert.rb | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/elasticsearch-rails/lib/rails/templates/03-expert.rb b/elasticsearch-rails/lib/rails/templates/03-expert.rb
index 267ddf2ed..ec098a420 100644
--- a/elasticsearch-rails/lib/rails/templates/03-expert.rb
+++ b/elasticsearch-rails/lib/rails/templates/03-expert.rb
@@ -5,6 +5,22 @@
exit(1)
end
+begin
+ require 'redis'
+rescue LoadError
+ say_status "ERROR", "Please install the 'redis' gem before running this template", :red
+ exit(1)
+end
+
+begin
+ Redis.new.info
+rescue Redis::CannotConnectError
+ puts
+ say_status "ERROR", "Redis not available", :red
+ say_status "", "This template uses an asynchronous indexer via Sidekiq, and requires a running Redis server."
+ exit(1)
+end
+
append_to_file 'README.rdoc', <<-README
== [3] Expert
From 35287a5d9a60c9740df5e6f9c353e4a8251997c1 Mon Sep 17 00:00:00 2001
From: Kenta Suzuki
Date: Thu, 25 Feb 2016 23:25:49 +0900
Subject: [PATCH 089/412] [CI] Update Bundler to 1.11.2 on Travis
Latest builds on Travis are broken because of bundler is too old and contains some issues
See: bundler/bundler#3558, travis-ci/travis-ci#3531
Related: #538
---
.travis.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index 3bdba79ff..72704bbaa 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -29,7 +29,7 @@ before_script:
script:
- SERVER=launch TEST_CLUSTER_COMMAND=/usr/share/elasticsearch/bin/elasticsearch TEST_CLUSTER_PARAMS='-Des.default.path.conf=/etc/elasticsearch/ -Des.default.path.logs==/var/log/elasticsearch/' bundle exec rake test:all
-install: "gem uninstall bundler -axI && gem install bundler -v 1.6.2"
+install: "gem uninstall bundler -axI && gem install bundler -v 1.11.2"
notifications:
disable: true
From ee824a5e055c18c90128fe188f86d3352217783e Mon Sep 17 00:00:00 2001
From: Kenta Suzuki
Date: Fri, 26 Feb 2016 00:03:12 +0900
Subject: [PATCH 090/412] [CI] Enable running on the new Travis'
container-based infrastructure
* Disable sudo (https://docs.travis-ci.com/user/workers/container-based-infrastructure/)
* Fix buffer overflow
Buffer overflow in Java_java_net_Inet4AddressImpl_getLocalHostName of OpenJDK6 and OpenJDK 7 (Refs: travis-ci/travis-ci#5227). Travis container-based infrastructure does not suffer from the long hostnames.
Closes: #538
---
.travis.yml | 2 ++
1 file changed, 2 insertions(+)
diff --git a/.travis.yml b/.travis.yml
index 72704bbaa..9c148e5db 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -33,3 +33,5 @@ install: "gem uninstall bundler -axI && gem install bundler -v 1.11.2"
notifications:
disable: true
+
+sudo: false
From 88bb6e850666fef8734e54ff1c4674538ffb4b44 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Thu, 10 Mar 2016 13:44:51 +0100
Subject: [PATCH 091/412] [CI] Move the Bundler install command on Travis to
the `before_install` section
Related: #538
---
.travis.yml | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 9c148e5db..d69951ae2 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -20,6 +20,11 @@ jdk:
services:
- mongodb
+before_install:
+ - gem update --system
+ - gem --version
+ - gem install bundler -v 1.11.2
+
before_script:
- ls -la /usr/share/elasticsearch/bin/elasticsearch
- echo $PWD
@@ -29,8 +34,6 @@ before_script:
script:
- SERVER=launch TEST_CLUSTER_COMMAND=/usr/share/elasticsearch/bin/elasticsearch TEST_CLUSTER_PARAMS='-Des.default.path.conf=/etc/elasticsearch/ -Des.default.path.logs==/var/log/elasticsearch/' bundle exec rake test:all
-install: "gem uninstall bundler -axI && gem install bundler -v 1.11.2"
-
notifications:
disable: true
From c9cff49f84f5e813a03acedf5c7bf54109772737 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Thu, 10 Mar 2016 13:48:34 +0100
Subject: [PATCH 092/412] [CI] Added, that Elasticsearch version is printed in
`before_script`
---
.travis.yml | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/.travis.yml b/.travis.yml
index d69951ae2..39d626bdb 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -27,7 +27,7 @@ before_install:
before_script:
- ls -la /usr/share/elasticsearch/bin/elasticsearch
- - echo $PWD
+ - elasticsearch -v
- rake bundle:clean
- rake bundle:install
From 599b2952969876dd69777bd70bb40c7112ff3975 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Thu, 10 Mar 2016 14:05:36 +0100
Subject: [PATCH 093/412] [CI] Pinned Rake to version 10.x to prevent clashes
with the Yard gem
The builds have been failing on Travis with:
cd /home/travis/build/elastic/elasticsearch-rails/elasticsearch-model && unset BUNDLE_GEMFILE && bundle exec rake test:unit
rm -rf tmp/reports
rake aborted!
NoMethodError: undefined method `last_comment' for #
/home/travis/.rvm/gems/ruby-1.9.3-p551/gems/yard-0.8.7.6/lib/yard/rake/yardoc_task.rb:69:in `define'
Related: #538
---
Gemfile | 2 +-
elasticsearch-model/elasticsearch-model.gemspec | 2 +-
elasticsearch-persistence/elasticsearch-persistence.gemspec | 2 +-
elasticsearch-rails/elasticsearch-rails.gemspec | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/Gemfile b/Gemfile
index b47e577b9..9bd388126 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
gem "bundler", "~> 1"
-gem "rake"
+gem "rake", "< 11.0"
gem 'elasticsearch'
gem 'elasticsearch-extensions'
diff --git a/elasticsearch-model/elasticsearch-model.gemspec b/elasticsearch-model/elasticsearch-model.gemspec
index c943c4002..df9509f06 100644
--- a/elasticsearch-model/elasticsearch-model.gemspec
+++ b/elasticsearch-model/elasticsearch-model.gemspec
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
s.add_dependency "hashie"
s.add_development_dependency "bundler", "~> 1.3"
- s.add_development_dependency "rake"
+ s.add_development_dependency "rake", "< 11.0"
s.add_development_dependency "elasticsearch-extensions"
diff --git a/elasticsearch-persistence/elasticsearch-persistence.gemspec b/elasticsearch-persistence/elasticsearch-persistence.gemspec
index c7a3b44e4..d71bfcf93 100644
--- a/elasticsearch-persistence/elasticsearch-persistence.gemspec
+++ b/elasticsearch-persistence/elasticsearch-persistence.gemspec
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
s.add_dependency "virtus"
s.add_development_dependency "bundler", "~> 1.5"
- s.add_development_dependency "rake"
+ s.add_development_dependency "rake", "< 11.0"
s.add_development_dependency "oj"
diff --git a/elasticsearch-rails/elasticsearch-rails.gemspec b/elasticsearch-rails/elasticsearch-rails.gemspec
index 5901601a3..14dee1ddf 100644
--- a/elasticsearch-rails/elasticsearch-rails.gemspec
+++ b/elasticsearch-rails/elasticsearch-rails.gemspec
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 1.9.3"
s.add_development_dependency "bundler", "~> 1.3"
- s.add_development_dependency "rake"
+ s.add_development_dependency "rake", "< 11.0"
s.add_development_dependency "elasticsearch-extensions"
s.add_development_dependency "elasticsearch-model"
From a714d5931182501531d2ce6b767bcb3c97ce56a7 Mon Sep 17 00:00:00 2001
From: "Tobias L. Maier"
Date: Sat, 5 Mar 2016 22:21:22 +0100
Subject: [PATCH 094/412] [RAILS] Copy files from Github rather than locally in
05-settings-files.rb
Closes #541
---
elasticsearch-rails/lib/rails/templates/05-settings-files.rb | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/elasticsearch-rails/lib/rails/templates/05-settings-files.rb b/elasticsearch-rails/lib/rails/templates/05-settings-files.rb
index 3ce868b6e..9717699fa 100644
--- a/elasticsearch-rails/lib/rails/templates/05-settings-files.rb
+++ b/elasticsearch-rails/lib/rails/templates/05-settings-files.rb
@@ -25,7 +25,9 @@
# ----- Copy the articles_settings.json file -------------------------------------------------------
-copy_file File.expand_path('../articles_settings.json', __FILE__), 'config/elasticsearch/articles_settings.json'
+# copy_file File.expand_path('../articles_settings.json', __FILE__), 'config/elasticsearch/articles_settings.json'
+get 'https://raw.githubusercontent.com/elastic/elasticsearch-rails/master/elasticsearch-rails/lib/rails/templates/articles_settings.json',
+ 'config/elasticsearch/articles_settings.json', force: true
git add: "config/elasticsearch/articles_settings.json"
git commit: "-m 'Create the articles settings file'"
From 15be8e86299cb888d459b093ead4c64ff7ecba19 Mon Sep 17 00:00:00 2001
From: David Padilla
Date: Tue, 22 Sep 2015 15:31:33 -0500
Subject: [PATCH 095/412] [MODEL] Added the `:includes` option to
Adapter::ActiveRecord::Records
This patch adds the `:includes` option to ActiveRecord adapter
which can be used to eager load submodels just like
you'd do with regular ActiveRecord.
This helps to eliminate the N+1 problem when using
elasticsearch-model searches.
Example:
class Article < ActiveRecord::Base
belongs_to :author
# ...
end
Article.search(...).records(includes: [:author])
# Would be similar to using:
Article.includes(:author).where(...)
Closes #472
---
.../lib/elasticsearch/model/adapters/active_record.rb | 7 +++++++
.../lib/elasticsearch/model/response.rb | 4 ++--
.../lib/elasticsearch/model/response/records.rb | 3 +++
.../test/unit/adapter_active_record_test.rb | 10 ++++++++++
4 files changed, 22 insertions(+), 2 deletions(-)
diff --git a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
index b0f61bc8a..2d9bb5378 100644
--- a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
@@ -10,10 +10,17 @@ module ActiveRecord
lambda { |klass| !!defined?(::ActiveRecord::Base) && klass.respond_to?(:ancestors) && klass.ancestors.include?(::ActiveRecord::Base) }
module Records
+ attr_writer :options
+
+ def options
+ @options ||= {}
+ end
+
# Returns an `ActiveRecord::Relation` instance
#
def records
sql_records = klass.where(klass.primary_key => ids)
+ sql_records = sql_records.includes(self.options[:includes]) if self.options[:includes]
# Re-order records based on the order from Elasticsearch hits
# by redefining `to_a`, unless the user has called `order()`
diff --git a/elasticsearch-model/lib/elasticsearch/model/response.rb b/elasticsearch-model/lib/elasticsearch/model/response.rb
index e85a1fd9f..fad3828b3 100644
--- a/elasticsearch-model/lib/elasticsearch/model/response.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/response.rb
@@ -44,8 +44,8 @@ def results
#
# @return [Records]
#
- def records
- @records ||= Records.new(klass, self)
+ def records(options = {})
+ @records ||= Records.new(klass, self, options)
end
# Returns the "took" time
diff --git a/elasticsearch-model/lib/elasticsearch/model/response/records.rb b/elasticsearch-model/lib/elasticsearch/model/response/records.rb
index cd936559b..4638ca689 100644
--- a/elasticsearch-model/lib/elasticsearch/model/response/records.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/response/records.rb
@@ -12,6 +12,8 @@ class Records
delegate :each, :empty?, :size, :slice, :[], :to_a, :to_ary, to: :records
+ attr_accessor :options
+
include Base
# @see Base#initialize
@@ -25,6 +27,7 @@ def initialize(klass, response, options={})
metaclass = class << self; self; end
metaclass.__send__ :include, adapter.records_mixin
+ self.options = options
self
end
diff --git a/elasticsearch-model/test/unit/adapter_active_record_test.rb b/elasticsearch-model/test/unit/adapter_active_record_test.rb
index 19fa238f4..335e3bd10 100644
--- a/elasticsearch-model/test/unit/adapter_active_record_test.rb
+++ b/elasticsearch-model/test/unit/adapter_active_record_test.rb
@@ -51,6 +51,16 @@ def ids
instance.load
end
+ should "load the records with its submodels when using :includes" do
+ klass = mock('class', primary_key: :some_key, where: @records)
+ @records.expects(:includes).with([:submodel]).at_least_once
+
+ instance = DummyClassForActiveRecord.new
+ instance.expects(:klass).returns(klass).at_least_once
+ instance.options[:includes] = [:submodel]
+ instance.records
+ end
+
should "reorder the records based on hits order" do
@records.instance_variable_set(:@records, @records)
From 39c1456c4138c8681e54332f907dddf312e1d8dc Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Wed, 4 May 2016 17:25:17 +0200
Subject: [PATCH 096/412] [MODEL] Added an integration test for the eager
loading in 22a4c1c
Related: #472
---
.../active_record_associations_test.rb | 21 ++++++++++++++++++-
1 file changed, 20 insertions(+), 1 deletion(-)
diff --git a/elasticsearch-model/test/integration/active_record_associations_test.rb b/elasticsearch-model/test/integration/active_record_associations_test.rb
index 984d4fe37..af67ad889 100644
--- a/elasticsearch-model/test/integration/active_record_associations_test.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_test.rb
@@ -133,11 +133,16 @@ def as_indexed_json(options={})
# Include the search integration
#
Post.__send__ :include, Searchable
+ Comment.__send__ :include, Elasticsearch::Model
+ Comment.__send__ :include, Elasticsearch::Model::Callbacks
- # ----- Reset the index -----------------------------------------------------------------
+ # ----- Reset the indices -----------------------------------------------------------------
Post.delete_all
Post.__elasticsearch__.create_index! force: true
+
+ Comment.delete_all
+ Comment.__elasticsearch__.create_index! force: true
end
should "index and find a document" do
@@ -300,6 +305,20 @@ def as_indexed_json(options={})
assert_equal 0, Post.search('categories:One').size
assert_equal 1, Post.search('categories:Updated').size
end
+
+ should "eagerly load associated records" do
+ post_1 = Post.create(title: 'One')
+ post_2 = Post.create(title: 'Two')
+ post_1.comments.create text: 'First comment'
+ post_1.comments.create text: 'Second comment'
+
+ Comment.__elasticsearch__.refresh_index!
+
+ records = Comment.search('first').records(includes: :post)
+
+ assert records.first.association(:post).loaded?, "The associated Post should be eagerly loaded"
+ assert_equal 'One', records.first.post.title
+ end
end
end
From 138781992a8a945bd716a83718fc74d54229baa6 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Thu, 5 May 2016 17:00:59 +0200
Subject: [PATCH 097/412] Release 0.1.9
---
elasticsearch-model/CHANGELOG.md | 8 ++++++++
elasticsearch-model/lib/elasticsearch/model/version.rb | 2 +-
elasticsearch-persistence/CHANGELOG.md | 4 ++++
.../lib/elasticsearch/persistence/version.rb | 2 +-
elasticsearch-rails/CHANGELOG.md | 6 ++++++
elasticsearch-rails/lib/elasticsearch/rails/version.rb | 2 +-
6 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/elasticsearch-model/CHANGELOG.md b/elasticsearch-model/CHANGELOG.md
index 55db448ef..1b2803383 100644
--- a/elasticsearch-model/CHANGELOG.md
+++ b/elasticsearch-model/CHANGELOG.md
@@ -1,3 +1,11 @@
+## 0.1.9
+
+* Added a `suggest` method to wrap the suggestions in response
+* Added the `:includes` option to Adapter::ActiveRecord::Records for eagerly loading associated models
+* Delegated `max_pages` method properly for Kaminari's `next_page`
+* Fixed `#dup` behaviour for Elasticsearch::Model
+* Fixed typos in the README and examples
+
## 0.1.8
* Added "default per page" methods for pagination with multi model searches
diff --git a/elasticsearch-model/lib/elasticsearch/model/version.rb b/elasticsearch-model/lib/elasticsearch/model/version.rb
index 2b34a8b27..44cfdabea 100644
--- a/elasticsearch-model/lib/elasticsearch/model/version.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/version.rb
@@ -1,5 +1,5 @@
module Elasticsearch
module Model
- VERSION = "0.1.8"
+ VERSION = "0.1.9"
end
end
diff --git a/elasticsearch-persistence/CHANGELOG.md b/elasticsearch-persistence/CHANGELOG.md
index cde1f6211..7d25720bb 100644
--- a/elasticsearch-persistence/CHANGELOG.md
+++ b/elasticsearch-persistence/CHANGELOG.md
@@ -1,3 +1,7 @@
+## 0.1.9
+
+* Added, that raw `_source` is accessible from a model instance
+
## 0.1.8
* Added `cluster.health wait_for_status: 'yellow'` to Repository integration test
diff --git a/elasticsearch-persistence/lib/elasticsearch/persistence/version.rb b/elasticsearch-persistence/lib/elasticsearch/persistence/version.rb
index 4c103e53e..93852c314 100644
--- a/elasticsearch-persistence/lib/elasticsearch/persistence/version.rb
+++ b/elasticsearch-persistence/lib/elasticsearch/persistence/version.rb
@@ -1,5 +1,5 @@
module Elasticsearch
module Persistence
- VERSION = "0.1.8"
+ VERSION = "0.1.9"
end
end
diff --git a/elasticsearch-rails/CHANGELOG.md b/elasticsearch-rails/CHANGELOG.md
index a32bfb7cc..5bb4e13bd 100644
--- a/elasticsearch-rails/CHANGELOG.md
+++ b/elasticsearch-rails/CHANGELOG.md
@@ -1,3 +1,9 @@
+## 0.1.9
+
+* Added checks for proper launch order and other updates to the example application templates
+* Updated the example application to work with Elasticsearch 2.x
+* Used the `suggest` method instead of `response['suggest']` in the application template
+
## 0.1.8
* Added an example application template that loads settings from a file
diff --git a/elasticsearch-rails/lib/elasticsearch/rails/version.rb b/elasticsearch-rails/lib/elasticsearch/rails/version.rb
index 5ae9014f7..88b2dd758 100644
--- a/elasticsearch-rails/lib/elasticsearch/rails/version.rb
+++ b/elasticsearch-rails/lib/elasticsearch/rails/version.rb
@@ -1,5 +1,5 @@
module Elasticsearch
module Rails
- VERSION = "0.1.8"
+ VERSION = "0.1.9"
end
end
From 31d04c7b482b0008a4bdb4835dd06939e63efbd8 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 10 May 2016 10:29:49 +0200
Subject: [PATCH 098/412] Updated the Rake dependency to 11.1
In 599b295, the Rake dependency has been pinned to < 11, but that prevents installing
of gems depending on later versions. The offending `last_comment` method was restored in Rake 11.1,
so it is safe to pin that version (relaxed). Furthermore, a patch to Yard is coming up which
fixes the original problem there.
Thanks for reporting and all the pointers, @dominicsayers!
Closes #566
Related: lsegal/yard#964
---
Gemfile | 2 +-
elasticsearch-model/elasticsearch-model.gemspec | 2 +-
elasticsearch-persistence/elasticsearch-persistence.gemspec | 2 +-
elasticsearch-rails/elasticsearch-rails.gemspec | 2 +-
4 files changed, 4 insertions(+), 4 deletions(-)
diff --git a/Gemfile b/Gemfile
index 9bd388126..13be68981 100644
--- a/Gemfile
+++ b/Gemfile
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
gem "bundler", "~> 1"
-gem "rake", "< 11.0"
+gem "rake", "~> 11.1"
gem 'elasticsearch'
gem 'elasticsearch-extensions'
diff --git a/elasticsearch-model/elasticsearch-model.gemspec b/elasticsearch-model/elasticsearch-model.gemspec
index df9509f06..cba0808ea 100644
--- a/elasticsearch-model/elasticsearch-model.gemspec
+++ b/elasticsearch-model/elasticsearch-model.gemspec
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
s.add_dependency "hashie"
s.add_development_dependency "bundler", "~> 1.3"
- s.add_development_dependency "rake", "< 11.0"
+ s.add_development_dependency "rake", "~> 11.1"
s.add_development_dependency "elasticsearch-extensions"
diff --git a/elasticsearch-persistence/elasticsearch-persistence.gemspec b/elasticsearch-persistence/elasticsearch-persistence.gemspec
index d71bfcf93..2b7a7cbea 100644
--- a/elasticsearch-persistence/elasticsearch-persistence.gemspec
+++ b/elasticsearch-persistence/elasticsearch-persistence.gemspec
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
s.add_dependency "virtus"
s.add_development_dependency "bundler", "~> 1.5"
- s.add_development_dependency "rake", "< 11.0"
+ s.add_development_dependency "rake", "~> 11.1"
s.add_development_dependency "oj"
diff --git a/elasticsearch-rails/elasticsearch-rails.gemspec b/elasticsearch-rails/elasticsearch-rails.gemspec
index 14dee1ddf..f33c9f6b7 100644
--- a/elasticsearch-rails/elasticsearch-rails.gemspec
+++ b/elasticsearch-rails/elasticsearch-rails.gemspec
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 1.9.3"
s.add_development_dependency "bundler", "~> 1.3"
- s.add_development_dependency "rake", "< 11.0"
+ s.add_development_dependency "rake", "~> 11.1"
s.add_development_dependency "elasticsearch-extensions"
s.add_development_dependency "elasticsearch-model"
From 6d10abbdcbd7d06405e35ffbd32b21f28727d77b Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Mon, 23 May 2016 17:46:48 +0200
Subject: [PATCH 099/412] [MODEL] Fixed a problem where `Hashie::Mash#min` and
`#max` returned unexpected values
While people can define names for aggregations such as `min` and `max`, these
methods are defined in `Enumerable#min` and `Enumerable#max`, and return
an unexpected value:
result.aggregations
# => {"price"=>{"doc_count"=>9428, "min"=>{"value"=>1.0}, "max"=>{"value"=>260000.0}}}
result.aggregations.price
# => {"doc_count"=>9428, "min"=>{"value"=>1.0}, "max"=>{"value"=>260000.0}}
result.aggregations.price.min
# => ["doc_count", 9428]
Therefore, the whole Hash with aggregations is being traversed, in a wrapper class,
and a `min` and `max` method is defined for Hashie::Mash instances,
because these names are too common.
The wrapper class can have its uses further down the line, so a dedicated
unit test file has been added as well.
Closes #568
Related: elastic/elasticsearch-ruby#306
---
.../lib/elasticsearch/model.rb | 1 +
.../lib/elasticsearch/model/response.rb | 2 +-
.../model/response/aggregations.rb | 36 +++++++++++++++
.../integration/active_record_basic_test.rb | 20 +++++---
.../test/unit/response_aggregations_test.rb | 46 +++++++++++++++++++
5 files changed, 98 insertions(+), 7 deletions(-)
create mode 100644 elasticsearch-model/lib/elasticsearch/model/response/aggregations.rb
create mode 100644 elasticsearch-model/test/unit/response_aggregations_test.rb
diff --git a/elasticsearch-model/lib/elasticsearch/model.rb b/elasticsearch-model/lib/elasticsearch/model.rb
index 9d2b93da5..d66b28e51 100644
--- a/elasticsearch-model/lib/elasticsearch/model.rb
+++ b/elasticsearch-model/lib/elasticsearch/model.rb
@@ -31,6 +31,7 @@
require 'elasticsearch/model/response/results'
require 'elasticsearch/model/response/records'
require 'elasticsearch/model/response/pagination'
+require 'elasticsearch/model/response/aggregations'
require 'elasticsearch/model/response/suggestions'
require 'elasticsearch/model/ext/active_record'
diff --git a/elasticsearch-model/lib/elasticsearch/model/response.rb b/elasticsearch-model/lib/elasticsearch/model/response.rb
index fad3828b3..fba25aaea 100644
--- a/elasticsearch-model/lib/elasticsearch/model/response.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/response.rb
@@ -69,7 +69,7 @@ def shards
# Returns a Hashie::Mash of the aggregations
#
def aggregations
- response['aggregations'] ? Hashie::Mash.new(response['aggregations']) : nil
+ Aggregations.new(response['aggregations'])
end
# Returns a Hashie::Mash of the suggestions
diff --git a/elasticsearch-model/lib/elasticsearch/model/response/aggregations.rb b/elasticsearch-model/lib/elasticsearch/model/response/aggregations.rb
new file mode 100644
index 000000000..06d46d1e3
--- /dev/null
+++ b/elasticsearch-model/lib/elasticsearch/model/response/aggregations.rb
@@ -0,0 +1,36 @@
+module Elasticsearch
+ module Model
+ module Response
+
+ class Aggregations < Hashie::Mash
+ def initialize(attributes={})
+ __redefine_enumerable_methods super(attributes)
+ end
+
+ # Fix the problem of Hashie::Mash returning unexpected values for `min` and `max` methods
+ #
+ # People can define names for aggregations such as `min` and `max`, but these
+ # methods are defined in `Enumerable#min` and `Enumerable#max`
+ #
+ # { foo: 'bar' }.min
+ # # => [:foo, "bar"]
+ #
+ # Therefore, any Hashie::Mash instance value has the `min` and `max`
+ # methods redefined to return the real value
+ #
+ def __redefine_enumerable_methods(h)
+ if h.respond_to?(:each_pair)
+ h.each_pair { |k, v| v = __redefine_enumerable_methods(v) }
+ end
+ if h.is_a?(Hashie::Mash)
+ class << h
+ define_method(:min) { self[:min] }
+ define_method(:max) { self[:max] }
+ end
+ end
+ end
+ end
+
+ end
+ end
+end
diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb
index e6ca97d6d..3636f98f4 100644
--- a/elasticsearch-model/test/integration/active_record_basic_test.rb
+++ b/elasticsearch-model/test/integration/active_record_basic_test.rb
@@ -12,6 +12,7 @@ class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCas
create_table :articles do |t|
t.string :title
t.string :body
+ t.integer :clicks, :default => 0
t.datetime :created_at, :default => 'NOW()'
end
end
@@ -24,6 +25,7 @@ class ::Article < ActiveRecord::Base
mapping do
indexes :title, type: 'string', analyzer: 'snowball'
indexes :body, type: 'string'
+ indexes :clicks, type: 'integer'
indexes :created_at, type: 'date'
end
end
@@ -31,7 +33,7 @@ class ::Article < ActiveRecord::Base
def as_indexed_json(options = {})
attributes
.symbolize_keys
- .slice(:title, :body, :created_at)
+ .slice(:title, :body, :clicks, :created_at)
.merge(suggest_title: title)
end
end
@@ -39,9 +41,9 @@ def as_indexed_json(options = {})
Article.delete_all
Article.__elasticsearch__.create_index! force: true
- ::Article.create! title: 'Test', body: ''
- ::Article.create! title: 'Testing Coding', body: ''
- ::Article.create! title: 'Coding', body: ''
+ ::Article.create! title: 'Test', body: '', clicks: 1
+ ::Article.create! title: 'Testing Coding', body: '', clicks: 2
+ ::Article.create! title: 'Coding', body: '', clicks: 3
Article.__elasticsearch__.refresh_index!
end
@@ -217,11 +219,17 @@ def as_indexed_json(options = {})
should "allow dot access to response" do
response = Article.search query: { match: { title: { query: 'test' } } },
- aggregations: { dates: { date_histogram: { field: 'created_at', interval: 'hour' } } },
+ aggregations: {
+ dates: { date_histogram: { field: 'created_at', interval: 'hour' } },
+ clicks: { global: {}, aggregations: { min: { min: { field: 'clicks' } } } }
+ },
suggest: { text: 'tezt', title: { term: { field: 'title', suggest_mode: 'always' } } }
response.response.respond_to?(:aggregations)
- assert_equal 2, response.aggregations.dates.buckets.first.doc_count
+ assert_equal 2, response.aggregations.dates.buckets.first.doc_count
+ assert_equal 3, response.aggregations.clicks.doc_count
+ assert_equal 1.0, response.aggregations.clicks.min.value
+ assert_nil response.aggregations.clicks.max
response.response.respond_to?(:suggest)
assert_equal 1, response.suggestions.title.first.options.size
diff --git a/elasticsearch-model/test/unit/response_aggregations_test.rb b/elasticsearch-model/test/unit/response_aggregations_test.rb
new file mode 100644
index 000000000..cac17759d
--- /dev/null
+++ b/elasticsearch-model/test/unit/response_aggregations_test.rb
@@ -0,0 +1,46 @@
+require 'test_helper'
+
+class Elasticsearch::Model::ResponseAggregationsTest < Test::Unit::TestCase
+ context "Response aggregations" do
+ class OriginClass
+ def self.index_name; 'foo'; end
+ def self.document_type; 'bar'; end
+ end
+
+ RESPONSE = {
+ 'aggregations' => {
+ 'foo' => {'bar' => 10 },
+ 'price' => { 'doc_count' => 123,
+ 'min' => { 'value' => 1.0},
+ 'max' => { 'value' => 99 }
+ }
+ }
+ }
+
+ setup do
+ @search = Elasticsearch::Model::Searching::SearchRequest.new OriginClass, '*'
+ @search.stubs(:execute!).returns(RESPONSE)
+ end
+
+ should "access the aggregations" do
+ @search.expects(:execute!).returns(RESPONSE)
+
+ response = Elasticsearch::Model::Response::Response.new OriginClass, @search
+ assert_respond_to response, :aggregations
+ assert_kind_of Elasticsearch::Model::Response::Aggregations, response.aggregations
+ assert_kind_of Hashie::Mash, response.aggregations.foo
+ assert_equal 10, response.aggregations.foo.bar
+ end
+
+ should "properly return min and max values" do
+ @search.expects(:execute!).returns(RESPONSE)
+
+ response = Elasticsearch::Model::Response::Response.new OriginClass, @search
+
+ assert_equal 123, response.aggregations.price.doc_count
+ assert_equal 1, response.aggregations.price.min.value
+ assert_equal 99, response.aggregations.price.max.value
+ end
+
+ end
+end
From ceef80eafce6f3cf5513496c2ac7bca6cb5b45f4 Mon Sep 17 00:00:00 2001
From: bogdanvlviv
Date: Sun, 29 May 2016 14:04:45 +0300
Subject: [PATCH 100/412] [MODEL] Added information about `elasticsearch-dsl`
to the README
Closes #574
[ci skip]
---
elasticsearch-model/README.md | 23 +++++++++++++++++++++--
1 file changed, 21 insertions(+), 2 deletions(-)
diff --git a/elasticsearch-model/README.md b/elasticsearch-model/README.md
index c219a4600..28b8d0a72 100644
--- a/elasticsearch-model/README.md
+++ b/elasticsearch-model/README.md
@@ -320,8 +320,8 @@ response.results.first.highlight.title
# ["Quick brown fox"]
```
-You can pass any object which implements a `to_hash` method, or you can use your favourite JSON builder
-to build the search definition as a JSON string:
+You can pass any object which implements a `to_hash` method, which is called automatically,
+so you can use a custom class or your favourite JSON builder to build the search definition:
```ruby
require 'jbuilder'
@@ -341,6 +341,25 @@ response.results.first.title
# => "Quick brown fox"
```
+Also, you can use the [**`elasticsearch-dsl`**](https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl) library, which provides a specialized Ruby API for
+the Elasticsearch Query DSL:
+
+```ruby
+require 'elasticsearch/dsl'
+
+query = Elasticsearch::DSL::Search.search do
+ query do
+ match :title do
+ query 'fox dogs'
+ end
+ end
+end
+
+response = Article.search query
+response.results.first.title
+# => "Quick brown fox"
+```
+
### Index Configuration
For proper search engine function, it's often necessary to configure the index properly.
From b8455db186664e21927bfb271bab6390853e7ff3 Mon Sep 17 00:00:00 2001
From: Ryan Mohr
Date: Tue, 17 Feb 2015 10:10:37 -1000
Subject: [PATCH 101/412] [MODEL] Added support for inherited index names and
doc types
This patch adds support for inheriting index_name and document_type on an opt-in basis:
Elasticsearch::Model.inheritance_enabled = true
class Animal < ActiveRecord::Base
document_type 'mammal'
index_name 'mammals'
end
class Dog < Animal
end
Animal.document_type # 'mammal'
Animal.index_name # 'mammals'
Dog.document_type # 'mammal'
Dog.index_name # 'mammals'
Closes #332
Related: #28, #92, #170, #344
---
.../lib/elasticsearch/model.rb | 19 ++++-
.../lib/elasticsearch/model/naming.rb | 28 ++++++-
.../test/unit/naming_inheritance_test.rb | 78 +++++++++++++++++++
3 files changed, 122 insertions(+), 3 deletions(-)
create mode 100644 elasticsearch-model/test/unit/naming_inheritance_test.rb
diff --git a/elasticsearch-model/lib/elasticsearch/model.rb b/elasticsearch-model/lib/elasticsearch/model.rb
index d66b28e51..8406026e7 100644
--- a/elasticsearch-model/lib/elasticsearch/model.rb
+++ b/elasticsearch-model/lib/elasticsearch/model.rb
@@ -131,7 +131,6 @@ class << self
end
module ClassMethods
-
# Get the client common for all models
#
# @example Get the client
@@ -181,6 +180,24 @@ def search(query_or_payload, models=[], options={})
request = Searching::SearchRequest.new(models, query_or_payload, options)
Response::Response.new(models, request)
end
+
+ # Check if inheritance is enabled
+ #
+ # @note Inheritance is disabled by default.
+ #
+ def inheritance_enabled
+ @inheritance_enabled ||= false
+ end
+
+ # Enable inheritance of index_name and document_type
+ #
+ # @example Enable inheritance
+ #
+ # Elasticsearch::Model.inheritance_enabled = true
+ #
+ def inheritance_enabled=(inheritance_enabled)
+ @inheritance_enabled = inheritance_enabled
+ end
end
extend ClassMethods
diff --git a/elasticsearch-model/lib/elasticsearch/model/naming.rb b/elasticsearch-model/lib/elasticsearch/model/naming.rb
index ce510d2d4..2de9f8e60 100644
--- a/elasticsearch-model/lib/elasticsearch/model/naming.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/naming.rb
@@ -34,7 +34,7 @@ def index_name name=nil, &block
if @index_name.respond_to?(:call)
@index_name.call
else
- @index_name || self.model_name.collection.gsub(/\//, '-')
+ @index_name || implicit(:index_name)
end
end
@@ -58,7 +58,7 @@ def index_name=(name)
# Article.document_type "my-article"
#
def document_type name=nil
- @document_type = name || @document_type || self.model_name.element
+ @document_type = name || @document_type || implicit(:document_type)
end
@@ -69,6 +69,30 @@ def document_type name=nil
def document_type=(name)
@document_type = name
end
+
+ private
+
+ def implicit(prop)
+ value = nil
+
+ if Elasticsearch::Model.inheritance_enabled
+ self.ancestors.each do |klass|
+ next if klass == self
+ break if value = klass.respond_to?(prop) && klass.send(prop)
+ end
+ end
+
+ value || self.send("default_#{prop}")
+ end
+
+ def default_index_name
+ self.model_name.collection.gsub(/\//, '-')
+ end
+
+ def default_document_type
+ self.model_name.element
+ end
+
end
module InstanceMethods
diff --git a/elasticsearch-model/test/unit/naming_inheritance_test.rb b/elasticsearch-model/test/unit/naming_inheritance_test.rb
new file mode 100644
index 000000000..6faf136a3
--- /dev/null
+++ b/elasticsearch-model/test/unit/naming_inheritance_test.rb
@@ -0,0 +1,78 @@
+require "test_helper"
+
+class Elasticsearch::Model::NamingInheritanceTest < Test::Unit::TestCase
+ def setup
+ Elasticsearch::Model.inheritance_enabled = true
+ end
+
+ def teardown
+ Elasticsearch::Model.inheritance_enabled = false
+ end
+
+ context "Naming module with inheritance" do
+ class ::TestBase
+ extend ActiveModel::Naming
+
+ extend Elasticsearch::Model::Naming::ClassMethods
+ include Elasticsearch::Model::Naming::InstanceMethods
+ end
+
+ class ::Animal < ::TestBase
+ extend ActiveModel::Naming
+
+ extend Elasticsearch::Model::Naming::ClassMethods
+ include Elasticsearch::Model::Naming::InstanceMethods
+
+ index_name "mammals"
+ document_type "mammal"
+ end
+
+ class ::Dog < ::Animal
+ end
+
+ module ::MyNamespace
+ class Dog < ::Animal
+ end
+ end
+
+ should "return the default index_name" do
+ assert_equal "test_bases", TestBase.index_name
+ assert_equal "test_bases", TestBase.new.index_name
+ end
+
+ should "return the explicit index_name" do
+ assert_equal "mammals", Animal.index_name
+ assert_equal "mammals", Animal.new.index_name
+ end
+
+ should "return the ancestor index_name" do
+ assert_equal "mammals", Dog.index_name
+ assert_equal "mammals", Dog.new.index_name
+ end
+
+ should "return the ancestor index_name for namespaced model" do
+ assert_equal "mammals", ::MyNamespace::Dog.index_name
+ assert_equal "mammals", ::MyNamespace::Dog.new.index_name
+ end
+
+ should "return the default document_type" do
+ assert_equal "test_base", TestBase.document_type
+ assert_equal "test_base", TestBase.new.document_type
+ end
+
+ should "return the explicit document_type" do
+ assert_equal "mammal", Animal.document_type
+ assert_equal "mammal", Animal.new.document_type
+ end
+
+ should "return the ancestor document_type" do
+ assert_equal "mammal", Dog.document_type
+ assert_equal "mammal", Dog.new.document_type
+ end
+
+ should "return the ancestor document_type for namespaced model" do
+ assert_equal "mammal", ::MyNamespace::Dog.document_type
+ assert_equal "mammal", ::MyNamespace::Dog.new.document_type
+ end
+ end
+end
From 2a08d09e0eba9f4322ec3e4e2560cc0aaa568b12 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sun, 26 Jun 2016 17:17:56 +0200
Subject: [PATCH 102/412] [MODEL] Added a `Elasticsearch::Model.settings`
method
---
elasticsearch-model/lib/elasticsearch/model.rb | 6 ++++++
elasticsearch-model/test/unit/module_test.rb | 11 +++++++++++
2 files changed, 17 insertions(+)
diff --git a/elasticsearch-model/lib/elasticsearch/model.rb b/elasticsearch-model/lib/elasticsearch/model.rb
index 8406026e7..aa3bf9c5f 100644
--- a/elasticsearch-model/lib/elasticsearch/model.rb
+++ b/elasticsearch-model/lib/elasticsearch/model.rb
@@ -130,6 +130,12 @@ class << self
end
end
+ # Access the module settings
+ #
+ def self.settings
+ @settings ||= {}
+ end
+
module ClassMethods
# Get the client common for all models
#
diff --git a/elasticsearch-model/test/unit/module_test.rb b/elasticsearch-model/test/unit/module_test.rb
index a429b3d11..fb8e0ba61 100644
--- a/elasticsearch-model/test/unit/module_test.rb
+++ b/elasticsearch-model/test/unit/module_test.rb
@@ -53,5 +53,16 @@ def self.search(query, options={})
end
end
+ context "settings" do
+ should "access the settings" do
+ assert_not_nil Elasticsearch::Model.settings
+ end
+
+ should "allow to set settings" do
+ assert_nothing_raised { Elasticsearch::Model.settings[:foo] = 'bar' }
+ assert_equal 'bar', Elasticsearch::Model.settings[:foo]
+ end
+ end
+
end
end
From 5251a5201f0b3fa1278e4366e364f9aaded81be6 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sun, 26 Jun 2016 17:18:15 +0200
Subject: [PATCH 103/412] [MODEL] Changed the naming inheritance logic to use
`Elasticsearch::Model.settings`
This patch removes the `Elasticsearch::Model.inheritance_enabled` method added
in b8455db, and changes the logic to use the `settings` for the module.
Related: #332
---
.../lib/elasticsearch/model/naming.rb | 2 +-
.../test/unit/naming_inheritance_test.rb | 20 +++++++++++++++++--
2 files changed, 19 insertions(+), 3 deletions(-)
diff --git a/elasticsearch-model/lib/elasticsearch/model/naming.rb b/elasticsearch-model/lib/elasticsearch/model/naming.rb
index 2de9f8e60..7bf24f089 100644
--- a/elasticsearch-model/lib/elasticsearch/model/naming.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/naming.rb
@@ -75,7 +75,7 @@ def document_type=(name)
def implicit(prop)
value = nil
- if Elasticsearch::Model.inheritance_enabled
+ if Elasticsearch::Model.settings[:inheritance_enabled]
self.ancestors.each do |klass|
next if klass == self
break if value = klass.respond_to?(prop) && klass.send(prop)
diff --git a/elasticsearch-model/test/unit/naming_inheritance_test.rb b/elasticsearch-model/test/unit/naming_inheritance_test.rb
index 6faf136a3..b66d415a0 100644
--- a/elasticsearch-model/test/unit/naming_inheritance_test.rb
+++ b/elasticsearch-model/test/unit/naming_inheritance_test.rb
@@ -2,11 +2,11 @@
class Elasticsearch::Model::NamingInheritanceTest < Test::Unit::TestCase
def setup
- Elasticsearch::Model.inheritance_enabled = true
+ Elasticsearch::Model.settings[:inheritance_enabled] = true
end
def teardown
- Elasticsearch::Model.inheritance_enabled = false
+ Elasticsearch::Model.settings[:inheritance_enabled] = false
end
context "Naming module with inheritance" do
@@ -35,6 +35,16 @@ class Dog < ::Animal
end
end
+ class ::Cat < ::Animal
+ extend ActiveModel::Naming
+
+ extend Elasticsearch::Model::Naming::ClassMethods
+ include Elasticsearch::Model::Naming::InstanceMethods
+
+ index_name "cats"
+ document_type "cat"
+ end
+
should "return the default index_name" do
assert_equal "test_bases", TestBase.index_name
assert_equal "test_bases", TestBase.new.index_name
@@ -43,6 +53,9 @@ class Dog < ::Animal
should "return the explicit index_name" do
assert_equal "mammals", Animal.index_name
assert_equal "mammals", Animal.new.index_name
+
+ assert_equal "cats", Cat.index_name
+ assert_equal "cats", Cat.new.index_name
end
should "return the ancestor index_name" do
@@ -63,6 +76,9 @@ class Dog < ::Animal
should "return the explicit document_type" do
assert_equal "mammal", Animal.document_type
assert_equal "mammal", Animal.new.document_type
+
+ assert_equal "cat", Cat.document_type
+ assert_equal "cat", Cat.new.document_type
end
should "return the ancestor document_type" do
From 15761247f3e99654bda946a178e50b5365414b59 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 28 Jun 2016 14:17:44 +0200
Subject: [PATCH 104/412] [MODEL] Added information about the `settings` method
and the `inheritance_enabled` setting into the README
---
elasticsearch-model/README.md | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/elasticsearch-model/README.md b/elasticsearch-model/README.md
index 28b8d0a72..b631b2a9b 100644
--- a/elasticsearch-model/README.md
+++ b/elasticsearch-model/README.md
@@ -704,6 +704,18 @@ response.records.records.class
More examples can be found in the `examples` folder. Please see the `Elasticsearch::Model::Adapter`
module and its submodules for technical information.
+### Settings
+
+The module provides a common `settings` method to customize various features.
+
+At the moment, the only supported setting is `:inheritance_enabled`, which makes the class receiving the module
+respect index names and document types of a super-class, eg. in case you're using "single table inheritance" (STI)
+in Rails:
+
+```ruby
+Elasticsearch::Model.settings[:inheritance_enabled] = true
+```
+
## Development and Community
For local development, clone the repository and run `bundle install`. See `rake -T` for a list of
From 94d0b28f08dfae63c0cfa47d510b563218e0e856 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 13 Dec 2016 09:31:41 +0100
Subject: [PATCH 105/412] [MODEL] Disable "verbose" and "warnings" in
integration tests
---
elasticsearch-model/Rakefile | 2 ++
1 file changed, 2 insertions(+)
diff --git a/elasticsearch-model/Rakefile b/elasticsearch-model/Rakefile
index 2825f58b0..93027ccd7 100644
--- a/elasticsearch-model/Rakefile
+++ b/elasticsearch-model/Rakefile
@@ -26,6 +26,8 @@ namespace :test do
Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
+ test.verbose = false
+ test.warning = false
end
desc "Run integration tests against ActiveModel 3 and 4"
From 66bb01273a53fe632898ef7cc6b8117d1047cd0a Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 13 Dec 2016 10:09:24 +0100
Subject: [PATCH 106/412] [MODEL] Added code for establishing ActiveRecord
connections to test classes
With ActiveRecord 3.x, the connection setup in the main `setup` method doesn't
seem to be called properly.
---
.../integration/active_record_associations_parent_child.rb | 3 +++
.../test/integration/active_record_associations_test.rb | 3 +++
.../test/integration/active_record_basic_test.rb | 3 +++
.../integration/active_record_custom_serialization_test.rb | 3 +++
.../test/integration/active_record_import_test.rb | 3 +++
.../test/integration/active_record_namespaced_model_test.rb | 3 +++
.../test/integration/active_record_pagination_test.rb | 3 +++
.../test/integration/dynamic_index_name_test.rb | 3 +++
elasticsearch-model/test/integration/multiple_models_test.rb | 3 +++
9 files changed, 27 insertions(+)
diff --git a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
index 39be1144b..5e9b49f9e 100644
--- a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
class Question < ActiveRecord::Base
include Elasticsearch::Model
diff --git a/elasticsearch-model/test/integration/active_record_associations_test.rb b/elasticsearch-model/test/integration/active_record_associations_test.rb
index af67ad889..8177c193e 100644
--- a/elasticsearch-model/test/integration/active_record_associations_test.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
module Elasticsearch
module Model
class ActiveRecordAssociationsIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb
index 3636f98f4..4800abc4a 100644
--- a/elasticsearch-model/test/integration/active_record_basic_test.rb
+++ b/elasticsearch-model/test/integration/active_record_basic_test.rb
@@ -3,6 +3,9 @@
puts "ActiveRecord #{ActiveRecord::VERSION::STRING}", '-'*80
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
module Elasticsearch
module Model
class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb b/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb
index 03eb9a441..60b1f6d32 100644
--- a/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb
+++ b/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
module Elasticsearch
module Model
class ActiveRecordCustomSerializationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_import_test.rb b/elasticsearch-model/test/integration/active_record_import_test.rb
index 8cc7448cc..49c0e5685 100644
--- a/elasticsearch-model/test/integration/active_record_import_test.rb
+++ b/elasticsearch-model/test/integration/active_record_import_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
module Elasticsearch
module Model
class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb b/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb
index be047f4e7..d1eb11f27 100644
--- a/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb
+++ b/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
module Elasticsearch
module Model
class ActiveRecordNamespacedModelIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_pagination_test.rb b/elasticsearch-model/test/integration/active_record_pagination_test.rb
index e1d6fefb1..9a043a1e8 100644
--- a/elasticsearch-model/test/integration/active_record_pagination_test.rb
+++ b/elasticsearch-model/test/integration/active_record_pagination_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
module Elasticsearch
module Model
class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/dynamic_index_name_test.rb b/elasticsearch-model/test/integration/dynamic_index_name_test.rb
index a71633c6a..afecc26c0 100755
--- a/elasticsearch-model/test/integration/dynamic_index_name_test.rb
+++ b/elasticsearch-model/test/integration/dynamic_index_name_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
module Elasticsearch
module Model
class DynamicIndexNameTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/multiple_models_test.rb b/elasticsearch-model/test/integration/multiple_models_test.rb
index 7d0bf7b6b..55ccd0175 100644
--- a/elasticsearch-model/test/integration/multiple_models_test.rb
+++ b/elasticsearch-model/test/integration/multiple_models_test.rb
@@ -1,6 +1,9 @@
require 'test_helper'
require 'active_record'
+# Needed for ActiveRecord 3.x ?
+ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+
Mongo.setup!
module Elasticsearch
From fa2b52556cd4a363e97b3010620724bcbb66e452 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 13 Dec 2016 10:11:26 +0100
Subject: [PATCH 107/412] [MODEL] Reorganized the class definitions in the
integration tests
With ActiveRecord 4 and 5, the class definitions within Shoulda's `setup`
method don't work correctly.
---
.../active_record_associations_test.rb | 108 +++++++++---------
.../integration/active_record_basic_test.rb | 43 +++----
.../integration/active_record_import_test.rb | 27 ++---
.../active_record_pagination_test.rb | 22 ++--
.../test/integration/multiple_models_test.rb | 49 ++++----
5 files changed, 126 insertions(+), 123 deletions(-)
diff --git a/elasticsearch-model/test/integration/active_record_associations_test.rb b/elasticsearch-model/test/integration/active_record_associations_test.rb
index 8177c193e..ca0361d89 100644
--- a/elasticsearch-model/test/integration/active_record_associations_test.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_test.rb
@@ -8,6 +8,58 @@ module Elasticsearch
module Model
class ActiveRecordAssociationsIntegrationTest < Elasticsearch::Test::IntegrationTestCase
+ # ----- Search integration via Concern module -----------------------------------------------------
+
+ module Searchable
+ extend ActiveSupport::Concern
+
+ included do
+ include Elasticsearch::Model
+ include Elasticsearch::Model::Callbacks
+
+ # Set up the mapping
+ #
+ settings index: { number_of_shards: 1, number_of_replicas: 0 } do
+ mapping do
+ indexes :title, analyzer: 'snowball'
+ indexes :created_at, type: 'date'
+
+ indexes :authors do
+ indexes :first_name
+ indexes :last_name
+ indexes :full_name, type: 'multi_field' do
+ indexes :full_name
+ indexes :raw, analyzer: 'keyword'
+ end
+ end
+
+ indexes :categories, analyzer: 'keyword'
+
+ indexes :comments, type: 'nested' do
+ indexes :text
+ indexes :author
+ end
+ end
+ end
+
+ # Customize the JSON serialization for Elasticsearch
+ #
+ def as_indexed_json(options={})
+ {
+ title: title,
+ text: text,
+ categories: categories.map(&:title),
+ authors: authors.as_json(methods: [:full_name], only: [:full_name, :first_name, :last_name]),
+ comments: comments.as_json(only: [:text, :author])
+ }
+ end
+
+ # Update document in the index after touch
+ #
+ after_touch() { __elasticsearch__.index_document }
+ end
+ end
+
context "ActiveRecord associations" do
setup do
@@ -40,7 +92,9 @@ class ActiveRecordAssociationsIntegrationTest < Elasticsearch::Test::Integration
t.string :author
t.references :post
t.timestamps
- end and add_index(:comments, :post_id)
+ end
+
+ add_index(:comments, :post_id) unless index_exists?(:comments, :post_id)
create_table :posts do |t|
t.string :title
@@ -81,58 +135,6 @@ class Post < ActiveRecord::Base
has_many :comments
end
- # ----- Search integration via Concern module -----------------------------------------------------
-
- module Searchable
- extend ActiveSupport::Concern
-
- included do
- include Elasticsearch::Model
- include Elasticsearch::Model::Callbacks
-
- # Set up the mapping
- #
- settings index: { number_of_shards: 1, number_of_replicas: 0 } do
- mapping do
- indexes :title, analyzer: 'snowball'
- indexes :created_at, type: 'date'
-
- indexes :authors do
- indexes :first_name
- indexes :last_name
- indexes :full_name, type: 'multi_field' do
- indexes :full_name
- indexes :raw, analyzer: 'keyword'
- end
- end
-
- indexes :categories, analyzer: 'keyword'
-
- indexes :comments, type: 'nested' do
- indexes :text
- indexes :author
- end
- end
- end
-
- # Customize the JSON serialization for Elasticsearch
- #
- def as_indexed_json(options={})
- {
- title: title,
- text: text,
- categories: categories.map(&:title),
- authors: authors.as_json(methods: [:full_name], only: [:full_name, :first_name, :last_name]),
- comments: comments.as_json(only: [:text, :author])
- }
- end
-
- # Update document in the index after touch
- #
- after_touch() { __elasticsearch__.index_document }
- end
- end
-
# Include the search integration
#
Post.__send__ :include, Searchable
diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb
index 4800abc4a..0f2acbd0b 100644
--- a/elasticsearch-model/test/integration/active_record_basic_test.rb
+++ b/elasticsearch-model/test/integration/active_record_basic_test.rb
@@ -9,6 +9,28 @@
module Elasticsearch
module Model
class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
+
+ class ::Article < ActiveRecord::Base
+ include Elasticsearch::Model
+ include Elasticsearch::Model::Callbacks
+
+ settings index: { number_of_shards: 1, number_of_replicas: 0 } do
+ mapping do
+ indexes :title, type: 'string', analyzer: 'snowball'
+ indexes :body, type: 'string'
+ indexes :clicks, type: 'integer'
+ indexes :created_at, type: 'date'
+ end
+ end
+
+ def as_indexed_json(options = {})
+ attributes
+ .symbolize_keys
+ .slice(:title, :body, :clicks, :created_at)
+ .merge(suggest_title: title)
+ end
+ end
+
context "ActiveRecord basic integration" do
setup do
ActiveRecord::Schema.define(:version => 1) do
@@ -20,27 +42,6 @@ class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCas
end
end
- class ::Article < ActiveRecord::Base
- include Elasticsearch::Model
- include Elasticsearch::Model::Callbacks
-
- settings index: { number_of_shards: 1, number_of_replicas: 0 } do
- mapping do
- indexes :title, type: 'string', analyzer: 'snowball'
- indexes :body, type: 'string'
- indexes :clicks, type: 'integer'
- indexes :created_at, type: 'date'
- end
- end
-
- def as_indexed_json(options = {})
- attributes
- .symbolize_keys
- .slice(:title, :body, :clicks, :created_at)
- .merge(suggest_title: title)
- end
- end
-
Article.delete_all
Article.__elasticsearch__.create_index! force: true
diff --git a/elasticsearch-model/test/integration/active_record_import_test.rb b/elasticsearch-model/test/integration/active_record_import_test.rb
index 49c0e5685..31febcb2c 100644
--- a/elasticsearch-model/test/integration/active_record_import_test.rb
+++ b/elasticsearch-model/test/integration/active_record_import_test.rb
@@ -7,6 +7,20 @@
module Elasticsearch
module Model
class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase
+
+ class ::ImportArticle < ActiveRecord::Base
+ include Elasticsearch::Model
+
+ scope :popular, -> { where('views >= 50') }
+
+ mapping do
+ indexes :title, type: 'string'
+ indexes :views, type: 'integer'
+ indexes :numeric, type: 'integer'
+ indexes :created_at, type: 'date'
+ end
+ end
+
context "ActiveRecord importing" do
setup do
ActiveRecord::Schema.define(:version => 1) do
@@ -18,19 +32,6 @@ class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCa
end
end
- class ::ImportArticle < ActiveRecord::Base
- include Elasticsearch::Model
-
- scope :popular, -> { where('views >= 50') }
-
- mapping do
- indexes :title, type: 'string'
- indexes :views, type: 'integer'
- indexes :numeric, type: 'integer'
- indexes :created_at, type: 'date'
- end
- end
-
ImportArticle.delete_all
ImportArticle.__elasticsearch__.create_index! force: true
ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
diff --git a/elasticsearch-model/test/integration/active_record_pagination_test.rb b/elasticsearch-model/test/integration/active_record_pagination_test.rb
index 9a043a1e8..78892b73c 100644
--- a/elasticsearch-model/test/integration/active_record_pagination_test.rb
+++ b/elasticsearch-model/test/integration/active_record_pagination_test.rb
@@ -7,21 +7,21 @@
module Elasticsearch
module Model
class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase
- context "ActiveRecord pagination" do
- setup do
- class ::ArticleForPagination < ActiveRecord::Base
- include Elasticsearch::Model
+ class ::ArticleForPagination < ActiveRecord::Base
+ include Elasticsearch::Model
- scope :published, -> { where(published: true) }
+ scope :published, -> { where(published: true) }
- settings index: { number_of_shards: 1, number_of_replicas: 0 } do
- mapping do
- indexes :title, type: 'string', analyzer: 'snowball'
- indexes :created_at, type: 'date'
- end
- end
+ settings index: { number_of_shards: 1, number_of_replicas: 0 } do
+ mapping do
+ indexes :title, type: 'string', analyzer: 'snowball'
+ indexes :created_at, type: 'date'
end
+ end
+ end
+ context "ActiveRecord pagination" do
+ setup do
ActiveRecord::Schema.define(:version => 1) do
create_table ::ArticleForPagination.table_name do |t|
t.string :title
diff --git a/elasticsearch-model/test/integration/multiple_models_test.rb b/elasticsearch-model/test/integration/multiple_models_test.rb
index 55ccd0175..957aca852 100644
--- a/elasticsearch-model/test/integration/multiple_models_test.rb
+++ b/elasticsearch-model/test/integration/multiple_models_test.rb
@@ -9,6 +9,30 @@
module Elasticsearch
module Model
class MultipleModelsIntegration < Elasticsearch::Test::IntegrationTestCase
+ module ::NameSearch
+ extend ActiveSupport::Concern
+
+ included do
+ include Elasticsearch::Model
+ include Elasticsearch::Model::Callbacks
+
+ settings index: {number_of_shards: 1, number_of_replicas: 0} do
+ mapping do
+ indexes :name, type: 'string', analyzer: 'snowball'
+ indexes :created_at, type: 'date'
+ end
+ end
+ end
+ end
+
+ class ::Episode < ActiveRecord::Base
+ include NameSearch
+ end
+
+ class ::Series < ActiveRecord::Base
+ include NameSearch
+ end
+
context "Multiple models" do
setup do
ActiveRecord::Schema.define(:version => 1) do
@@ -23,30 +47,6 @@ class MultipleModelsIntegration < Elasticsearch::Test::IntegrationTestCase
end
end
- module ::NameSearch
- extend ActiveSupport::Concern
-
- included do
- include Elasticsearch::Model
- include Elasticsearch::Model::Callbacks
-
- settings index: {number_of_shards: 1, number_of_replicas: 0} do
- mapping do
- indexes :name, type: 'string', analyzer: 'snowball'
- indexes :created_at, type: 'date'
- end
- end
- end
- end
-
- class ::Episode < ActiveRecord::Base
- include NameSearch
- end
-
- class ::Series < ActiveRecord::Base
- include NameSearch
- end
-
[::Episode, ::Series].each do |model|
model.delete_all
model.__elasticsearch__.create_index! force: true
@@ -55,7 +55,6 @@ class ::Series < ActiveRecord::Base
model.create name: "The greatest #{model.name}"
model.__elasticsearch__.refresh_index!
end
-
end
should "find matching documents across multiple models" do
From e64baf326de9b4692262ca79263bc0c1ffbb29fa Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 13 Dec 2016 12:02:16 +0100
Subject: [PATCH 108/412] [MODEL] Moved `require` within unit test to the top
of the file
This is to avoid:
activesupport-4.0.13/lib/active_support/json.rb:2: warning: loading in progress, circular require considered harmful
---
elasticsearch-model/test/unit/response_result_test.rb | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/elasticsearch-model/test/unit/response_result_test.rb b/elasticsearch-model/test/unit/response_result_test.rb
index ff78d2579..c357d46ba 100644
--- a/elasticsearch-model/test/unit/response_result_test.rb
+++ b/elasticsearch-model/test/unit/response_result_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
+require 'active_support/json/encoding'
class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
context "Response result" do
@@ -80,7 +81,6 @@ class Elasticsearch::Model::ResultTest < Test::Unit::TestCase
end
should "delegate as_json to @result even when ActiveSupport changed half of Ruby" do
- require 'active_support/json/encoding'
result = Elasticsearch::Model::Response::Result.new foo: 'bar'
result.instance_variable_get(:@result).expects(:as_json)
From 8f8761a0cd6877f13956ce958be0a19de3210c10 Mon Sep 17 00:00:00 2001
From: Savater Sebastien
Date: Wed, 17 Aug 2016 18:45:27 +0200
Subject: [PATCH 109/412] [MODEL] Added ActiveRecord 5 support to integration
test configuration
---
Rakefile | 3 +++
elasticsearch-model/.gitignore | 1 +
elasticsearch-model/Rakefile | 3 ++-
elasticsearch-model/elasticsearch-model.gemspec | 2 +-
elasticsearch-model/gemfiles/5.0.gemfile | 12 ++++++++++++
5 files changed, 19 insertions(+), 2 deletions(-)
create mode 100644 elasticsearch-model/gemfiles/5.0.gemfile
diff --git a/Rakefile b/Rakefile
index e6c517970..776e3004a 100644
--- a/Rakefile
+++ b/Rakefile
@@ -35,6 +35,8 @@ namespace :bundle do
sh "bundle install --gemfile #{__current__.join('elasticsearch-model/gemfiles')}/3.0.gemfile"
puts '-'*80
sh "bundle install --gemfile #{__current__.join('elasticsearch-model/gemfiles')}/4.0.gemfile"
+ puts '-'*80
+ sh "bundle install --gemfile #{__current__.join('elasticsearch-model/gemfiles')}/5.0.gemfile"
end
desc "Remove Gemfile.lock in all subprojects"
@@ -45,6 +47,7 @@ namespace :bundle do
end
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/3.0.gemfile.lock"
sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/4.0.gemfile.lock"
+ sh "rm -f #{__current__.join('elasticsearch-model/gemfiles')}/5.0.gemfile.lock"
end
end
diff --git a/elasticsearch-model/.gitignore b/elasticsearch-model/.gitignore
index 3934d7e55..8a8ab4613 100644
--- a/elasticsearch-model/.gitignore
+++ b/elasticsearch-model/.gitignore
@@ -18,3 +18,4 @@ tmp
gemfiles/3.0.gemfile.lock
gemfiles/4.0.gemfile.lock
+gemfiles/5.0.gemfile.lock
diff --git a/elasticsearch-model/Rakefile b/elasticsearch-model/Rakefile
index 93027ccd7..031ca8599 100644
--- a/elasticsearch-model/Rakefile
+++ b/elasticsearch-model/Rakefile
@@ -30,10 +30,11 @@ namespace :test do
test.warning = false
end
- desc "Run integration tests against ActiveModel 3 and 4"
+ desc "Run integration tests against ActiveModel 3, 4 and 5"
task :integration do
sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration" unless defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
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
desc "Run unit and integration tests"
diff --git a/elasticsearch-model/elasticsearch-model.gemspec b/elasticsearch-model/elasticsearch-model.gemspec
index cba0808ea..07ecc87b9 100644
--- a/elasticsearch-model/elasticsearch-model.gemspec
+++ b/elasticsearch-model/elasticsearch-model.gemspec
@@ -39,7 +39,7 @@ Gem::Specification.new do |s|
s.add_development_dependency "kaminari"
s.add_development_dependency "will_paginate"
- s.add_development_dependency "minitest", "~> 4.2"
+ s.add_development_dependency "minitest", ">= 4.2"
s.add_development_dependency "test-unit" if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
s.add_development_dependency "shoulda-context"
s.add_development_dependency "mocha"
diff --git a/elasticsearch-model/gemfiles/5.0.gemfile b/elasticsearch-model/gemfiles/5.0.gemfile
new file mode 100644
index 000000000..75b8a7ca9
--- /dev/null
+++ b/elasticsearch-model/gemfiles/5.0.gemfile
@@ -0,0 +1,12 @@
+# Usage:
+#
+# $ BUNDLE_GEMFILE=./gemfiles/5.0.gemfile bundle install
+# $ BUNDLE_GEMFILE=./gemfiles/5.0.gemfile bundle exec rake test:integration
+
+source 'https://rubygems.org'
+
+gemspec path: '../'
+
+gem 'activemodel', '~> 5'
+gem 'activerecord', '~> 5'
+gem 'sqlite3'
From c9c4d2f1e0146b68feec76a54e8b27c569049d61 Mon Sep 17 00:00:00 2001
From: Savater Sebastien
Date: Wed, 17 Aug 2016 21:22:27 +0200
Subject: [PATCH 110/412] [MODEL] Fixed records sorting with ActiveRecord 5.x
Closes #618
---
.../lib/elasticsearch/model/adapters/active_record.rb | 5 ++++-
.../test/integration/active_record_basic_test.rb | 5 ++++-
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
index 2d9bb5378..9d092dd10 100644
--- a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
@@ -26,7 +26,10 @@ def records
# by redefining `to_a`, unless the user has called `order()`
#
sql_records.instance_exec(response.response['hits']['hits']) do |hits|
- define_singleton_method :to_a do
+ ar_records_method_name = :to_a
+ ar_records_method_name = :records if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5
+
+ define_singleton_method(ar_records_method_name) do
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
self.load
else
diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb
index 0f2acbd0b..fe3972301 100644
--- a/elasticsearch-model/test/integration/active_record_basic_test.rb
+++ b/elasticsearch-model/test/integration/active_record_basic_test.rb
@@ -104,7 +104,10 @@ def as_indexed_json(options = {})
end
should "preserve the search results order for records" do
- response = Article.search('title:code')
+ response = Article.search query: { match: { title: 'code' }}, sort: { clicks: :desc }
+
+ assert_equal response.records[0].clicks, 3
+ assert_equal response.records[1].clicks, 2
response.records.each_with_hit do |r, h|
assert_equal h._id, r.id.to_s
From 40f9e7eae2696d44abf4a23ae4a499650f0b29f4 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 13 Dec 2016 12:25:21 +0100
Subject: [PATCH 111/412] [MODEL] Added, that `add_index` for ActiveRecord
models is only called when it doesn't exist already
---
elasticsearch-model/examples/activerecord_associations.rb | 3 ++-
.../integration/active_record_associations_parent_child.rb | 5 ++++-
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/elasticsearch-model/examples/activerecord_associations.rb b/elasticsearch-model/examples/activerecord_associations.rb
index 6143a0356..89b280c26 100644
--- a/elasticsearch-model/examples/activerecord_associations.rb
+++ b/elasticsearch-model/examples/activerecord_associations.rb
@@ -56,7 +56,8 @@
t.references :article
t.timestamps
end
- add_index(:comments, :article_id)
+
+ add_index(:comments, :article_id) unless index_exists?(:comments, :article_id)
end
# ----- Elasticsearch client setup ----------------------------------------------------------------
diff --git a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
index 5e9b49f9e..4d964ac5e 100644
--- a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
@@ -71,12 +71,15 @@ class ActiveRecordAssociationsParentChildIntegrationTest < Elasticsearch::Test::
t.string :author
t.timestamps
end
+
create_table :answers do |t|
t.text :text
t.string :author
t.references :question
t.timestamps
- end and add_index(:answers, :question_id)
+ end
+
+ add_index(:answers, :question_id) unless index_exists?(:answers, :question_id)
end
Question.delete_all
From b6d485748c71a07d064ea2a46a6da82d64a04cd7 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Tue, 13 Dec 2016 12:25:48 +0100
Subject: [PATCH 112/412] [MODEL] Use `records.__send__ :load` instead of
`records.load` in the ActiveRecord adapter
This is to prevent the "private method load called for Array" exceptions in unit tests.
---
.../lib/elasticsearch/model/adapters/active_record.rb | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
index 9d092dd10..23c4267fa 100644
--- a/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
+++ b/elasticsearch-model/lib/elasticsearch/model/adapters/active_record.rb
@@ -36,7 +36,7 @@ def records
self.__send__(:exec_queries)
end
@records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } }
- end
+ end if self
end
sql_records
@@ -45,7 +45,7 @@ def records
# Prevent clash with `ActiveSupport::Dependencies::Loadable`
#
def load
- records.load
+ records.__send__(:load)
end
# Intercept call to the `order` method, so we can ignore the order from Elasticsearch
From 02a8407c8dda5c351832287e65b12d6ea2193d19 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 11:21:51 +0100
Subject: [PATCH 113/412] Reduced verbosity of `rake test:unit` and `rake
test:integration`
---
elasticsearch-persistence/Rakefile | 6 ++++--
elasticsearch-rails/Rakefile | 6 ++++--
2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/elasticsearch-persistence/Rakefile b/elasticsearch-persistence/Rakefile
index 303f43997..f92cfd64e 100644
--- a/elasticsearch-persistence/Rakefile
+++ b/elasticsearch-persistence/Rakefile
@@ -18,14 +18,16 @@ namespace :test do
Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
- # test.verbose = true
- # test.warning = true
+ test.verbose = false
+ test.warning = false
end
Rake::TestTask.new(:integration) do |test|
Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
+ test.verbose = false
+ test.warning = false
end
Rake::TestTask.new(:all) do |test|
diff --git a/elasticsearch-rails/Rakefile b/elasticsearch-rails/Rakefile
index 3cf581a91..dfad126cb 100644
--- a/elasticsearch-rails/Rakefile
+++ b/elasticsearch-rails/Rakefile
@@ -18,14 +18,16 @@ namespace :test do
Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
- # test.verbose = true
- # test.warning = true
+ test.verbose = false
+ test.warning = false
end
Rake::TestTask.new(:integration) do |test|
Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
+ test.verbose = false
+ test.warning = false
end
Rake::TestTask.new(:all) do |test|
From be5b44d80c97e1460a05ba40898a9ebfe6db4dc2 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 27 Jan 2017 19:26:50 +0100
Subject: [PATCH 114/412] [MODEL] Call `Kaminari::Hooks.init` only when
available
See: kaminari/kaminari@653143b
Originally reported by @twe4ked in #660.
Closes #660
---
elasticsearch-model/README.md | 2 +-
elasticsearch-model/examples/activerecord_article.rb | 2 +-
.../test/integration/active_record_pagination_test.rb | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/elasticsearch-model/README.md b/elasticsearch-model/README.md
index b631b2a9b..8f963018d 100644
--- a/elasticsearch-model/README.md
+++ b/elasticsearch-model/README.md
@@ -303,7 +303,7 @@ In a Rails controller, use the the `params[:page]` parameter to paginate through
To initialize and include the Kaminari pagination support manually:
```ruby
-Kaminari::Hooks.init
+Kaminari::Hooks.init if defined?(Kaminari::Hooks)
Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model::Response::Pagination::Kaminari
```
diff --git a/elasticsearch-model/examples/activerecord_article.rb b/elasticsearch-model/examples/activerecord_article.rb
index b18ee9c7b..428c4dcc1 100644
--- a/elasticsearch-model/examples/activerecord_article.rb
+++ b/elasticsearch-model/examples/activerecord_article.rb
@@ -26,7 +26,7 @@
end
end
-Kaminari::Hooks.init
+Kaminari::Hooks.init if defined?(Kaminari::Hooks) if defined?(Kaminari::Hooks)
class Article < ActiveRecord::Base
end
diff --git a/elasticsearch-model/test/integration/active_record_pagination_test.rb b/elasticsearch-model/test/integration/active_record_pagination_test.rb
index 78892b73c..e4d30b059 100644
--- a/elasticsearch-model/test/integration/active_record_pagination_test.rb
+++ b/elasticsearch-model/test/integration/active_record_pagination_test.rb
@@ -30,7 +30,7 @@ class ::ArticleForPagination < ActiveRecord::Base
end
end
- Kaminari::Hooks.init
+ Kaminari::Hooks.init if defined?(Kaminari::Hooks)
ArticleForPagination.delete_all
ArticleForPagination.__elasticsearch__.create_index! force: true
From 245f033bd07590bfa70da0574bf46292fdd81698 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 27 Jan 2017 19:38:37 +0100
Subject: [PATCH 115/412] [MODEL] Fixed the deprecation messages for
`raise_in_transactional_callbacks`
See: http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#error-handling-in-transaction-callbacks
---
.../test/integration/active_record_associations_parent_child.rb | 2 ++
.../test/integration/active_record_associations_test.rb | 2 ++
.../test/integration/active_record_basic_test.rb | 2 ++
.../test/integration/active_record_custom_serialization_test.rb | 2 ++
.../test/integration/active_record_import_test.rb | 2 ++
.../test/integration/active_record_namespaced_model_test.rb | 2 ++
.../test/integration/active_record_pagination_test.rb | 2 ++
elasticsearch-model/test/integration/dynamic_index_name_test.rb | 2 ++
elasticsearch-model/test/integration/multiple_models_test.rb | 2 ++
9 files changed, 18 insertions(+)
diff --git a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
index 4d964ac5e..08d8f81df 100644
--- a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
class Question < ActiveRecord::Base
include Elasticsearch::Model
diff --git a/elasticsearch-model/test/integration/active_record_associations_test.rb b/elasticsearch-model/test/integration/active_record_associations_test.rb
index ca0361d89..1271f2b97 100644
--- a/elasticsearch-model/test/integration/active_record_associations_test.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_test.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
module Elasticsearch
module Model
class ActiveRecordAssociationsIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_basic_test.rb b/elasticsearch-model/test/integration/active_record_basic_test.rb
index fe3972301..f6015da70 100644
--- a/elasticsearch-model/test/integration/active_record_basic_test.rb
+++ b/elasticsearch-model/test/integration/active_record_basic_test.rb
@@ -6,6 +6,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
module Elasticsearch
module Model
class ActiveRecordBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb b/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb
index 60b1f6d32..cb706cf99 100644
--- a/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb
+++ b/elasticsearch-model/test/integration/active_record_custom_serialization_test.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
module Elasticsearch
module Model
class ActiveRecordCustomSerializationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_import_test.rb b/elasticsearch-model/test/integration/active_record_import_test.rb
index 31febcb2c..4b9560d3a 100644
--- a/elasticsearch-model/test/integration/active_record_import_test.rb
+++ b/elasticsearch-model/test/integration/active_record_import_test.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
module Elasticsearch
module Model
class ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb b/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb
index d1eb11f27..9885b3a1a 100644
--- a/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb
+++ b/elasticsearch-model/test/integration/active_record_namespaced_model_test.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
module Elasticsearch
module Model
class ActiveRecordNamespacedModelIntegrationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/active_record_pagination_test.rb b/elasticsearch-model/test/integration/active_record_pagination_test.rb
index e4d30b059..e2f58f7e5 100644
--- a/elasticsearch-model/test/integration/active_record_pagination_test.rb
+++ b/elasticsearch-model/test/integration/active_record_pagination_test.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
module Elasticsearch
module Model
class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/dynamic_index_name_test.rb b/elasticsearch-model/test/integration/dynamic_index_name_test.rb
index afecc26c0..f87db7978 100755
--- a/elasticsearch-model/test/integration/dynamic_index_name_test.rb
+++ b/elasticsearch-model/test/integration/dynamic_index_name_test.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
module Elasticsearch
module Model
class DynamicIndexNameTest < Elasticsearch::Test::IntegrationTestCase
diff --git a/elasticsearch-model/test/integration/multiple_models_test.rb b/elasticsearch-model/test/integration/multiple_models_test.rb
index 957aca852..612c695d1 100644
--- a/elasticsearch-model/test/integration/multiple_models_test.rb
+++ b/elasticsearch-model/test/integration/multiple_models_test.rb
@@ -4,6 +4,8 @@
# Needed for ActiveRecord 3.x ?
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
+::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
+
Mongo.setup!
module Elasticsearch
From e6cf1289f541275cbcf977a38e4f87a26e9574c9 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 09:07:58 +0100
Subject: [PATCH 116/412] [MODEL] Fixed the deprecation messages for
`timestamps` in migrations in integration tests
---
.../examples/activerecord_associations.rb | 10 +++++-----
.../active_record_associations_parent_child.rb | 4 ++--
.../integration/active_record_associations_test.rb | 10 +++++-----
3 files changed, 12 insertions(+), 12 deletions(-)
diff --git a/elasticsearch-model/examples/activerecord_associations.rb b/elasticsearch-model/examples/activerecord_associations.rb
index 89b280c26..8e5066eaa 100644
--- a/elasticsearch-model/examples/activerecord_associations.rb
+++ b/elasticsearch-model/examples/activerecord_associations.rb
@@ -28,23 +28,23 @@
ActiveRecord::Schema.define(version: 1) do
create_table :categories do |t|
t.string :title
- t.timestamps
+ t.timestamps null: false
end
create_table :authors do |t|
t.string :first_name, :last_name
- t.timestamps
+ t.timestamps null: false
end
create_table :authorships do |t|
t.references :article
t.references :author
- t.timestamps
+ t.timestamps null: false
end
create_table :articles do |t|
t.string :title
- t.timestamps
+ t.timestamps null: false
end
create_table :articles_categories, id: false do |t|
@@ -54,7 +54,7 @@
create_table :comments do |t|
t.string :text
t.references :article
- t.timestamps
+ t.timestamps null: false
end
add_index(:comments, :article_id) unless index_exists?(:comments, :article_id)
diff --git a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
index 08d8f81df..956c80036 100644
--- a/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_parent_child.rb
@@ -71,14 +71,14 @@ class ActiveRecordAssociationsParentChildIntegrationTest < Elasticsearch::Test::
t.string :title
t.text :text
t.string :author
- t.timestamps
+ t.timestamps null: false
end
create_table :answers do |t|
t.text :text
t.string :author
t.references :question
- t.timestamps
+ t.timestamps null: false
end
add_index(:answers, :question_id) unless index_exists?(:answers, :question_id)
diff --git a/elasticsearch-model/test/integration/active_record_associations_test.rb b/elasticsearch-model/test/integration/active_record_associations_test.rb
index 1271f2b97..933672e25 100644
--- a/elasticsearch-model/test/integration/active_record_associations_test.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_test.rb
@@ -70,7 +70,7 @@ def as_indexed_json(options={})
ActiveRecord::Schema.define(version: 1) do
create_table :categories do |t|
t.string :title
- t.timestamps
+ t.timestamps null: false
end
create_table :categories_posts, id: false do |t|
@@ -79,21 +79,21 @@ def as_indexed_json(options={})
create_table :authors do |t|
t.string :first_name, :last_name
- t.timestamps
+ t.timestamps null: false
end
create_table :authorships do |t|
t.string :first_name, :last_name
t.references :post
t.references :author
- t.timestamps
+ t.timestamps null: false
end
create_table :comments do |t|
t.string :text
t.string :author
t.references :post
- t.timestamps
+ t.timestamps null: false
end
add_index(:comments, :post_id) unless index_exists?(:comments, :post_id)
@@ -102,7 +102,7 @@ def as_indexed_json(options={})
t.string :title
t.text :text
t.boolean :published
- t.timestamps
+ t.timestamps null: false
end
end
From a13dd89cba0f5ac6e64dee672591d2c14740d2b1 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 09:14:17 +0100
Subject: [PATCH 117/412] [MODEL] Fixed the naming for the indexing integration
tests
---
elasticsearch-model/test/unit/indexing_test.rb | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/elasticsearch-model/test/unit/indexing_test.rb b/elasticsearch-model/test/unit/indexing_test.rb
index a52603a1e..b415f3316 100644
--- a/elasticsearch-model/test/unit/indexing_test.rb
+++ b/elasticsearch-model/test/unit/indexing_test.rb
@@ -423,7 +423,7 @@ def as_indexed_json(options={})
end
context "Checking for index existence" do
- context "the index exists" do
+ context "when the index exists" do
should "return true" do
indices = mock('indices', exists: true)
client = stub('client', indices: indices)
@@ -434,7 +434,7 @@ def as_indexed_json(options={})
end
end
- context "the index does not exists" do
+ context "when the index does not exists" do
should "return false" do
indices = mock('indices', exists: false)
client = stub('client', indices: indices)
@@ -445,7 +445,7 @@ def as_indexed_json(options={})
end
end
- context "the indices raises" do
+ context "when the indices API raises an error" do
should "return false" do
client = stub('client')
client.expects(:indices).raises(StandardError)
@@ -456,7 +456,7 @@ def as_indexed_json(options={})
end
end
- context "the indices raises" do
+ context "the indices.exists API raises an error" do
should "return false" do
indices = stub('indices')
client = stub('client')
From 9ec745499f9092fb3aa5923d8be215e7d11e481b Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 09:14:54 +0100
Subject: [PATCH 118/412] [MODEL] Fixed the failing integration tests for
ActiveRecord associations
---
.../integration/active_record_associations_test.rb | 11 +++++++++--
1 file changed, 9 insertions(+), 2 deletions(-)
diff --git a/elasticsearch-model/test/integration/active_record_associations_test.rb b/elasticsearch-model/test/integration/active_record_associations_test.rb
index 933672e25..0648057f3 100644
--- a/elasticsearch-model/test/integration/active_record_associations_test.rb
+++ b/elasticsearch-model/test/integration/active_record_associations_test.rb
@@ -115,6 +115,8 @@ class Category < ActiveRecord::Base
class Author < ActiveRecord::Base
has_many :authorships
+ after_update { self.authorships.each(&:touch) }
+
def full_name
[first_name, last_name].compact.join(' ')
end
@@ -133,8 +135,13 @@ class Post < ActiveRecord::Base
has_and_belongs_to_many :categories, after_add: [ lambda { |a,c| a.__elasticsearch__.index_document } ],
after_remove: [ lambda { |a,c| a.__elasticsearch__.index_document } ]
has_many :authorships
- has_many :authors, through: :authorships
- has_many :comments
+ has_many :authors, through: :authorships,
+ after_add: [ lambda { |a,c| a.__elasticsearch__.index_document } ],
+ after_remove: [ lambda { |a,c| a.__elasticsearch__.index_document } ]
+ has_many :comments, after_add: [ lambda { |a,c| a.__elasticsearch__.index_document } ],
+ after_remove: [ lambda { |a,c| a.__elasticsearch__.index_document } ]
+
+ after_touch() { __elasticsearch__.index_document }
end
# Include the search integration
From 39b1167b00dc46a2707e5852a3d611a019a33f01 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 09:15:30 +0100
Subject: [PATCH 119/412] [MODEL] Fixed integration tests for ActiveRecord
pagination
---
.../test/integration/active_record_pagination_test.rb | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/elasticsearch-model/test/integration/active_record_pagination_test.rb b/elasticsearch-model/test/integration/active_record_pagination_test.rb
index e2f58f7e5..9c07ef115 100644
--- a/elasticsearch-model/test/integration/active_record_pagination_test.rb
+++ b/elasticsearch-model/test/integration/active_record_pagination_test.rb
@@ -92,12 +92,11 @@ class ::ArticleForPagination < ActiveRecord::Base
assert_equal 0, records.size
assert_equal 6, records.current_page
- assert_equal 5, records.prev_page
+
assert_equal nil, records.next_page
assert_equal 3, records.total_pages
assert ! records.first_page?, "Should NOT be the first page"
- assert records.last_page?, "Should be the last page"
assert records.out_of_range?, "Should be out of range"
end
From f547cfa94eef8e654015d73451fb8a96ab7c164c Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 11:23:30 +0100
Subject: [PATCH 120/412] [MODEL] Added the `rake bundle:install` Rake task to
install dependencies for all gemfiles
---
elasticsearch-model/Rakefile | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/elasticsearch-model/Rakefile b/elasticsearch-model/Rakefile
index 031ca8599..25b71168e 100644
--- a/elasticsearch-model/Rakefile
+++ b/elasticsearch-model/Rakefile
@@ -4,6 +4,15 @@ desc "Run unit tests"
task :default => 'test:unit'
task :test => 'test:unit'
+namespace :bundler do
+ desc "Install dependencies for all the Gemfiles"
+ task :install do
+ sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle install"
+ sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle install"
+ sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle install"
+ end
+end
+
# ----- Test tasks ------------------------------------------------------------
require 'rake/testtask'
From 029be4cc3577611c51612323c2a53384c72dca1f Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 09:31:02 +0100
Subject: [PATCH 121/412] [MODEL] Run unit tests against all Gemfiles
Also, reduce the verbosity of tests
---
elasticsearch-model/Rakefile | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/elasticsearch-model/Rakefile b/elasticsearch-model/Rakefile
index 25b71168e..24b0a7126 100644
--- a/elasticsearch-model/Rakefile
+++ b/elasticsearch-model/Rakefile
@@ -23,12 +23,12 @@ namespace :test do
Rake::Task['ci:setup:minitest'].invoke
end
- Rake::TestTask.new(:unit) do |test|
+ Rake::TestTask.new(:run_unit) do |test|
Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
- # test.verbose = true
- # test.warning = true
+ test.verbose = false
+ test.warning = false
end
Rake::TestTask.new(:run_integration) do |test|
@@ -39,9 +39,16 @@ namespace :test do
test.warning = false
end
+ desc "Run unit tests against ActiveModel 3, 4 and 5"
+ task :unit do
+ sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
+ sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
+ 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"
task :integration do
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration" unless defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
+ 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
From db2cd1cccecea678457f90142c0d00240799fb19 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 11:22:36 +0100
Subject: [PATCH 122/412] [MODEL] Updated dependencies in gemspec
---
elasticsearch-model/elasticsearch-model.gemspec | 17 +++++++----------
1 file changed, 7 insertions(+), 10 deletions(-)
diff --git a/elasticsearch-model/elasticsearch-model.gemspec b/elasticsearch-model/elasticsearch-model.gemspec
index 07ecc87b9..2f289b6bc 100644
--- a/elasticsearch-model/elasticsearch-model.gemspec
+++ b/elasticsearch-model/elasticsearch-model.gemspec
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 1.9.3"
- s.add_dependency "elasticsearch", '> 0.4'
+ s.add_dependency "elasticsearch", '~> 1.1'
s.add_dependency "activesupport", '> 3'
s.add_dependency "hashie"
@@ -33,25 +33,22 @@ Gem::Specification.new do |s|
s.add_development_dependency "elasticsearch-extensions"
s.add_development_dependency "sqlite3"
- s.add_development_dependency "activemodel", "> 3.0"
+ s.add_development_dependency "activemodel", "> 3"
s.add_development_dependency "oj"
s.add_development_dependency "kaminari"
s.add_development_dependency "will_paginate"
- s.add_development_dependency "minitest", ">= 4.2"
- s.add_development_dependency "test-unit" if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
+ s.add_development_dependency "minitest"
+ s.add_development_dependency "test-unit"
s.add_development_dependency "shoulda-context"
s.add_development_dependency "mocha"
s.add_development_dependency "turn"
s.add_development_dependency "yard"
s.add_development_dependency "ruby-prof"
s.add_development_dependency "pry"
- s.add_development_dependency "ci_reporter", "~> 1.9"
- if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
- s.add_development_dependency "simplecov"
- s.add_development_dependency "cane"
- s.add_development_dependency "require-prof"
- end
+ s.add_development_dependency "simplecov"
+ s.add_development_dependency "cane"
+ s.add_development_dependency "require-prof"
end
From fca8b6b4126aa01c2df10014888c8634331a5220 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 11:23:07 +0100
Subject: [PATCH 123/412] [STORE] Updated dependencies in gemspec
---
.../elasticsearch-persistence.gemspec | 19 ++++++++-----------
1 file changed, 8 insertions(+), 11 deletions(-)
diff --git a/elasticsearch-persistence/elasticsearch-persistence.gemspec b/elasticsearch-persistence/elasticsearch-persistence.gemspec
index 2b7a7cbea..ab8ecc163 100644
--- a/elasticsearch-persistence/elasticsearch-persistence.gemspec
+++ b/elasticsearch-persistence/elasticsearch-persistence.gemspec
@@ -23,10 +23,10 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 1.9.3"
- s.add_dependency "elasticsearch", '> 0.4'
+ s.add_dependency "elasticsearch", '~> 1.1'
s.add_dependency "elasticsearch-model", '>= 0.1'
- s.add_dependency "activesupport", '> 3'
- s.add_dependency "activemodel", '> 3'
+ s.add_dependency "activesupport", '> 4'
+ s.add_dependency "activemodel", '> 4'
s.add_dependency "hashie"
s.add_dependency "virtus"
@@ -35,22 +35,19 @@ Gem::Specification.new do |s|
s.add_development_dependency "oj"
- s.add_development_dependency "rails"
+ s.add_development_dependency "rails", '> 4'
s.add_development_dependency "elasticsearch-extensions"
- s.add_development_dependency "minitest", "~> 4.2"
- s.add_development_dependency "test-unit" if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
+ s.add_development_dependency "minitest"
+ s.add_development_dependency "test-unit"
s.add_development_dependency "shoulda-context"
s.add_development_dependency "mocha"
s.add_development_dependency "turn"
s.add_development_dependency "yard"
s.add_development_dependency "ruby-prof"
s.add_development_dependency "pry"
- s.add_development_dependency "ci_reporter", "~> 1.9"
- if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
- s.add_development_dependency "simplecov"
- s.add_development_dependency "cane"
- end
+ s.add_development_dependency "simplecov"
+ s.add_development_dependency "cane"
end
From eb9ef48d806380258a4579a7d6bcdbd1fca1c033 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 11:23:22 +0100
Subject: [PATCH 124/412] [STORE] Updated dependencies in gemspec
---
elasticsearch-rails/elasticsearch-rails.gemspec | 13 +++++--------
1 file changed, 5 insertions(+), 8 deletions(-)
diff --git a/elasticsearch-rails/elasticsearch-rails.gemspec b/elasticsearch-rails/elasticsearch-rails.gemspec
index f33c9f6b7..71b4c1abb 100644
--- a/elasticsearch-rails/elasticsearch-rails.gemspec
+++ b/elasticsearch-rails/elasticsearch-rails.gemspec
@@ -34,19 +34,16 @@ Gem::Specification.new do |s|
s.add_development_dependency "lograge"
- s.add_development_dependency "minitest", "~> 4.2"
- s.add_development_dependency "test-unit" if defined?(RUBY_VERSION) && RUBY_VERSION > '2.2'
+ s.add_development_dependency "minitest"
+ s.add_development_dependency "test-unit"
s.add_development_dependency "shoulda-context"
s.add_development_dependency "mocha"
s.add_development_dependency "turn"
s.add_development_dependency "yard"
s.add_development_dependency "ruby-prof"
s.add_development_dependency "pry"
- s.add_development_dependency "ci_reporter", "~> 1.9"
- if defined?(RUBY_VERSION) && RUBY_VERSION > '1.9'
- s.add_development_dependency "simplecov"
- s.add_development_dependency "cane"
- s.add_development_dependency "require-prof"
- end
+ s.add_development_dependency "simplecov"
+ s.add_development_dependency "cane"
+ s.add_development_dependency "require-prof"
end
From f6c1fdb90ea564c02168f8b15d73207626cc0180 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 09:59:45 +0100
Subject: [PATCH 125/412] Removed the "CI Reporter" integration from test Rake
tasks
The integration is failing, so let's first remove it and figure out
later how to generate code statistics for test runs.
---
Rakefile | 16 ----------------
elasticsearch-model/Rakefile | 10 ----------
elasticsearch-persistence/Rakefile | 9 ---------
elasticsearch-rails/Rakefile | 9 ---------
4 files changed, 44 deletions(-)
diff --git a/Rakefile b/Rakefile
index 776e3004a..841b9c1b9 100644
--- a/Rakefile
+++ b/Rakefile
@@ -56,7 +56,6 @@ namespace :test do
desc "Run unit tests in all subprojects"
task :unit do
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
subprojects.each do |project|
puts '-'*80
sh "cd #{__current__.join(project)} && unset BUNDLE_GEMFILE && bundle exec rake test:unit"
@@ -66,8 +65,6 @@ namespace :test do
desc "Run integration tests in all subprojects"
task :integration do
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
-
# 1/ elasticsearch-model
#
puts '-'*80
@@ -93,23 +90,10 @@ namespace :test do
desc "Run all tests in all subprojects"
task :all do
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
-
Rake::Task['test:unit'].invoke
Rake::Task['test:integration'].invoke
end
- task :ci_reporter do
- ENV['CI_REPORTS'] ||= 'tmp/reports'
- if defined?(RUBY_VERSION) && RUBY_VERSION < '1.9'
- require 'ci/reporter/rake/test_unit'
- Rake::Task['ci:setup:testunit'].invoke
- else
- require 'ci/reporter/rake/minitest'
- Rake::Task['ci:setup:minitest'].invoke
- end
- end
-
namespace :cluster do
desc "Start Elasticsearch nodes for tests"
task :start do
diff --git a/elasticsearch-model/Rakefile b/elasticsearch-model/Rakefile
index 24b0a7126..1efad46da 100644
--- a/elasticsearch-model/Rakefile
+++ b/elasticsearch-model/Rakefile
@@ -17,14 +17,7 @@ end
require 'rake/testtask'
namespace :test do
- task :ci_reporter do
- ENV['CI_REPORTS'] ||= 'tmp/reports'
- require 'ci/reporter/rake/minitest'
- Rake::Task['ci:setup:minitest'].invoke
- end
-
Rake::TestTask.new(:run_unit) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
test.verbose = false
@@ -32,7 +25,6 @@ namespace :test do
end
Rake::TestTask.new(:run_integration) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
test.verbose = false
@@ -55,8 +47,6 @@ namespace :test do
desc "Run unit and integration tests"
task :all do
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
-
Rake::Task['test:unit'].invoke
Rake::Task['test:integration'].invoke
end
diff --git a/elasticsearch-persistence/Rakefile b/elasticsearch-persistence/Rakefile
index f92cfd64e..61038d08c 100644
--- a/elasticsearch-persistence/Rakefile
+++ b/elasticsearch-persistence/Rakefile
@@ -8,14 +8,7 @@ task :test => 'test:unit'
require 'rake/testtask'
namespace :test do
- task :ci_reporter do
- ENV['CI_REPORTS'] ||= 'tmp/reports'
- require 'ci/reporter/rake/minitest'
- Rake::Task['ci:setup:minitest'].invoke
- end
-
Rake::TestTask.new(:unit) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
test.verbose = false
@@ -23,7 +16,6 @@ namespace :test do
end
Rake::TestTask.new(:integration) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
test.verbose = false
@@ -31,7 +23,6 @@ namespace :test do
end
Rake::TestTask.new(:all) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb", "test/integration/**/*_test.rb"]
end
diff --git a/elasticsearch-rails/Rakefile b/elasticsearch-rails/Rakefile
index dfad126cb..622731c66 100644
--- a/elasticsearch-rails/Rakefile
+++ b/elasticsearch-rails/Rakefile
@@ -8,14 +8,7 @@ task :test => 'test:unit'
require 'rake/testtask'
namespace :test do
- task :ci_reporter do
- ENV['CI_REPORTS'] ||= 'tmp/reports'
- require 'ci/reporter/rake/minitest'
- Rake::Task['ci:setup:minitest'].invoke
- end
-
Rake::TestTask.new(:unit) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
test.verbose = false
@@ -23,7 +16,6 @@ namespace :test do
end
Rake::TestTask.new(:integration) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
test.verbose = false
@@ -31,7 +23,6 @@ namespace :test do
end
Rake::TestTask.new(:all) do |test|
- Rake::Task['test:ci_reporter'].invoke if ENV['CI']
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb", "test/integration/**/*_test.rb"]
end
From a4696efe94d034d03f60cb8544f6286b782f2741 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 11:44:17 +0100
Subject: [PATCH 126/412] Changed, that the main `Gemfile.lock` is not removed
in the `bundle:clean` Rake task
---
Rakefile | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)
diff --git a/Rakefile b/Rakefile
index 841b9c1b9..64fab6ac6 100644
--- a/Rakefile
+++ b/Rakefile
@@ -1,6 +1,6 @@
require 'pathname'
-subprojects = %w| elasticsearch-model elasticsearch-rails elasticsearch-persistence |
+subprojects = %w| elasticsearch-rails elasticsearch-persistence elasticsearch-model |
__current__ = Pathname( File.expand_path('..', __FILE__) )
@@ -23,9 +23,6 @@ task :bundle => 'bundle:install'
namespace :bundle do
desc "Run `bundle install` in all subprojects"
task :install do
- puts '-'*80
- sh "bundle install --gemfile #{__current__}/Gemfile"
- puts
subprojects.each do |project|
puts '-'*80
sh "bundle install --gemfile #{__current__.join(project)}/Gemfile"
@@ -41,7 +38,6 @@ namespace :bundle do
desc "Remove Gemfile.lock in all subprojects"
task :clean do
- sh "rm -f Gemfile.lock"
subprojects.each do |project|
sh "rm -f #{__current__.join(project)}/Gemfile.lock"
end
From 135fd285af1fc16e434462a79c1808ff7639c841 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 17:28:23 +0100
Subject: [PATCH 127/412] Removed unnecessary gems from the main Gemfile
---
Gemfile | 13 -------------
1 file changed, 13 deletions(-)
diff --git a/Gemfile b/Gemfile
index 13be68981..4a7fe0473 100644
--- a/Gemfile
+++ b/Gemfile
@@ -3,21 +3,8 @@ source 'https://rubygems.org'
gem "bundler", "~> 1"
gem "rake", "~> 11.1"
-gem 'elasticsearch'
gem 'elasticsearch-extensions'
-gem 'elasticsearch-model', :path => File.expand_path("../elasticsearch-model", __FILE__), :require => false
-gem 'elasticsearch-rails', :path => File.expand_path("../elasticsearch-rails", __FILE__), :require => false
-
gem "pry"
gem "ansi"
-gem "shoulda-context"
-gem "mocha"
-gem "turn"
-gem "yard"
-gem "ci_reporter", "~> 1.9"
-gem "ruby-prof"
-gem "simplecov"
-gem "simplecov-rcov"
gem "cane"
-gem "require-prof"
From a063458586033a517a4268a88e1ba29882e12bb2 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 28 Jan 2017 17:56:12 +0100
Subject: [PATCH 128/412] Updated the Travis CI configuration
Related: https://github.com/elastic/elasticsearch-ruby/issues/389
---
.travis.yml | 41 +++++++++++++++++++++++++----------------
1 file changed, 25 insertions(+), 16 deletions(-)
diff --git a/.travis.yml b/.travis.yml
index 39d626bdb..ba677f628 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,39 +2,48 @@
# Configuration file for http://travis-ci.org/elasticsearch/elasticsearch-rails
# -----------------------------------------------------------------------------
+dist: trusty
+
+sudo: required
+
language: ruby
+services:
+ - mongodb
+
branches:
only:
- master
- travis
-rvm:
- - 1.9.3
- - 2.1
- - 2.2
+matrix:
+ include:
+ - rvm: 2.2.6
+ jdk: oraclejdk8
+ env: TEST_SUITE=unit
-jdk:
- - openjdk7
+ - rvm: 2.3.3
+ jdk: oraclejdk8
+ env: TEST_SUITE=unit
-services:
- - mongodb
+ - rvm: 2.3.3
+ jdk: oraclejdk8
+ env: TEST_SUITE=integration SERVER=start TEST_CLUSTER_LOGS=/tmp/log TEST_CLUSTER_COMMAND=/tmp/elasticsearch-1.7.5/bin/elasticsearch
before_install:
- - gem update --system
+ - gem update --system --no-rdoc --no-ri
- gem --version
- - gem install bundler -v 1.11.2
+ - gem install bundler -v 1.14.3 --no-rdoc --no-ri
+ - bundle version
+ - curl -sS https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-1.7.5.tar.gz | tar xz -C /tmp
-before_script:
- - ls -la /usr/share/elasticsearch/bin/elasticsearch
- - elasticsearch -v
+install:
+ - bundle install
- rake bundle:clean
- rake bundle:install
script:
- - SERVER=launch TEST_CLUSTER_COMMAND=/usr/share/elasticsearch/bin/elasticsearch TEST_CLUSTER_PARAMS='-Des.default.path.conf=/etc/elasticsearch/ -Des.default.path.logs==/var/log/elasticsearch/' bundle exec rake test:all
+ - rake test:$TEST_SUITE
notifications:
disable: true
-
-sudo: false
From 1dc6b8e1b84828d75308abdd62cc1d55ded1453a Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sun, 29 Jan 2017 08:54:29 +0100
Subject: [PATCH 129/412] Updated the main README
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
* Added a "Development" chapter and improved the instructions
* Added Travis CI and Code Climate badges
---
README.md | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index fc1275f98..67c57d11f 100644
--- a/README.md
+++ b/README.md
@@ -132,15 +132,26 @@ Article.create title: 'Test'
* [[Documentation]](http://rubydoc.info/gems/elasticsearch-rails)
* [[Test Suite]](https://github.com/elasticsearch/elasticsearch-rails/blob/master/elasticsearch-rails/test)
-## Running the Test Suite
+## Development
+
+[](https://travis-ci.org/elastic/elasticsearch-ruby) [](https://codeclimate.com/github/elastic/elasticsearch-ruby)
+
+To work on the code, clone the repository and install all dependencies first:
+
+```
+git clone https://github.com/elastic/elasticsearch-rails.git
+cd elasticsearch-rails/
+bundle install
+rake bundle:install
+```
+
+### Running the Test Suite
You can run unit and integration tests for each sub-project by running the respective Rake tasks in their folders.
-You can also unit, integration, or both tests in the top level directory for each sub-project:
+You can also unit, integration, or both tests for all sub-projects from the top-level directory:
- rake bundle:clean
- rake bundle:install
- bundle exec rake test:all
+ rake test:all
The test suite expects an Elasticsearch cluster running on port 9250, and **will delete all the data**. You can launch an isolated, in-memory Elasticsearch cluster with the following Rake task:
From 915556c8d1187f1e897b3e8544c40a719a79ccaf Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 3 Feb 2017 14:32:59 +0100
Subject: [PATCH 130/412] [MODEL] Relaxed the dependency on the "elasticsearch"
gem
Related: #665
---
elasticsearch-model/elasticsearch-model.gemspec | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/elasticsearch-model/elasticsearch-model.gemspec b/elasticsearch-model/elasticsearch-model.gemspec
index 2f289b6bc..e058ac43d 100644
--- a/elasticsearch-model/elasticsearch-model.gemspec
+++ b/elasticsearch-model/elasticsearch-model.gemspec
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 1.9.3"
- s.add_dependency "elasticsearch", '~> 1.1'
+ s.add_dependency "elasticsearch", '> 1'
s.add_dependency "activesupport", '> 3'
s.add_dependency "hashie"
From 69a561bcb4892f5c4aedd6f4838687e422615ee4 Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Fri, 3 Feb 2017 14:33:33 +0100
Subject: [PATCH 131/412] [STORE] Relaxed the dependency on the "elasticsearch"
gem
Related: #665
---
elasticsearch-persistence/elasticsearch-persistence.gemspec | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/elasticsearch-persistence/elasticsearch-persistence.gemspec b/elasticsearch-persistence/elasticsearch-persistence.gemspec
index ab8ecc163..510da3a3e 100644
--- a/elasticsearch-persistence/elasticsearch-persistence.gemspec
+++ b/elasticsearch-persistence/elasticsearch-persistence.gemspec
@@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.required_ruby_version = ">= 1.9.3"
- s.add_dependency "elasticsearch", '~> 1.1'
+ s.add_dependency "elasticsearch", '> 1'
s.add_dependency "elasticsearch-model", '>= 0.1'
s.add_dependency "activesupport", '> 4'
s.add_dependency "activemodel", '> 4'
From 5c692b74f8cd5d1fb1bbca4670d726bbf850ef4b Mon Sep 17 00:00:00 2001
From: Karel Minarik
Date: Sat, 11 Feb 2017 09:40:20 +0100
Subject: [PATCH 132/412] [RAILS] Updated the application templates to support
Rails 5 & Elasticsearch 5
This patch adds support for both Rails 5 and Elasticsearch 5 for the
application templates in `elasticsearch-rails/lib/rails/templates/`.
The main problems on the Elasticsearch side have been in incorrect mapping,
which still used the `multi_field` property type. The mapping definition
in the `searchable.rb` and `searchable.dsl.rb` has been fixed, along
with small fixes in HTML views an the `SearchController`.
On the Rails side, the `quiet_assets` gem has been removed, as originally
reported by @frontandstart in #634, and various fixes in the HTML views
(using `params.permit`, ...) and in the controller test (using the new
syntax for passing parameters) have been done.
The templates are still compatible with Rails 4.
Closes #655
---
.../lib/rails/templates/01-basic.rb | 18 +++++-----
.../lib/rails/templates/02-pretty.rb | 4 +--
.../lib/rails/templates/03-expert.rb | 36 +++++--------------
.../lib/rails/templates/04-dsl.rb | 12 +++----
.../lib/rails/templates/05-settings-files.rb | 6 ----
.../lib/rails/templates/index.html.dsl.erb | 18 +++++-----
.../lib/rails/templates/index.html.erb | 24 ++++++-------
.../templates/search_controller_test.dsl.rb | 23 ++++++------
.../rails/templates/search_controller_test.rb | 22 ++++++------
.../lib/rails/templates/searchable.dsl.rb | 18 +++++-----
.../lib/rails/templates/searchable.rb | 21 +++++------
11 files changed, 89 insertions(+), 113 deletions(-)
diff --git a/elasticsearch-rails/lib/rails/templates/01-basic.rb b/elasticsearch-rails/lib/rails/templates/01-basic.rb
index 129d5ffd7..eb06e167b 100644
--- a/elasticsearch-rails/lib/rails/templates/01-basic.rb
+++ b/elasticsearch-rails/lib/rails/templates/01-basic.rb
@@ -41,7 +41,9 @@
# ----- Download Elasticsearch --------------------------------------------------------------------
-unless (Net::HTTP.get(URI.parse('http://localhost:9200')) rescue false)
+ELASTICSEARCH_URL = ENV.fetch('ELASTICSEARCH_URL', 'http://localhost:9200')
+
+unless (Net::HTTP.get(URI.parse(ELASTICSEARCH_URL)) rescue false)
COMMAND = <<-COMMAND.gsub(/^ /, '')
curl -# -O "http://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.0.1.tar.gz"
tar -zxf elasticsearch-1.0.1.tar.gz
@@ -116,6 +118,7 @@
# ----- Auxiliary gems ----------------------------------------------------------------------------
gem 'mocha', group: 'test', require: 'mocha/api'
+gem 'rails-controller-testing', group: 'test'
# ----- Remove CoffeeScript, Sass and "all that jazz" ---------------------------------------------
@@ -145,9 +148,8 @@
puts '-'*80, ''; sleep 0.25
environment 'config.assets.logger = false', env: 'development'
-gem 'quiet_assets', group: "development"
+environment 'config.assets.quiet = true', env: 'development'
-git add: "Gemfile*"
git add: "config/"
git commit: "-m 'Disabled asset logging in development'"
@@ -208,7 +210,7 @@ def search
CODE
end
-inject_into_file 'app/views/articles/index.html.erb', after: %r{
Listing articles
}i do
+inject_into_file 'app/views/articles/index.html.erb', after: %r{
.*Articles
}i do
<<-CODE
@@ -236,21 +238,21 @@ def search
end
CODE
-gsub_file "#{Rails::VERSION::STRING > '4' ? 'test/controllers' : 'test/functional'}/articles_controller_test.rb", %r{setup do.*?end}m, <<-CODE
+gsub_file "test/controllers/articles_controller_test.rb", %r{setup do.*?end}m, <<-CODE
setup do
@article = articles(:one)
- Article.__elasticsearch__.import
+ Article.__elasticsearch__.import force: true
Article.__elasticsearch__.refresh_index!
end
CODE
-inject_into_file "#{Rails::VERSION::STRING > '4' ? 'test/controllers' : 'test/functional'}/articles_controller_test.rb", after: %r{test "should get index" do.*?end}m do
+inject_into_file "test/controllers/articles_controller_test.rb", after: %r{test "should get index" do.*?end}m do
<<-CODE
test "should get search results" do
- get :search, q: 'mystring'
+ #{ Rails::VERSION::STRING > '5' ? 'get search_articles_url(q: "mystring")' : 'get :search, q: "mystring"' }
assert_response :success
assert_not_nil assigns(:articles)
assert_equal 2, assigns(:articles).size
diff --git a/elasticsearch-rails/lib/rails/templates/02-pretty.rb b/elasticsearch-rails/lib/rails/templates/02-pretty.rb
index 7fd6e5048..ef588247c 100644
--- a/elasticsearch-rails/lib/rails/templates/02-pretty.rb
+++ b/elasticsearch-rails/lib/rails/templates/02-pretty.rb
@@ -154,8 +154,8 @@ def self.search(query)
# ----- Customize the header -----------------------------------------------------------------
-gsub_file 'app/views/articles/index.html.erb', %r{