elasticsearch-model 6.1.1 → 7.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +3 -1
- data/Gemfile +18 -1
- data/LICENSE.txt +199 -10
- data/README.md +44 -45
- data/Rakefile +24 -10
- data/elasticsearch-model.gemspec +54 -39
- data/examples/activerecord_article.rb +17 -0
- data/examples/activerecord_associations.rb +33 -0
- data/examples/activerecord_custom_analyzer.rb +17 -0
- data/examples/activerecord_mapping_completion.rb +17 -0
- data/examples/activerecord_mapping_edge_ngram.rb +17 -0
- data/examples/couchbase_article.rb +17 -0
- data/examples/datamapper_article.rb +17 -0
- data/examples/mongoid_article.rb +17 -0
- data/examples/ohm_article.rb +17 -0
- data/examples/riak_article.rb +17 -0
- data/gemfiles/3.0.gemfile +18 -1
- data/gemfiles/4.0.gemfile +18 -1
- data/gemfiles/5.0.gemfile +18 -1
- data/gemfiles/6.0.gemfile +21 -3
- data/lib/elasticsearch/model.rb +25 -60
- data/lib/elasticsearch/model/adapter.rb +17 -0
- data/lib/elasticsearch/model/adapters/active_record.rb +17 -0
- data/lib/elasticsearch/model/adapters/default.rb +17 -0
- data/lib/elasticsearch/model/adapters/mongoid.rb +17 -0
- data/lib/elasticsearch/model/adapters/multiple.rb +29 -4
- data/lib/elasticsearch/model/callbacks.rb +17 -0
- data/lib/elasticsearch/model/client.rb +17 -0
- data/lib/elasticsearch/model/ext/active_record.rb +17 -0
- data/lib/elasticsearch/model/hash_wrapper.rb +17 -0
- data/lib/elasticsearch/model/importing.rb +60 -16
- data/lib/elasticsearch/model/indexing.rb +69 -43
- data/lib/elasticsearch/model/multimodel.rb +17 -0
- data/lib/elasticsearch/model/naming.rb +19 -19
- data/lib/elasticsearch/model/proxy.rb +58 -21
- data/lib/elasticsearch/model/response.rb +17 -0
- data/lib/elasticsearch/model/response/aggregations.rb +17 -0
- data/lib/elasticsearch/model/response/base.rb +22 -1
- data/lib/elasticsearch/model/response/pagination.rb +17 -0
- data/lib/elasticsearch/model/response/pagination/kaminari.rb +17 -0
- data/lib/elasticsearch/model/response/pagination/will_paginate.rb +17 -0
- data/lib/elasticsearch/model/response/records.rb +17 -1
- data/lib/elasticsearch/model/response/result.rb +17 -0
- data/lib/elasticsearch/model/response/results.rb +17 -0
- data/lib/elasticsearch/model/response/suggestions.rb +17 -0
- data/lib/elasticsearch/model/searching.rb +17 -0
- data/lib/elasticsearch/model/serializing.rb +17 -0
- data/lib/elasticsearch/model/version.rb +18 -1
- data/spec/elasticsearch/model/adapter_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +281 -226
- data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +18 -12
- data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +18 -1
- data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +18 -1
- data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/active_record_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/default_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +28 -11
- data/spec/elasticsearch/model/adapters/mongoid_spec.rb +17 -0
- data/spec/elasticsearch/model/adapters/multiple_spec.rb +17 -0
- data/spec/elasticsearch/model/callbacks_spec.rb +17 -0
- data/spec/elasticsearch/model/client_spec.rb +17 -0
- data/spec/elasticsearch/model/hash_wrapper_spec.rb +17 -0
- data/spec/elasticsearch/model/importing_spec.rb +29 -0
- data/spec/elasticsearch/model/indexing_spec.rb +111 -15
- data/spec/elasticsearch/model/module_spec.rb +18 -25
- data/spec/elasticsearch/model/multimodel_spec.rb +17 -0
- data/spec/elasticsearch/model/naming_spec.rb +22 -5
- data/spec/elasticsearch/model/proxy_spec.rb +17 -0
- data/spec/elasticsearch/model/response/aggregations_spec.rb +17 -0
- data/spec/elasticsearch/model/response/base_spec.rb +17 -0
- data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +85 -23
- data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +17 -0
- data/spec/elasticsearch/model/response/records_spec.rb +17 -0
- data/spec/elasticsearch/model/response/response_spec.rb +17 -0
- data/spec/elasticsearch/model/response/result_spec.rb +17 -0
- data/spec/elasticsearch/model/response/results_spec.rb +17 -0
- data/spec/elasticsearch/model/searching_search_request_spec.rb +17 -0
- data/spec/elasticsearch/model/searching_spec.rb +17 -0
- data/spec/elasticsearch/model/serializing_spec.rb +17 -0
- data/spec/spec_helper.rb +37 -5
- data/spec/support/app.rb +27 -2
- data/spec/support/app/answer.rb +17 -0
- data/spec/support/app/article.rb +17 -0
- data/spec/support/app/article_for_pagination.rb +17 -0
- data/spec/support/app/article_no_type.rb +37 -0
- data/spec/support/app/article_with_custom_serialization.rb +17 -0
- data/spec/support/app/article_with_dynamic_index_name.rb +17 -0
- data/spec/support/app/author.rb +17 -0
- data/spec/support/app/authorship.rb +17 -0
- data/spec/support/app/category.rb +17 -0
- data/spec/support/app/comment.rb +17 -0
- data/spec/support/app/episode.rb +17 -0
- data/spec/support/app/image.rb +17 -0
- data/spec/support/app/import_article.rb +17 -0
- data/spec/support/app/mongoid_article.rb +17 -0
- data/spec/support/app/namespaced_book.rb +17 -0
- data/spec/support/app/parent_and_child_searchable.rb +21 -4
- data/spec/support/app/post.rb +17 -0
- data/spec/support/app/question.rb +17 -0
- data/spec/support/app/searchable.rb +17 -0
- data/spec/support/app/series.rb +17 -0
- metadata +46 -46
- data/spec/elasticsearch/model/naming_inheritance_spec.rb +0 -184
data/lib/elasticsearch/model.rb
CHANGED
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
require 'hashie/mash'
|
2
19
|
|
3
20
|
require 'active_support/core_ext/module/delegation'
|
@@ -72,9 +89,10 @@ module Elasticsearch
|
|
72
89
|
|
73
90
|
# Adds the `Elasticsearch::Model` functionality to the including class.
|
74
91
|
#
|
75
|
-
# * Creates the `__elasticsearch__` class and instance methods
|
76
|
-
#
|
77
|
-
# *
|
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`.
|
78
96
|
#
|
79
97
|
# @example Include the module in the `Article` model definition
|
80
98
|
#
|
@@ -91,44 +109,16 @@ module Elasticsearch
|
|
91
109
|
base.class_eval do
|
92
110
|
include Elasticsearch::Model::Proxy
|
93
111
|
|
94
|
-
|
95
|
-
include Elasticsearch::Model::Client::ClassMethods
|
96
|
-
include Elasticsearch::Model::Naming::ClassMethods
|
97
|
-
include Elasticsearch::Model::Indexing::ClassMethods
|
98
|
-
include Elasticsearch::Model::Searching::ClassMethods
|
99
|
-
end
|
100
|
-
|
101
|
-
Elasticsearch::Model::Proxy::InstanceMethodsProxy.class_eval do
|
102
|
-
include Elasticsearch::Model::Client::InstanceMethods
|
103
|
-
include Elasticsearch::Model::Naming::InstanceMethods
|
104
|
-
include Elasticsearch::Model::Indexing::InstanceMethods
|
105
|
-
include Elasticsearch::Model::Serializing::InstanceMethods
|
106
|
-
end
|
107
|
-
|
108
|
-
Elasticsearch::Model::Proxy::InstanceMethodsProxy.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
109
|
-
def as_indexed_json(options={})
|
110
|
-
target.respond_to?(:as_indexed_json) ? target.__send__(:as_indexed_json, options) : super
|
111
|
-
end
|
112
|
-
CODE
|
113
|
-
|
114
|
-
# Delegate important methods to the `__elasticsearch__` proxy, unless they are defined already
|
115
|
-
#
|
112
|
+
# Delegate common methods to the `__elasticsearch__` ClassMethodsProxy, unless they are defined already
|
116
113
|
class << self
|
117
114
|
METHODS.each do |method|
|
118
115
|
delegate method, to: :__elasticsearch__ unless self.public_instance_methods.include?(method)
|
119
116
|
end
|
120
117
|
end
|
121
|
-
|
122
|
-
# Mix the importing module into the proxy
|
123
|
-
#
|
124
|
-
self.__elasticsearch__.class_eval do
|
125
|
-
include Elasticsearch::Model::Importing::ClassMethods
|
126
|
-
include Adapter.from_class(base).importing_mixin
|
127
|
-
end
|
128
|
-
|
129
|
-
# Add to the registry if it's a class (and not in intermediate module)
|
130
|
-
Registry.add(base) if base.is_a?(Class)
|
131
118
|
end
|
119
|
+
|
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)
|
132
122
|
end
|
133
123
|
|
134
124
|
module ClassMethods
|
@@ -182,36 +172,11 @@ module Elasticsearch
|
|
182
172
|
Response::Response.new(models, request)
|
183
173
|
end
|
184
174
|
|
185
|
-
# Check if inheritance is enabled
|
186
|
-
#
|
187
|
-
# @note Inheritance is disabled by default.
|
188
|
-
#
|
189
|
-
def inheritance_enabled
|
190
|
-
@settings[:inheritance_enabled] ||= false
|
191
|
-
end
|
192
|
-
|
193
|
-
# Enable inheritance of index_name and document_type
|
194
|
-
#
|
195
|
-
# @example Enable inheritance
|
196
|
-
#
|
197
|
-
# Elasticsearch::Model.inheritance_enabled = true
|
198
|
-
#
|
199
|
-
def inheritance_enabled=(inheritance_enabled)
|
200
|
-
warn STI_DEPRECATION_WARNING if inheritance_enabled
|
201
|
-
@settings[:inheritance_enabled] = inheritance_enabled
|
202
|
-
end
|
203
|
-
|
204
175
|
# Access the module settings
|
205
176
|
#
|
206
177
|
def settings
|
207
178
|
@settings ||= {}
|
208
179
|
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
|
215
180
|
end
|
216
181
|
extend ClassMethods
|
217
182
|
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
module Adapter
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
module Adapter
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
module Adapter
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
module Adapter
|
@@ -58,8 +75,8 @@ module Elasticsearch
|
|
58
75
|
klass.where(klass.primary_key => ids)
|
59
76
|
when Elasticsearch::Model::Adapter::Mongoid.equal?(adapter)
|
60
77
|
klass.where(:id.in => ids)
|
61
|
-
|
62
|
-
|
78
|
+
else
|
79
|
+
klass.find(ids)
|
63
80
|
end
|
64
81
|
end
|
65
82
|
|
@@ -91,13 +108,21 @@ module Elasticsearch
|
|
91
108
|
def __type_for_hit(hit)
|
92
109
|
@@__types ||= {}
|
93
110
|
|
94
|
-
|
111
|
+
key = "#{hit[:_index]}::#{hit[:_type]}" if hit[:_type] && hit[:_type] != '_doc'
|
112
|
+
key = hit[:_index] unless key
|
113
|
+
|
114
|
+
@@__types[key] ||= begin
|
95
115
|
Registry.all.detect do |model|
|
96
|
-
model.index_name == hit[:_index] &&
|
116
|
+
(model.index_name == hit[:_index] && __no_type?(hit)) ||
|
117
|
+
(model.index_name == hit[:_index] && model.document_type == hit[:_type])
|
97
118
|
end
|
98
119
|
end
|
99
120
|
end
|
100
121
|
|
122
|
+
def __no_type?(hit)
|
123
|
+
hit[:_type].nil? || hit[:_type] == '_doc'
|
124
|
+
end
|
125
|
+
|
101
126
|
# Returns the adapter registered for a particular `klass` or `nil` if not available
|
102
127
|
#
|
103
128
|
# @api private
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
# Prevent `MyModel.inspect` failing with `ActiveRecord::ConnectionNotEstablished`
|
2
19
|
# (triggered by elasticsearch-model/lib/elasticsearch/model.rb:79:in `included')
|
3
20
|
#
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
|
@@ -1,3 +1,20 @@
|
|
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
|
+
|
1
18
|
module Elasticsearch
|
2
19
|
module Model
|
3
20
|
|
@@ -36,7 +53,29 @@ module Elasticsearch
|
|
36
53
|
#
|
37
54
|
# @yield [Hash] Gives the Hash with the Elasticsearch response to the block
|
38
55
|
#
|
39
|
-
# @return [Fixnum]
|
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
|
+
#
|
40
79
|
#
|
41
80
|
# @example Import all records into the index
|
42
81
|
#
|
@@ -82,20 +121,19 @@ module Elasticsearch
|
|
82
121
|
#
|
83
122
|
# @example Update the batch before yielding it
|
84
123
|
#
|
85
|
-
#
|
86
|
-
#
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
#
|
93
|
-
#
|
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
|
94
133
|
#
|
95
134
|
# Article.import preprocess: :enrich
|
96
135
|
#
|
97
|
-
# @example Return an array of error elements instead of the number of errors,
|
98
|
-
# 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
|
99
137
|
#
|
100
138
|
# Article.import return: 'errors'
|
101
139
|
#
|
@@ -105,6 +143,7 @@ module Elasticsearch
|
|
105
143
|
target_index = options.delete(:index) || index_name
|
106
144
|
target_type = options.delete(:type) || document_type
|
107
145
|
transform = options.delete(:transform) || __transform
|
146
|
+
pipeline = options.delete(:pipeline)
|
108
147
|
return_value = options.delete(:return) || 'count'
|
109
148
|
|
110
149
|
unless transform.respond_to?(:call)
|
@@ -120,10 +159,15 @@ module Elasticsearch
|
|
120
159
|
end
|
121
160
|
|
122
161
|
__find_in_batches(options) do |batch|
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
162
|
+
params = {
|
163
|
+
index: target_index,
|
164
|
+
type: target_type,
|
165
|
+
body: __batch_to_bulk(batch, transform)
|
166
|
+
}
|
167
|
+
|
168
|
+
params[:pipeline] = pipeline if pipeline
|
169
|
+
|
170
|
+
response = client.bulk params
|
127
171
|
|
128
172
|
yield response if block_given?
|
129
173
|
|