elasticsearch-model 6.0.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -0
  3. data/README.md +14 -7
  4. data/Rakefile +27 -36
  5. data/elasticsearch-model.gemspec +1 -1
  6. data/examples/activerecord_mapping_completion.rb +2 -15
  7. data/gemfiles/3.0.gemfile +6 -1
  8. data/gemfiles/4.0.gemfile +7 -1
  9. data/gemfiles/5.0.gemfile +6 -0
  10. data/lib/elasticsearch/model.rb +15 -8
  11. data/lib/elasticsearch/model/adapters/active_record.rb +7 -26
  12. data/lib/elasticsearch/model/indexing.rb +5 -3
  13. data/lib/elasticsearch/model/naming.rb +6 -1
  14. data/lib/elasticsearch/model/response.rb +2 -2
  15. data/lib/elasticsearch/model/response/pagination.rb +2 -192
  16. data/lib/elasticsearch/model/response/pagination/kaminari.rb +109 -0
  17. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +95 -0
  18. data/lib/elasticsearch/model/response/result.rb +1 -1
  19. data/lib/elasticsearch/model/version.rb +1 -1
  20. data/spec/elasticsearch/model/adapter_spec.rb +119 -0
  21. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +334 -0
  22. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +340 -0
  23. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +18 -0
  24. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +187 -0
  25. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +110 -0
  26. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +38 -0
  27. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +315 -0
  28. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +75 -0
  29. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +61 -0
  30. data/spec/elasticsearch/model/adapters/active_record_spec.rb +207 -0
  31. data/spec/elasticsearch/model/adapters/default_spec.rb +41 -0
  32. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +267 -0
  33. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +66 -0
  34. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +235 -0
  35. data/spec/elasticsearch/model/adapters/multiple_spec.rb +125 -0
  36. data/spec/elasticsearch/model/callbacks_spec.rb +33 -0
  37. data/spec/elasticsearch/model/client_spec.rb +66 -0
  38. data/spec/elasticsearch/model/hash_wrapper_spec.rb +12 -0
  39. data/spec/elasticsearch/model/importing_spec.rb +214 -0
  40. data/spec/elasticsearch/model/indexing_spec.rb +918 -0
  41. data/spec/elasticsearch/model/module_spec.rb +101 -0
  42. data/spec/elasticsearch/model/multimodel_spec.rb +55 -0
  43. data/spec/elasticsearch/model/naming_inheritance_spec.rb +184 -0
  44. data/spec/elasticsearch/model/naming_spec.rb +186 -0
  45. data/spec/elasticsearch/model/proxy_spec.rb +107 -0
  46. data/spec/elasticsearch/model/response/aggregations_spec.rb +66 -0
  47. data/spec/elasticsearch/model/response/base_spec.rb +90 -0
  48. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +410 -0
  49. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +262 -0
  50. data/spec/elasticsearch/model/response/records_spec.rb +118 -0
  51. data/spec/elasticsearch/model/response/response_spec.rb +131 -0
  52. data/spec/elasticsearch/model/response/result_spec.rb +122 -0
  53. data/spec/elasticsearch/model/response/results_spec.rb +56 -0
  54. data/spec/elasticsearch/model/searching_search_request_spec.rb +112 -0
  55. data/spec/elasticsearch/model/searching_spec.rb +49 -0
  56. data/spec/elasticsearch/model/serializing_spec.rb +22 -0
  57. data/spec/spec_helper.rb +161 -0
  58. data/spec/support/app.rb +21 -0
  59. data/spec/support/app/answer.rb +33 -0
  60. data/spec/support/app/article.rb +22 -0
  61. data/spec/support/app/article_for_pagination.rb +12 -0
  62. data/spec/support/app/article_with_custom_serialization.rb +13 -0
  63. data/spec/support/app/article_with_dynamic_index_name.rb +15 -0
  64. data/spec/support/app/author.rb +9 -0
  65. data/spec/support/app/authorship.rb +4 -0
  66. data/spec/support/app/category.rb +3 -0
  67. data/spec/support/app/comment.rb +3 -0
  68. data/spec/support/app/episode.rb +11 -0
  69. data/spec/support/app/image.rb +19 -0
  70. data/spec/support/app/import_article.rb +12 -0
  71. data/spec/support/app/mongoid_article.rb +21 -0
  72. data/spec/support/app/namespaced_book.rb +10 -0
  73. data/spec/support/app/parent_and_child_searchable.rb +24 -0
  74. data/spec/support/app/post.rb +14 -0
  75. data/spec/support/app/question.rb +27 -0
  76. data/spec/support/app/searchable.rb +48 -0
  77. data/spec/support/app/series.rb +11 -0
  78. data/spec/support/model.json +1 -0
  79. data/{test → spec}/support/model.yml +0 -0
  80. metadata +129 -86
  81. data/test/integration/active_record_associations_parent_child_test.rb +0 -188
  82. data/test/integration/active_record_associations_test.rb +0 -339
  83. data/test/integration/active_record_basic_test.rb +0 -255
  84. data/test/integration/active_record_custom_serialization_test.rb +0 -67
  85. data/test/integration/active_record_import_test.rb +0 -168
  86. data/test/integration/active_record_namespaced_model_test.rb +0 -56
  87. data/test/integration/active_record_pagination_test.rb +0 -149
  88. data/test/integration/dynamic_index_name_test.rb +0 -52
  89. data/test/integration/mongoid_basic_test.rb +0 -240
  90. data/test/integration/multiple_models_test.rb +0 -176
  91. data/test/support/model.json +0 -1
  92. data/test/test_helper.rb +0 -92
  93. data/test/unit/adapter_active_record_test.rb +0 -157
  94. data/test/unit/adapter_default_test.rb +0 -41
  95. data/test/unit/adapter_mongoid_test.rb +0 -161
  96. data/test/unit/adapter_multiple_test.rb +0 -106
  97. data/test/unit/adapter_test.rb +0 -69
  98. data/test/unit/callbacks_test.rb +0 -31
  99. data/test/unit/client_test.rb +0 -27
  100. data/test/unit/hash_wrapper_test.rb +0 -13
  101. data/test/unit/importing_test.rb +0 -224
  102. data/test/unit/indexing_test.rb +0 -720
  103. data/test/unit/module_test.rb +0 -68
  104. data/test/unit/multimodel_test.rb +0 -38
  105. data/test/unit/naming_inheritance_test.rb +0 -94
  106. data/test/unit/naming_test.rb +0 -103
  107. data/test/unit/proxy_test.rb +0 -98
  108. data/test/unit/response_aggregations_test.rb +0 -46
  109. data/test/unit/response_base_test.rb +0 -40
  110. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  111. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  112. data/test/unit/response_records_test.rb +0 -91
  113. data/test/unit/response_result_test.rb +0 -90
  114. data/test/unit/response_results_test.rb +0 -34
  115. data/test/unit/response_test.rb +0 -104
  116. data/test/unit/searching_search_request_test.rb +0 -78
  117. data/test/unit/searching_test.rb +0 -41
  118. data/test/unit/serializing_test.rb +0 -17
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 726d16096fd6ca31266bab9b42c4f878cba6942eada1649de7fbf946af2575f0
4
- data.tar.gz: 8ce1faffcdda9dae38ff2beddab31f0b161853c4fd9919b699cdb5e5a27f2baa
3
+ metadata.gz: 7ed2b3f49b07fddee98c7628224c50ed65147a28a27c0e1cad0234ebe38b645f
4
+ data.tar.gz: 9c2cea09cb144c08c912352d4f055d70796c6662713900382ee3672a33a90da2
5
5
  SHA512:
