elasticsearch-model 7.0.0.pre → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be320b65dde58892add536b616fa70a1bd3ad1be9f4dd4d49d3250fb62d8bb93
4
- data.tar.gz: 27386a4dea06f8ba4cbb6bf926b76315b38b4dff0df55e02a050ffdd54430afc
3
+ metadata.gz: 1ced6c99b3278104e708dc2db19a73422273f92b9c18d3aa1f0d552e0ba27d97
4
+ data.tar.gz: 9075efe50a3f7da082933e3e9acc9751901dc4561f92a70c4d3cbea000c581ee
5
5
  SHA512:
6
- metadata.gz: f47057b8d82eb6f49fe7b495806a3b35caa149ebdb3236ed9133d4faad32159e5b47db970e2d6b2488681be4dba5a6f40dc1eec7cfaaee997c428bfb1bd9bb0b
7
- data.tar.gz: b1eb060284f12d714c1a7a32cb3705aeee05eb641fb98fcb269e582eacfa28b7da8449d97fa30d6bf5ec4ad188f5f491250ba81e54bf4fa2b8358af62adac60a
6
+ metadata.gz: b8f7d0e726d805048544b8ffce8a81b269be4f17c3696241372f6258a7c9add622998718fb23c35b752cc1a51d64a0b8ef42a293b31718f6ba4ed51cfced7586
7
+ data.tar.gz: 36e3325b72bfa63f8ea133b44086d579bb8727a6e5d8c71115ab25d424b6058e849a32aa68326f5450c1f0fbe5c75fedead8878893309ffbca29901c773367be
data/README.md CHANGED
@@ -5,7 +5,7 @@ the [`elasticsearch`](https://github.com/elastic/elasticsearch-ruby) library.
5
5
 
6
6
  It aims to simplify integration of Ruby classes ("models"), commonly found
7
7
  e.g. in [Ruby on Rails](http://rubyonrails.org) applications, with the
8
- [Elasticsearch](http://www.elasticsearch.org) search and analytics engine.
8
+ [Elasticsearch](https://www.elastic.co) search and analytics engine.
9
9
 
10
10
  ## Compatibility
11
11
 
@@ -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
@@ -151,7 +152,7 @@ for information about the Ruby client API.
151
152
 
152
153
  ### Importing the data
153
154
 
154
- The first thing you'll want to do is importing your data into the index:
155
+ The first thing you'll want to do is import your data into the index:
155
156
 
156
157
  ```ruby
157
158
  Article.import
@@ -322,7 +323,7 @@ Elasticsearch::Model::Response::Response.__send__ :include, Elasticsearch::Model
322
323
  #### The Elasticsearch DSL
323
324
 
324
325
  In most situations, you'll want to pass the search definition
325
- in the Elasticsearch [domain-specific language](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl.html) to the client:
326
+ in the Elasticsearch [domain-specific language](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html) to the client:
326
327
 
327
328
  ```ruby
328
329
  response = Article.search query: { match: { title: "Fox Dogs" } },
@@ -532,7 +533,7 @@ class Indexer
532
533
  Logger = Sidekiq.logger.level == Logger::DEBUG ? Sidekiq.logger : nil
533
534
  Client = Elasticsearch::Client.new host: 'localhost:9200', logger: Logger
534
535
 
535
- def perform(operation, record_id)
536
+ def perform_async(operation, record_id)
536
537
  logger.debug [operation, "ID: #{record_id}"]
537
538
 
538
539
  case operation.to_s
@@ -732,13 +733,8 @@ module and its submodules for technical information.
732
733
 
733
734
  The module provides a common `settings` method to customize various features.
734
735
 
735
- At the moment, the only supported setting is `:inheritance_enabled`, which makes the class receiving the module
736
- respect index names and document types of a super-class, eg. in case you're using "single table inheritance" (STI)
737
- in Rails:
738
-
739
- ```ruby
740
- Elasticsearch::Model.settings[:inheritance_enabled] = true
741
- ```
736
+ Before version 7.0.0 of the gem, the only supported setting was `:inheritance_enabled`. This setting has been deprecated
737
+ and removed.
742
738
 
743
739
  ## Development and Community
744
740
 
@@ -756,20 +752,34 @@ curl -# https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticse
756
752
  SERVER=start TEST_CLUSTER_COMMAND=$PWD/tmp/elasticsearch-1.0.0.RC1/bin/elasticsearch bundle exec rake test:all
757
753
  ```
758
754
 
755
+ ### Single Table Inheritance support
756
+
757
+ Versions < 7.0.0 of this gem supported inheritance-- more specifically, `Single Table Inheritance`. With this feature,
758
+ elasticsearch settings (index mappings, etc) on a parent model could be inherited by a child model leading to different
759
+ model documents being indexed into the same Elasticsearch index. This feature depended on the ability to set a `type`
760
+ for a document in Elasticsearch. The Elasticsearch team has deprecated support for `types`, as is described
761
+ [here.](https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html)
762
+ This gem will also remove support for types and `Single Table Inheritance` in version 7.0 as it enables an anti-pattern.
763
+ Please save different model documents in separate indices. If you want to use STI, you can include an artificial
764
+ `type` field manually in each document and use it in other operations.
765
+
759
766
  ## License
760
767
 
761
768
  This software is licensed under the Apache 2 license, quoted below.
762
769
 
763
- Copyright (c) 2014 Elasticsearch <http://www.elasticsearch.org>
764
-
765
- Licensed under the Apache License, Version 2.0 (the "License");
766
- you may not use this file except in compliance with the License.
770
+ Licensed to Elasticsearch B.V. under one or more contributor
771
+ license agreements. See the NOTICE file distributed with
772
+ this work for additional information regarding copyright
773
+ ownership. Elasticsearch B.V. licenses this file to you under
774
+ the Apache License, Version 2.0 (the "License"); you may
775
+ not use this file except in compliance with the License.
767
776
  You may obtain a copy of the License at
768
-
769
- http://www.apache.org/licenses/LICENSE-2.0
770
-
771
- Unless required by applicable law or agreed to in writing, software
772
- distributed under the License is distributed on an "AS IS" BASIS,
773
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
774
- See the License for the specific language governing permissions and
775
- limitations under the License.
777
+
778
+ http://www.apache.org/licenses/LICENSE-2.0
779
+
780
+ Unless required by applicable law or agreed to in writing,
781
+ software distributed under the License is distributed on an
782
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
783
+ KIND, either express or implied. See the License for the
784
+ specific language governing permissions and limitations
785
+ under the License.
@@ -98,6 +98,22 @@ module Searchable
98
98
 
99
99
  module Indexing
100
100
 
101
+ #Index only the specified fields
102
+ settings do
103
+ mappings dynamic: false do
104
+ indexes :categories, type: :object do
105
+ indexes :title
106
+ end
107
+ indexes :authors, type: :object do
108
+ indexes :full_name
109
+ indexes :department
110
+ end
111
+ indexes :comments, type: :object do
112
+ indexes :text
113
+ end
114
+ end
115
+ end
116
+
101
117
  # Customize the JSON serialization for Elasticsearch
102
118
  def as_indexed_json(options={})
103
119
  self.as_json(
@@ -25,10 +25,10 @@ source 'https://rubygems.org'
25
25
 
26
26
  gemspec path: '../'
27
27
 
28
- gem 'activemodel', '6.0.0.rc1'
29
- gem 'activerecord', '6.0.0.rc1'
28
+ gem 'activemodel', '6.0.0'
29
+ gem 'activerecord', '6.0.0'
30
30
  gem 'sqlite3' unless defined?(JRUBY_VERSION)
31
- gem 'mongoid', '~> 6'
31
+ #gem 'mongoid', '~> 6'
32
32
 
33
33
  group :development, :testing do
34
34
  gem 'rspec'
@@ -89,9 +89,10 @@ module Elasticsearch
89
89
 
90
90
  # Adds the `Elasticsearch::Model` functionality to the including class.
91
91
  #
92
- # * Creates the `__elasticsearch__` class and instance methods, pointing to the proxy object
93
- # * Includes the necessary modules in the proxy classes
94
- # * Sets up delegation for crucial methods such as `search`, etc.
92
+ # * Creates the `__elasticsearch__` class and instance method. These methods return a proxy object with
93
+ # other common methods defined on them.
94
+ # * The module includes other modules with further functionality.
95
+ # * Sets up delegation for common methods such as `import` and `search`.
95
96
  #
96
97
  # @example Include the module in the `Article` model definition
97
98
  #
@@ -108,50 +109,16 @@ module Elasticsearch
108
109
  base.class_eval do
109
110
  include Elasticsearch::Model::Proxy
110
111
 
111
- Elasticsearch::Model::Proxy::ClassMethodsProxy.class_eval do
112
- include Elasticsearch::Model::Client::ClassMethods
113
- include Elasticsearch::Model::Naming::ClassMethods
114
- include Elasticsearch::Model::Indexing::ClassMethods
115
- include Elasticsearch::Model::Searching::ClassMethods
116
- end
117
-
118
- Elasticsearch::Model::Proxy::InstanceMethodsProxy.class_eval do
119
- include Elasticsearch::Model::Client::InstanceMethods
120
- include Elasticsearch::Model::Naming::InstanceMethods
121
- include Elasticsearch::Model::Indexing::InstanceMethods
122
- include Elasticsearch::Model::Serializing::InstanceMethods
123
- end
124
-
125
- Elasticsearch::Model::Proxy::InstanceMethodsProxy.class_eval <<-CODE, __FILE__, __LINE__ + 1
126
- def as_indexed_json(options={})
127
- target.respond_to?(:as_indexed_json) ? target.__send__(:as_indexed_json, options) : super
128
- end
129
- CODE
130
-
131
- # Delegate important methods to the `__elasticsearch__` proxy, unless they are defined already
132
- #
112
+ # Delegate common methods to the `__elasticsearch__` ClassMethodsProxy, unless they are defined already
133
113
  class << self
134
114
  METHODS.each do |method|
135
- delegate method, to: :__elasticsearch__ unless self.public_instance_methods.include?(method)
115
+ delegate method, to: :__elasticsearch__ unless self.respond_to?(method)
136
116
  end
137
117
  end
138
-
139
- # Mix the importing module into the proxy
140
- #
141
- self.__elasticsearch__.class_eval do
142
- include Elasticsearch::Model::Importing::ClassMethods
143
- include Adapter.from_class(base).importing_mixin
144
- end
145
-
146
- # Add to the registry if it's a class (and not in intermediate module)
147
- Registry.add(base) if base.is_a?(Class)
148
118
  end
149
- end
150
119
 
151
- # Access the module settings
152
- #
153
- def self.settings
154
- @settings ||= {}
120
+ # Add to the model to the registry if it's a class (and not in intermediate module)
121
+ Registry.add(base) if base.is_a?(Class)
155
122
  end
156
123
 
157
124
  module ClassMethods
@@ -205,22 +172,10 @@ module Elasticsearch
205
172
  Response::Response.new(models, request)
206
173
  end
207
174
 
208
- # Check if inheritance is enabled
209
- #
210
- # @note Inheritance is disabled by default.
211
- #
212
- def inheritance_enabled
213
- @inheritance_enabled ||= false
214
- end
215
-
216
- # Enable inheritance of index_name and document_type
217
- #
218
- # @example Enable inheritance
219
- #
220
- # Elasticsearch::Model.inheritance_enabled = true
175
+ # Access the module settings
221
176
  #
222
- def inheritance_enabled=(inheritance_enabled)
223
- @inheritance_enabled = inheritance_enabled
177
+ def settings
178
+ @settings ||= {}
224
179
  end
225
180
  end
226
181
  extend ClassMethods
@@ -53,7 +53,29 @@ module Elasticsearch
53
53
  #
54
54
  # @yield [Hash] Gives the Hash with the Elasticsearch response to the block
55
55
  #
56
- # @return [Fixnum] Number of errors encountered during importing
56
+ # @return [Fixnum] default, number of errors encountered during importing
57
+ # @return [Array<Hash>] if +return+ option is specified to be +"errors"+,
58
+ # contains only those failed items in the response +items+ key, e.g.:
59
+ #
60
+ # [
61
+ # {
62
+ # "index" => {
63
+ # "error" => 'FAILED',
64
+ # "_index" => "test",
65
+ # "_type" => "_doc",
66
+ # "_id" => '1',
67
+ # "_version" => 1,
68
+ # "result" => "foo",
69
+ # "_shards" => {
70
+ # "total" => 1,
71
+ # "successful" => 0,
72
+ # "failed" => 1
73
+ # },
74
+ # "status" => 400
75
+ # }
76
+ # }
77
+ # ]
78
+ #
57
79
  #
58
80
  # @example Import all records into the index
59
81
  #
@@ -99,20 +121,19 @@ module Elasticsearch
99
121
  #
100
122
  # @example Update the batch before yielding it
101
123
  #
102
- # class Article
103
- # # ...
104
- # def self.enrich(batch)
105
- # batch.each do |item|
106
- # item.metadata = MyAPI.get_metadata(item.id)
107
- # end
108
- # batch
109
- # end
110
- # end
124
+ # class Article
125
+ # # ...
126
+ # def self.enrich(batch)
127
+ # batch.each do |item|
128
+ # item.metadata = MyAPI.get_metadata(item.id)
129
+ # end
130
+ # batch
131
+ # end
132
+ # end
111
133
  #
112
134
  # Article.import preprocess: :enrich
113
135
  #
114
- # @example Return an array of error elements instead of the number of errors, eg.
115
- # to try importing these records again
136
+ # @example Return an array of error elements instead of the number of errors, e.g. to try importing these records again
116
137
  #
117
138
  # Article.import return: 'errors'
118
139
  #
@@ -30,7 +30,7 @@ module Elasticsearch
30
30
  #
31
31
  module Indexing
32
32
 
33
- # Wraps the [index settings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/setup-configuration.html#configuration-index-settings)
33
+ # Wraps the [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)
34
34
  #
35
35
  class Settings
36
36
  attr_accessor :settings
@@ -48,7 +48,7 @@ module Elasticsearch
48
48
  end
49
49
  end
50
50
 
51
- # Wraps the [index mappings](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/mapping.html)
51
+ # Wraps the [index mappings](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html)
52
52
  #
53
53
  class Mappings
54
54
  attr_accessor :options, :type
@@ -308,7 +308,7 @@ module Elasticsearch
308
308
  #
309
309
  # Article.__elasticsearch__.refresh_index! index: 'my-index'
310
310
  #
311
- # @see http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-refresh.html
311
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
312
312
  #
313
313
  def refresh_index!(options={})
314
314
  target_index = options.delete(:index) || self.index_name
@@ -338,13 +338,17 @@ module Elasticsearch
338
338
  #
339
339
  # @see #update_document
340
340
  #
341
- base.before_save do |i|
342
- if i.class.instance_methods.include?(:changes_to_save) # Rails 5.1
343
- i.instance_variable_set(:@__changed_model_attributes,
344
- Hash[ i.changes_to_save.map { |key, value| [key, value.last] } ])
345
- elsif i.class.instance_methods.include?(:changes)
346
- i.instance_variable_set(:@__changed_model_attributes,
347
- Hash[ i.changes.map { |key, value| [key, value.last] } ])
341
+ base.before_save do |obj|
342
+ if obj.respond_to?(:changes_to_save) # Rails 5.1
343
+ changes_to_save = obj.changes_to_save
344
+ elsif obj.respond_to?(:changes)
345
+ changes_to_save = obj.changes
346
+ end
347
+
348
+ if changes_to_save
349
+ attrs = obj.instance_variable_get(:@__changed_model_attributes) || {}
350
+ latest_changes = changes_to_save.inject({}) { |latest_changes, (k,v)| latest_changes.merge!(k => v.last) }
351
+ obj.instance_variable_set(:@__changed_model_attributes, attrs.merge(latest_changes))
348
352
  end
349
353
  end if base.respond_to?(:before_save)
350
354
  end
@@ -364,14 +368,13 @@ module Elasticsearch
364
368
  # @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:index
365
369
  #
366
370
  def index_document(options={})
367
- document = self.as_indexed_json
368
-
369
- client.index(
370
- { index: index_name,
371
- type: document_type,
372
- id: self.id,
373
- body: document }.merge(options)
374
- )
371
+ document = as_indexed_json
372
+ request = { index: index_name,
373
+ id: id,
374
+ body: document }
375
+ request.merge!(type: document_type) if document_type
376
+
377
+ client.index(request.merge!(options))
375
378
  end
376
379
 
377
380
  # Deletes the model instance from the index
@@ -388,11 +391,11 @@ module Elasticsearch
388
391
  # @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:delete
389
392
  #
390
393
  def delete_document(options={})
391
- client.delete(
392
- { index: index_name,
393
- type: document_type,
394
- id: self.id }.merge(options)
395
- )
394
+ request = { index: index_name,
395
+ id: self.id }
396
+ request.merge!(type: document_type) if document_type
397
+
398
+ client.delete(request.merge!(options))
396
399
  end
397
400
 
398
401
  # Tries to gather the changed attributes of a model instance
@@ -427,12 +430,14 @@ module Elasticsearch
427
430
  attributes_in_database
428
431
  end
429
432
 
430
- client.update(
431
- { index: index_name,
432
- type: document_type,
433
- id: self.id,
434
- body: { doc: attributes } }.merge(options)
435
- ) unless attributes.empty?
433
+ unless attributes.empty?
434
+ request = { index: index_name,
435
+ id: self.id,
436
+ body: { doc: attributes } }
437
+ request.merge!(type: document_type) if document_type
438
+
439
+ client.update(request.merge!(options))
440
+ end
436
441
  else
437
442
  index_document(options)
438
443
  end
@@ -453,12 +458,12 @@ module Elasticsearch
453
458
  # @return [Hash] The response from Elasticsearch
454
459
  #
455
460
  def update_document_attributes(attributes, options={})
456
- client.update(
457
- { index: index_name,
458
- type: document_type,
459
- id: self.id,
460
- body: { doc: attributes } }.merge(options)
461
- )
461
+ request = { index: index_name,
462
+ id: self.id,
463
+ body: { doc: attributes } }
464
+ request.merge!(type: document_type) if document_type
465
+
466
+ client.update(request.merge!(options))
462
467
  end
463
468
  end
464
469
 
@@ -92,16 +92,7 @@ module Elasticsearch
92
92
  private
93
93
 
94
94
  def implicit(prop)
95
- value = nil
96
-
97
- if Elasticsearch::Model.settings[:inheritance_enabled]
98
- self.ancestors.each do |klass|
99
- next if klass == self
100
- break if value = klass.respond_to?(prop) && klass.send(prop)
101
- end
102
- end
103
-
104
- value || self.send("default_#{prop}")
95
+ self.send("default_#{prop}")
105
96
  end
106
97
 
107
98
  def default_index_name
@@ -19,15 +19,15 @@ module Elasticsearch
19
19
  module Model
20
20
 
21
21
  # This module provides a proxy interfacing between the including class and
22
- # {Elasticsearch::Model}, preventing the pollution of the including class namespace.
22
+ # `Elasticsearch::Model`, preventing the pollution of the including class namespace.
23
23
  #
24
24
  # The only "gateway" between the model and Elasticsearch::Model is the
25
- # `__elasticsearch__` class and instance method.
25
+ # `#__elasticsearch__` class and instance method.
26
26
  #
27
27
  # The including class must be compatible with
28
28
  # [ActiveModel](https://github.com/rails/rails/tree/master/activemodel).
29
29
  #
30
- # @example Include the {Elasticsearch::Model} module into an `Article` model
30
+ # @example Include the `Elasticsearch::Model` module into an `Article` model
31
31
  #
32
32
  # class Article < ActiveRecord::Base
33
33
  # include Elasticsearch::Model
@@ -53,21 +53,19 @@ module Elasticsearch
53
53
  # module and the functionality is accessible via the proxy.
54
54
  #
55
55
  def self.included(base)
56
+
56
57
  base.class_eval do
57
- # {ClassMethodsProxy} instance, accessed as `MyModel.__elasticsearch__`
58
- #
58
+
59
+ # `ClassMethodsProxy` instance, accessed as `MyModel.__elasticsearch__`
59
60
  def self.__elasticsearch__ &block
60
61
  @__elasticsearch__ ||= ClassMethodsProxy.new(self)
61
62
  @__elasticsearch__.instance_eval(&block) if block_given?
62
63
  @__elasticsearch__
63
64
  end
64
65
 
65
- # {InstanceMethodsProxy}, accessed as `@mymodel.__elasticsearch__`
66
- #
67
- def __elasticsearch__ &block
68
- @__elasticsearch__ ||= InstanceMethodsProxy.new(self)
69
- @__elasticsearch__.instance_eval(&block) if block_given?
70
- @__elasticsearch__
66
+ # Mix the importing module into the `ClassMethodsProxy`
67
+ self.__elasticsearch__.class_eval do
68
+ include Adapter.from_class(base).importing_mixin
71
69
  end
72
70
 
73
71
  # Register a callback for storing changed attributes for models which implement
@@ -75,18 +73,28 @@ module Elasticsearch
75
73
  #
76
74
  # @see http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
77
75
  #
78
- before_save do |i|
79
- if i.class.instance_methods.include?(:changes_to_save) # Rails 5.1
80
- a = i.__elasticsearch__.instance_variable_get(:@__changed_model_attributes) || {}
81
- i.__elasticsearch__.instance_variable_set(:@__changed_model_attributes,
82
- a.merge(Hash[ i.changes_to_save.map { |key, value| [key, value.last] } ]))
83
- elsif i.class.instance_methods.include?(:changes)
84
- a = i.__elasticsearch__.instance_variable_get(:@__changed_model_attributes) || {}
85
- i.__elasticsearch__.instance_variable_set(:@__changed_model_attributes,
86
- a.merge(Hash[ i.changes.map { |key, value| [key, value.last] } ]))
76
+ before_save do |obj|
77
+ if obj.respond_to?(:changes_to_save) # Rails 5.1
78
+ changes_to_save = obj.changes_to_save
79
+ elsif obj.respond_to?(:changes)
80
+ changes_to_save = obj.changes
81
+ end
82
+
83
+ if changes_to_save
84
+ attrs = obj.__elasticsearch__.instance_variable_get(:@__changed_model_attributes) || {}
85
+ latest_changes = changes_to_save.inject({}) { |latest_changes, (k,v)| latest_changes.merge!(k => v.last) }
86
+ obj.__elasticsearch__.instance_variable_set(:@__changed_model_attributes, attrs.merge(latest_changes))
87
87
  end
88
88
  end if respond_to?(:before_save)
89
89
  end
90
+
91
+ # {InstanceMethodsProxy}, accessed as `@mymodel.__elasticsearch__`
92
+ #
93
+ def __elasticsearch__ &block
94
+ @__elasticsearch__ ||= InstanceMethodsProxy.new(self)
95
+ @__elasticsearch__.instance_eval(&block) if block_given?
96
+ @__elasticsearch__
97
+ end
90
98
  end
91
99
 
92
100
  # @overload dup
@@ -130,6 +138,11 @@ module Elasticsearch
130
138
  #
131
139
  class ClassMethodsProxy
132
140
  include Base
141
+ include Elasticsearch::Model::Client::ClassMethods
142
+ include Elasticsearch::Model::Naming::ClassMethods
143
+ include Elasticsearch::Model::Indexing::ClassMethods
144
+ include Elasticsearch::Model::Searching::ClassMethods
145
+ include Elasticsearch::Model::Importing::ClassMethods
133
146
  end
134
147
 
135
148
  # A proxy interfacing between Elasticsearch::Model instance methods and model instance methods
@@ -138,6 +151,10 @@ module Elasticsearch
138
151
  #
139
152
  class InstanceMethodsProxy
140
153
  include Base
154
+ include Elasticsearch::Model::Client::InstanceMethods
155
+ include Elasticsearch::Model::Naming::InstanceMethods
156
+ include Elasticsearch::Model::Indexing::InstanceMethods
157
+ include Elasticsearch::Model::Serializing::InstanceMethods
141
158
 
142
159
  def klass
143
160
  target.class
@@ -153,8 +170,11 @@ module Elasticsearch
153
170
  def as_json(options={})
154
171
  target.as_json(options)
155
172
  end
156
- end
157
173
 
174
+ def as_indexed_json(options={})
175
+ target.respond_to?(:as_indexed_json) ? target.__send__(:as_indexed_json, options) : super
176
+ end
177
+ end
158
178
  end
159
179
  end
160
180
  end
@@ -45,7 +45,6 @@ module Elasticsearch
45
45
  metaclass.__send__ :include, adapter.records_mixin
46
46
 
47
47
  self.options = options
48
- self
49
48
  end
50
49
 
51
50
  # Returns the hit IDs
@@ -17,6 +17,6 @@
17
17
 
18
18
  module Elasticsearch
19
19
  module Model
20
- VERSION = "7.0.0.pre"
20
+ VERSION = "7.0.0"
21
21
  end
22
22
  end
@@ -418,7 +418,7 @@ describe Elasticsearch::Model::Indexing do
418
418
  expect(instance).to receive(:client).and_return(client)
419
419
  expect(instance).to receive(:as_indexed_json).and_return('JSON')
420
420
  expect(instance).to receive(:index_name).and_return('foo')
421
- expect(instance).to receive(:document_type).and_return('bar')
421
+ expect(instance).to receive(:document_type).twice.and_return('bar')
422
422
  expect(instance).to receive(:id).and_return('1')
423
423
  end
424
424
 
@@ -458,7 +458,7 @@ describe Elasticsearch::Model::Indexing do
458
458
  before do
459
459
  expect(instance).to receive(:client).and_return(client)
460
460
  expect(instance).to receive(:index_name).and_return('foo')
461
- expect(instance).to receive(:document_type).and_return('bar')
461
+ expect(instance).to receive(:document_type).twice.and_return('bar')
462
462
  expect(instance).to receive(:id).and_return('1')
463
463
  end
464
464
 
@@ -602,7 +602,7 @@ describe Elasticsearch::Model::Indexing do
602
602
  before do
603
603
  expect(instance).to receive(:client).and_return(client)
604
604
  expect(instance).to receive(:index_name).and_return('foo')
605
- expect(instance).to receive(:document_type).and_return('bar')
605
+ expect(instance).to receive(:document_type).twice.and_return('bar')
606
606
  expect(instance).to receive(:id).and_return('1')
607
607
  instance.instance_variable_set(:@__changed_model_attributes, { author: 'john' })
608
608
  end
@@ -23,7 +23,11 @@ require 'will_paginate/collection'
23
23
  require 'elasticsearch/model'
24
24
  require 'hashie/version'
25
25
  require 'active_model'
26
- require 'mongoid'
26
+ begin
27
+ require 'mongoid'
28
+ rescue LoadError
29
+ $stderr.puts("'mongoid' gem could not be loaded")
30
+ end
27
31
  require 'yaml'
28
32
  require 'active_record'
29
33
 
@@ -151,8 +155,10 @@ def test_mongoid?
151
155
  client.database.command(ping: 1) && true
152
156
  end
153
157
  end and true
154
- rescue Timeout::Error, LoadError, Mongo::Error => e
155
- client.close
158
+ rescue LoadError
159
+ $stderr.puts("'mongoid' gem could not be loaded")
160
+ rescue Timeout::Error, Mongo::Error => e
161
+ client.close if client
156
162
  $stderr.puts("MongoDB not installed or running: #{e}")
157
163
  end
158
164
  end
@@ -26,9 +26,7 @@ require 'support/app/namespaced_book'
26
26
  require 'support/app/article_for_pagination'
27
27
  require 'support/app/article_with_dynamic_index_name'
28
28
  require 'support/app/episode'
29
- require 'support/app/image'
30
29
  require 'support/app/series'
31
- require 'support/app/mongoid_article'
32
30
  require 'support/app/article'
33
31
  require 'support/app/article_no_type'
34
32
  require 'support/app/searchable'
@@ -37,3 +35,12 @@ require 'support/app/author'
37
35
  require 'support/app/authorship'
38
36
  require 'support/app/comment'
39
37
  require 'support/app/post'
38
+
39
+
40
+ # Mongoid models
41
+ begin
42
+ require 'support/app/image'
43
+ require 'support/app/mongoid_article'
44
+ rescue
45
+ $stderr.puts("'mongoid' gem is not installed, could not load Mongoid models")
46
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticsearch-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.0.pre
4
+ version: 7.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Karel Minarik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-30 00:00:00.000000000 Z
11
+ date: 2019-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: elasticsearch
@@ -399,7 +399,6 @@ files:
399
399
  - spec/elasticsearch/model/indexing_spec.rb
400
400
  - spec/elasticsearch/model/module_spec.rb
401
401
  - spec/elasticsearch/model/multimodel_spec.rb
402
- - spec/elasticsearch/model/naming_inheritance_spec.rb
403
402
  - spec/elasticsearch/model/naming_spec.rb
404
403
  - spec/elasticsearch/model/proxy_spec.rb
405
404
  - spec/elasticsearch/model/response/aggregations_spec.rb
@@ -453,11 +452,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
453
452
  version: 1.9.3
454
453
  required_rubygems_version: !ruby/object:Gem::Requirement
455
454
  requirements:
456
- - - ">"
455
+ - - ">="
457
456
  - !ruby/object:Gem::Version
458
- version: 1.3.1
457
+ version: '0'
459
458
  requirements: []
460
- rubygems_version: 3.0.3
459
+ rubygems_version: 3.0.6
461
460
  signing_key:
462
461
  specification_version: 4
463
462
  summary: ActiveModel/Record integrations for Elasticsearch.
@@ -485,7 +484,6 @@ test_files:
485
484
  - spec/elasticsearch/model/indexing_spec.rb
486
485
  - spec/elasticsearch/model/module_spec.rb
487
486
  - spec/elasticsearch/model/multimodel_spec.rb
488
- - spec/elasticsearch/model/naming_inheritance_spec.rb
489
487
  - spec/elasticsearch/model/naming_spec.rb
490
488
  - spec/elasticsearch/model/proxy_spec.rb
491
489
  - spec/elasticsearch/model/response/aggregations_spec.rb
@@ -1,123 +0,0 @@
1
- # Licensed to Elasticsearch B.V. under one or more contributor
2
- # license agreements. See the NOTICE file distributed with
3
- # this work for additional information regarding copyright
4
- # ownership. Elasticsearch B.V. licenses this file to you under
5
- # the Apache License, Version 2.0 (the "License"); you may
6
- # not use this file except in compliance with the License.
7
- # You may obtain a copy of the License at
8
- #
9
- # http://www.apache.org/licenses/LICENSE-2.0
10
- #
11
- # Unless required by applicable law or agreed to in writing,
12
- # software distributed under the License is distributed on an
13
- # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- # KIND, either express or implied. See the License for the
15
- # specific language governing permissions and limitations
16
- # under the License.
17
-
18
- require 'spec_helper'
19
-
20
- describe 'naming inheritance' do
21
-
22
- before(:all) do
23
- class ::TestBase
24
- extend ActiveModel::Naming
25
-
26
- extend Elasticsearch::Model::Naming::ClassMethods
27
- include Elasticsearch::Model::Naming::InstanceMethods
28
- end
29
-
30
- class ::Animal < ::TestBase
31
- extend ActiveModel::Naming
32
-
33
- extend Elasticsearch::Model::Naming::ClassMethods
34
- include Elasticsearch::Model::Naming::InstanceMethods
35
-
36
- index_name "mammals"
37
- document_type "mammal"
38
- end
39
-
40
- class ::Dog < ::Animal
41
- end
42
-
43
- module ::MyNamespace
44
- class Dog < ::Animal
45
- end
46
- end
47
-
48
- class ::Cat < ::Animal
49
- extend ActiveModel::Naming
50
-
51
- extend Elasticsearch::Model::Naming::ClassMethods
52
- include Elasticsearch::Model::Naming::InstanceMethods
53
-
54
- index_name "cats"
55
- document_type "cat"
56
- end
57
-
58
- end
59
-
60
- after(:all) do
61
- remove_classes(TestBase, Animal, MyNamespace, Cat)
62
- end
63
-
64
- around(:all) do |example|
65
- original_value = Elasticsearch::Model.settings[:inheritance_enabled]
66
- Elasticsearch::Model.settings[:inheritance_enabled] = true
67
- example.run
68
- Elasticsearch::Model.settings[:inheritance_enabled] = original_value
69
- end
70
-
71
-
72
- describe '#index_name' do
73
-
74
- it 'returns the default index name' do
75
- expect(TestBase.index_name).to eq('test_bases')
76
- expect(TestBase.new.index_name).to eq('test_bases')
77
- end
78
-
79
- it 'returns the explicit index name' do
80
- expect(Animal.index_name).to eq('mammals')
81
- expect(Animal.new.index_name).to eq('mammals')
82
-
83
- expect(Cat.index_name).to eq('cats')
84
- expect(Cat.new.index_name).to eq('cats')
85
- end
86
-
87
- it 'returns the ancestor index name' do
88
- expect(Dog.index_name).to eq('mammals')
89
- expect(Dog.new.index_name).to eq('mammals')
90
- end
91
-
92
- it 'returns the ancestor index name for namespaced models' do
93
- expect(::MyNamespace::Dog.index_name).to eq('mammals')
94
- expect(::MyNamespace::Dog.new.index_name).to eq('mammals')
95
- end
96
- end
97
-
98
- describe '#document_type' do
99
-
100
- it 'returns nil' do
101
- expect(TestBase.document_type).to be_nil
102
- expect(TestBase.new.document_type).to be_nil
103
- end
104
-
105
- it 'returns the explicit document type' do
106
- expect(Animal.document_type).to eq('mammal')
107
- expect(Animal.new.document_type).to eq('mammal')
108
-
109
- expect(Cat.document_type).to eq('cat')
110
- expect(Cat.new.document_type).to eq('cat')
111
- end
112
-
113
- it 'returns the ancestor document type' do
114
- expect(Dog.document_type).to eq('mammal')
115
- expect(Dog.new.document_type).to eq('mammal')
116
- end
117
-
118
- it 'returns the ancestor document type for namespaced models' do
119
- expect(::MyNamespace::Dog.document_type).to eq('mammal')
120
- expect(::MyNamespace::Dog.new.document_type).to eq('mammal')
121
- end
122
- end
123
- end