6
- metadata.gz: 63904bd61ec508918ee8a0d6cbe7cd089dc0b6df7c6f4d8ea1fef7538b2aea3a88962724e6a5fc486f5ec10146350e4176655b3fbeec868a66b6b3e00e767307
7
- data.tar.gz: 36fccab94cf1f3cdfa599ad0a6d74cecc1b503c80da296e73381f3b3f2130244d57a9b51fabaff5e624e7f0955442e3b0e67d7006b6b94f705fdc4249252ac12
6
+ metadata.gz: 3156e42603250f4e3a6deee15c7bf78bfe95a6f2de1c425a45e350e1f7bbe3fc366837dbcbf80b492c2dfedd815c0968e56ba9437da2434342d634bd3993155e
7
+ data.tar.gz: 3862bef940069503924101479c3c3514a20ae783658a2ae395245f5245328c5e41d5c7e485c6e2371534ccd4502bcc82cf1205846e3f550006fe2b4c79be4d3f
data/Gemfile CHANGED
@@ -2,3 +2,8 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in elasticsearch-model.gemspec
4
4
  gemspec
5
+
6
+ group :development, :testing do
7
+ gem 'rspec'
8
+ gem 'pry-nav'
9
+ end
data/README.md CHANGED
@@ -19,6 +19,7 @@ is compatible with the Elasticsearch `master` branch, therefore, with the next m
19
19
  | 0.1 | → | 1.x |
20
20
  | 2.x | → | 2.x |
21
21
  | 5.x | → | 5.x |
22
+ | 6.x | → | 6.x |
22
23
  | master | → | master |
23
24
 
24
25
  ## Installation
@@ -724,13 +725,8 @@ module and its submodules for technical information.
724
725
 
725
726
  The module provides a common `settings` method to customize various features.
726
727
 
727
- At the moment, the only supported setting is `:inheritance_enabled`, which makes the class receiving the module
728
- respect index names and document types of a super-class, eg. in case you're using "single table inheritance" (STI)
729
- in Rails:
730
-
731
- ```ruby
732
- Elasticsearch::Model.settings[:inheritance_enabled] = true
733
- ```
728
+ Before version 7.0.0 of the gem, the only supported setting was `:inheritance_enabled`. This setting has been deprecated
729
+ and removed.
734
730
 
735
731
  ## Development and Community
736
732
 
@@ -748,6 +744,17 @@ curl -# https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticse
748
744
  SERVER=start TEST_CLUSTER_COMMAND=$PWD/tmp/elasticsearch-1.0.0.RC1/bin/elasticsearch bundle exec rake test:all
749
745
  ```
750
746
 
747
+ ### Single Table Inheritance deprecation
748
+
749
+ `Single Table Inheritance` has been supported through the 6.x series of this gem. With this feature,
750
+ elasticsearch settings (index mappings, etc) on a parent model could be inherited by a child model leading to different
751
+ model documents being indexed into the same Elasticsearch index. This feature depended on the ability to set a `type`
752
+ for a document in Elasticsearch. The Elasticsearch team has deprecated support for `types`, as is described
753
+ [here.](https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html)
754
+ This gem will also remove support for types and `Single Table Inheritance` in version 7.0 as it enables an anti-pattern.
755
+ Please save different model documents in separate indices. If you want to use STI, you can include an artificial
756
+ `type` field manually in each document and use it in other operations.
757
+
751
758
  ## License
752
759
 
753
760
  This software is licensed under the Apache 2 license, quoted below.
data/Rakefile CHANGED
@@ -4,12 +4,25 @@ desc "Run unit tests"
4
4
  task :default => 'test:unit'
5
5
  task :test => 'test:unit'
6
6
 
7
- namespace :bundler do
8
- desc "Install dependencies for all the Gemfiles"
7
+ if RUBY_VERSION < '2.3'
8
+ GEMFILES = ['3.0.gemfile', '4.0.gemfile', '5.0.gemfile']
9
+ else
10
+ GEMFILES = ['4.0.gemfile', '5.0.gemfile']
11
+ end
12
+
13
+ namespace :bundle do
14
+ desc 'Install dependencies for all the Gemfiles in /gemfiles. Optionally define env variable RAILS_VERSIONS. E.g. RAILS_VERSIONS=3.0,5.0'
9
15
  task :install do
10
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle install"
11
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle install"
12
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle install"
16
+ unless defined?(JRUBY_VERSION)
17
+ puts '-'*80
18
+ gemfiles = ENV['RAILS_VERSIONS'] ? ENV['RAILS_VERSIONS'].split(',').map { |v| "#{v}.gemfile"} : GEMFILES
19
+ gemfiles.each do |gemfile|
20
+ Bundler.with_clean_env do
21
+ sh "bundle install --gemfile #{File.expand_path('../gemfiles/'+gemfile, __FILE__)}"
22
+ end
23
+ puts '-'*80
24
+ end
25
+ end
13
26
  end
14
27
  end
15
28
 
@@ -17,38 +30,16 @@ end
17
30
 
18
31
  require 'rake/testtask'
19
32
  namespace :test do
20
- Rake::TestTask.new(:run_unit) do |test|
21
- test.libs << 'lib' << 'test'
22
- test.test_files = FileList["test/unit/**/*_test.rb"]
23
- test.verbose = false
24
- test.warning = false
25
- end
26
-
27
- Rake::TestTask.new(:run_integration) do |test|
28
- test.libs << 'lib' << 'test'
29
- test.test_files = FileList["test/integration/**/*_test.rb"]
30
- test.verbose = false
31
- test.warning = false
32
- end
33
-
34
- desc "Run unit tests against ActiveModel 3, 4 and 5"
35
- task :unit do
36
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
37
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
38
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_unit"
39
- end
40
-
41
- desc "Run integration tests against latest stable ActiveModel (5)"
42
- task :integration do
43
- #sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/3.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
44
- #sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/4.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
45
- sh "BUNDLE_GEMFILE='#{File.expand_path('../gemfiles/5.0.gemfile', __FILE__)}' bundle exec rake test:run_integration"
46
- end
47
33
 
48
- desc "Run unit and integration tests"
49
- task :all do
50
- Rake::Task['test:unit'].invoke
51
- Rake::Task['test:integration'].invoke
34
+ desc 'Run all tests. Optionally define env variable RAILS_VERSIONS. E.g. RAILS_VERSIONS=3.0,5.0'
35
+ task :all, [:rails_versions] do |task, args|
36
+ gemfiles = ENV['RAILS_VERSIONS'] ? ENV['RAILS_VERSIONS'].split(',').map {|v| "#{v}.gemfile"} : GEMFILES
37
+ puts '-' * 80
38
+ gemfiles.each do |gemfile|
39
+ sh "BUNDLE_GEMFILE='#{File.expand_path("../gemfiles/#{gemfile}", __FILE__)}' " +
40
+ " bundle exec rspec"
41
+ puts '-' * 80
42
+ end
52
43
  end
53
44
  end
54
45
 
@@ -27,7 +27,7 @@ Gem::Specification.new do |s|
27
27
  s.add_dependency "activesupport", '> 3'
28
28
  s.add_dependency "hashie"
29
29
 
30
- s.add_development_dependency "bundler", "~> 1.3"
30
+ s.add_development_dependency "bundler"
31
31
  s.add_development_dependency "rake", "~> 11.1"
32
32
 
33
33
  s.add_development_dependency "elasticsearch-extensions"
@@ -50,20 +50,7 @@ response_1 = Article.search 'foo';
50
50
  puts "Article search:".ansi(:bold),
51
51
  response_1.to_a.map { |d| "Title: #{d.title}" }.inspect.ansi(:bold, :yellow)
52
52
 
53
- response_2 = Article.__elasticsearch__.client.suggest \
54
- index: Article.index_name,
55
- body: {
56
- articles: {
57
- text: 'foo',
58
- completion: { field: 'title.suggest' }
59
- }
60
- };
61
-
62
- puts "Article suggest:".ansi(:bold),
63
- response_2['articles'].first['options'].map { |d| "#{d['text']} -> #{d['_source']['url']}" }.
64
- inspect.ansi(:bold, :green)
65
-
66
- response_3 = Article.search \
53
+ response_2 = Article.search \
67
54
  query: {
68
55
  match: { title: 'foo' }
69
56
  },
@@ -76,7 +63,7 @@ response_3 = Article.search \
76
63
  _source: ['title', 'url']
77
64
 
78
65
  puts "Article search with suggest:".ansi(:bold),
79
- response_3.response['suggest']['articles'].first['options'].map { |d| "#{d['text']} -> #{d['_source']['url']}" }.
66
+ response_2.response['suggest']['articles'].first['options'].map { |d| "#{d['text']} -> #{d['_source']['url']}" }.
80
67
  inspect.ansi(:bold, :blue)
81
68
 
82
69
  require 'pry'; binding.pry;
@@ -10,4 +10,9 @@ gemspec path: '../'
10
10
  gem 'activemodel', '>= 3.0'
11
11
  gem 'activerecord', '~> 3.2'
12
12
  gem 'mongoid', '>= 3.0'
13
- gem 'sqlite3' unless defined?(JRUBY_VERSION)
13
+ gem 'sqlite3', '~> 1.3.6' unless defined?(JRUBY_VERSION)
14
+
15
+ group :development, :testing do
16
+ gem 'rspec'
17
+ gem 'pry-nav'
18
+ end
@@ -9,4 +9,10 @@ gemspec path: '../'
9
9
 
10
10
  gem 'activemodel', '~> 4'
11
11
  gem 'activerecord', '~> 4'
12
- gem 'sqlite3' unless defined?(JRUBY_VERSION)
12
+ gem 'sqlite3', '~> 1.3.6' unless defined?(JRUBY_VERSION)
13
+ gem 'mongoid', '~> 5'
14
+
15
+ group :development, :testing do
16
+ gem 'rspec'
17
+ gem 'pry-nav'
18
+ end
@@ -10,3 +10,9 @@ gemspec path: '../'
10
10
  gem 'activemodel', '~> 5'
11
11
  gem 'activerecord', '~> 5'
12
12
  gem 'sqlite3' unless defined?(JRUBY_VERSION)
13
+ gem 'mongoid', '~> 6'
14
+
15
+ group :development, :testing do
16
+ gem 'rspec'
17
+ gem 'pry-nav'
18
+ end
@@ -131,12 +131,6 @@ module Elasticsearch
131
131
  end
132
132
  end
133
133
 
134
- # Access the module settings
135
- #
136
- def self.settings
137
- @settings ||= {}
138
- end
139
-
140
134
  module ClassMethods
141
135
  # Get the client common for all models
142
136
  #
@@ -193,7 +187,7 @@ module Elasticsearch
193
187
  # @note Inheritance is disabled by default.
194
188
  #
195
189
  def inheritance_enabled
196
- @inheritance_enabled ||= false
190
+ @settings[:inheritance_enabled] ||= false
197
191
  end
198
192
 
199
193
  # Enable inheritance of index_name and document_type
@@ -203,8 +197,21 @@ module Elasticsearch
203
197
  # Elasticsearch::Model.inheritance_enabled = true
204
198
  #
205
199
  def inheritance_enabled=(inheritance_enabled)
206
- @inheritance_enabled = inheritance_enabled
200
+ warn STI_DEPRECATION_WARNING if inheritance_enabled
201
+ @settings[:inheritance_enabled] = inheritance_enabled
202
+ end
203
+
204
+ # Access the module settings
205
+ #
206
+ def settings
207
+ @settings ||= {}
207
208
  end
209
+
210
+ private
211
+
212
+ STI_DEPRECATION_WARNING = "DEPRECATION WARNING: Support for Single Table Inheritance (STI) is deprecated " +
213
+ "and will be removed in version 7.0.0.\nPlease save different model documents in separate indices and refer " +
214
+ "to the Elasticsearch documentation for more information.".freeze
208
215
  end
209
216
  extend ClassMethods
210
217
 
@@ -35,7 +35,11 @@ module Elasticsearch
35
35
  else
36
36
  self.__send__(:exec_queries)
37
37
  end
38
- @records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } }
38
+ if !self.order_values.present?
39
+ @records.sort_by { |record| hits.index { |hit| hit['_id'].to_s == record.id.to_s } }
40
+ else
41
+ @records
42
+ end
39
43
  end if self
40
44
  end
41
45
 
@@ -47,30 +51,6 @@ module Elasticsearch
47
51
  def load
48
52
  records.__send__(:load)
49
53
  end
50
-
51
- # Intercept call to the `order` method, so we can ignore the order from Elasticsearch
52
- #
53
- def order(*args)
54
- sql_records = records.__send__ :order, *args
55
-
56
- # Redefine the `to_a` method to the original one
57
- #
58
- sql_records.instance_exec do
59
- ar_records_method_name = :to_a
60
- ar_records_method_name = :records if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5
61
-
62
- define_singleton_method(ar_records_method_name) do
63
- if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
64
- self.load
65
- else
66
- self.__send__(:exec_queries)
67
- end
68
- @records
69
- end
70
- end
71
-
72
- sql_records
73
- end
74
54
  end
75
55
 
76
56
  module Callbacks
@@ -106,7 +86,8 @@ module Elasticsearch
106
86
  scope = scope.instance_exec(&query) if query
107
87
 
108
88
  scope.find_in_batches(options) do |batch|
109
- yield (preprocess ? self.__send__(preprocess, batch) : batch)
89
+ batch = self.__send__(preprocess, batch) if preprocess
90
+ yield(batch) if batch.present?
110
91
  end
111
92
  end
112
93
 
@@ -269,7 +269,8 @@ module Elasticsearch
269
269
  self.client.indices.delete index: target_index
270
270
  rescue Exception => e
271
271
  if e.class.to_s =~ /NotFound/ && options[:force]
272
- STDERR.puts "[!!!] Index does not exist (#{e.class})"
272
+ client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
273
+ nil
273
274
  else
274
275
  raise e
275
276
  end
@@ -295,7 +296,8 @@ module Elasticsearch
295
296
  self.client.indices.refresh index: target_index
296
297
  rescue Exception => e
297
298
  if e.class.to_s =~ /NotFound/ && options[:force]
298
- STDERR.puts "[!!!] Index does not exist (#{e.class})"
299
+ client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
300
+ nil
299
301
  else
300
302
  raise e
301
303
  end
@@ -409,7 +411,7 @@ module Elasticsearch
409
411
  type: document_type,
410
412
  id: self.id,
411
413
  body: { doc: attributes } }.merge(options)
412
- )
414
+ ) unless attributes.empty?
413
415
  else
414
416
  index_document(options)
415
417
  end
@@ -79,7 +79,12 @@ module Elasticsearch
79
79
 
80
80
  if Elasticsearch::Model.settings[:inheritance_enabled]
81
81
  self.ancestors.each do |klass|
82
- next if klass == self
82
+ # When Naming is included in Proxy::ClassMethods the actual model
83
+ # is among its ancestors. We don't want to call the actual model
84
+ # since it will result in the same call to the same instance of
85
+ # Proxy::ClassMethods. To prevent this we also skip the ancestor
86
+ # that is the target.
87
+ next if klass == self || self.respond_to?(:target) && klass == self.target
83
88
  break if value = klass.respond_to?(prop) && klass.send(prop)
84
89
  end
85
90
  end
@@ -60,7 +60,7 @@ module Elasticsearch
60
60
  # Returns the statistics on shards
61
61
  #
62
62
  def shards
63
- @shards ||= HashWrapper.new(raw_response['_shards'])
63
+ @shards ||= response['_shards']
64
64
  end
65
65
 
66
66
  # Returns a Hashie::Mash of the aggregations
@@ -76,7 +76,7 @@ module Elasticsearch
76
76
  end
77
77
 
78
78
  def raw_response
79
- @raw_response ||= search.execute!
79
+ @raw_response ||= @response ? @response.to_hash : search.execute!
80
80
  end
81
81
  end
82
82
  end
@@ -1,192 +1,2 @@
1
- module Elasticsearch
2
- module Model
3
- module Response
4
-
5
- # Pagination for search results/records
6
- #
7
- module Pagination
8
- # Allow models to be paginated with the "kaminari" gem [https://github.com/amatsuda/kaminari]
9
- #
10
- module Kaminari
11
- def self.included(base)
12
- # Include the Kaminari configuration and paging method in response
13
- #
14
- base.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
15
- base.__send__ :include, ::Kaminari::PageScopeMethods
16
-
17
- # Include the Kaminari paging methods in results and records
18
- #
19
- Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
20
- Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::PageScopeMethods
21
- Elasticsearch::Model::Response::Records.__send__ :include, ::Kaminari::PageScopeMethods
22
-
23
- Elasticsearch::Model::Response::Results.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
24
- Elasticsearch::Model::Response::Records.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
25
-
26
- base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
27
- # Define the `page` Kaminari method
28
- #
29
- def #{::Kaminari.config.page_method_name}(num=nil)
30
- @results = nil
31
- @records = nil
32
- @response = nil
33
- @page = [num.to_i, 1].max
34
- @per_page ||= __default_per_page
35
-
36
- self.search.definition.update size: @per_page,
37
- from: @per_page * (@page - 1)
38
-
39
- self
40
- end
41
- RUBY
42
- end
43
-
44
- # Returns the current "limit" (`size`) value
45
- #
46
- def limit_value
47
- case
48
- when search.definition[:size]
49
- search.definition[:size]
50
- else
51
- __default_per_page
52
- end
53
- end
54
-
55
- # Returns the current "offset" (`from`) value
56
- #
57
- def offset_value
58
- case
59
- when search.definition[:from]
60
- search.definition[:from]
61
- else
62
- 0
63
- end
64
- end
65
-
66
- # Set the "limit" (`size`) value
67
- #
68
- def limit(value)
69
- return self if value.to_i <= 0
70
- @results = nil
71
- @records = nil
72
- @response = nil
73
- @per_page = value.to_i
74
-
75
- search.definition.update :size => @per_page
76
- search.definition.update :from => @per_page * (@page - 1) if @page
77
- self
78
- end
79
-
80
- # Set the "offset" (`from`) value
81
- #
82
- def offset(value)
83
- return self if value.to_i < 0
84
- @results = nil
85
- @records = nil
86
- @response = nil
87
- @page = nil
88
- search.definition.update :from => value.to_i
89
- self
90
- end
91
-
92
- # Returns the total number of results
93
- #
94
- def total_count
95
- results.total
96
- end
97
-
98
- # Returns the models's `per_page` value or the default
99
- #
100
- # @api private
101
- #
102
- def __default_per_page
103
- klass.respond_to?(:default_per_page) && klass.default_per_page || ::Kaminari.config.default_per_page
104
- end
105
- end
106
-
107
- # Allow models to be paginated with the "will_paginate" gem [https://github.com/mislav/will_paginate]
108
- #
109
- module WillPaginate
110
- def self.included(base)
111
- base.__send__ :include, ::WillPaginate::CollectionMethods
112
-
113
- # Include the paging methods in results and records
114
- #
115
- methods = [:current_page, :offset, :length, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
116
- Elasticsearch::Model::Response::Results.__send__ :delegate, *methods, to: :response
117
- Elasticsearch::Model::Response::Records.__send__ :delegate, *methods, to: :response
118
- end
119
-
120
- def offset
121
- (current_page - 1) * per_page
122
- end
123
-
124
- def length
125
- search.definition[:size]
126
- end
127
-
128
- # Main pagination method
129
- #
130
- # @example
131
- #
132
- # Article.search('foo').paginate(page: 1, per_page: 30)
133
- #
134
- def paginate(options)
135
- param_name = options[:param_name] || :page
136
- page = [options[param_name].to_i, 1].max
137
- per_page = (options[:per_page] || __default_per_page).to_i
138
-
139
- search.definition.update size: per_page,
140
- from: (page - 1) * per_page
141
- self
142
- end
143
-
144
- # Return the current page
145
- #
146
- def current_page
147
- search.definition[:from] / per_page + 1 if search.definition[:from] && per_page
148
- end
149
-
150
- # Pagination method
151
- #
152
- # @example
153
- #
154
- # Article.search('foo').page(2)
155
- #
156
- def page(num)
157
- paginate(page: num, per_page: per_page) # shorthand
158
- end
159
-
160
- # Return or set the "size" value
161
- #
162
- # @example
163
- #
164
- # Article.search('foo').per_page(15).page(2)
165
- #
166
- def per_page(num = nil)
167
- if num.nil?
168
- search.definition[:size]
169
- else
170
- paginate(page: current_page, per_page: num) # shorthand
171
- end
172
- end
173
-
174
- # Returns the total number of results
175
- #
176
- def total_entries
177
- results.total
178
- end
179
-
180
- # Returns the models's `per_page` value or the default
181
- #
182
- # @api private
183
- #
184
- def __default_per_page
185
- klass.respond_to?(:per_page) && klass.per_page || ::WillPaginate.per_page
186
- end
187
- end
188
- end
189
-
190
- end
191
- end
192
- end
1
+ require 'elasticsearch/model/response/pagination/kaminari'
2
+ require 'elasticsearch/model/response/pagination/will_paginate'