elasticsearch-model 6.1.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +3 -3
- data/Gemfile +17 -0
- data/LICENSE.txt +199 -10
- data/README.md +36 -25
- data/Rakefile +18 -1
- data/elasticsearch-model.gemspec +17 -0
- 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 +17 -0
- data/gemfiles/6.0.gemfile +36 -0
- 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 +50 -12
- 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/aggregations.rb +17 -0
- data/lib/elasticsearch/model/response/base.rb +22 -1
- 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/pagination.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/response.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/lib/elasticsearch/model.rb +26 -61
- 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 +17 -0
- 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 +17 -0
- 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 +17 -0
- data/spec/elasticsearch/model/indexing_spec.rb +111 -15
- data/spec/elasticsearch/model/module_spec.rb +17 -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 +27 -3
- 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
- data/spec/support/app.rb +27 -2
- metadata +6 -5
- data/spec/elasticsearch/model/naming_inheritance_spec.rb +0 -184
data/examples/riak_article.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
|
# Riak and Elasticsearch
|
|
2
19
|
# ======================
|
|
3
20
|
#
|
data/gemfiles/3.0.gemfile
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
|
# Usage:
|
|
2
19
|
#
|
|
3
20
|
# $ BUNDLE_GEMFILE=./gemfiles/3.0.gemfile bundle install
|
|
@@ -10,7 +27,7 @@ gemspec path: '../'
|
|
|
10
27
|
gem 'activemodel', '>= 3.0'
|
|
11
28
|
gem 'activerecord', '~> 3.2'
|
|
12
29
|
gem 'mongoid', '>= 3.0'
|
|
13
|
-
gem 'sqlite3', '
|
|
30
|
+
gem 'sqlite3', '> 1.3', '< 1.4' unless defined?(JRUBY_VERSION)
|
|
14
31
|
|
|
15
32
|
group :development, :testing do
|
|
16
33
|
gem 'rspec'
|
data/gemfiles/4.0.gemfile
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
|
# Usage:
|
|
2
19
|
#
|
|
3
20
|
# $ BUNDLE_GEMFILE=./gemfiles/4.0.gemfile bundle install
|
|
@@ -9,7 +26,7 @@ gemspec path: '../'
|
|
|
9
26
|
|
|
10
27
|
gem 'activemodel', '~> 4'
|
|
11
28
|
gem 'activerecord', '~> 4'
|
|
12
|
-
gem 'sqlite3', '
|
|
29
|
+
gem 'sqlite3', '> 1.3', '< 1.4' unless defined?(JRUBY_VERSION)
|
|
13
30
|
gem 'mongoid', '~> 5'
|
|
14
31
|
|
|
15
32
|
group :development, :testing do
|
data/gemfiles/5.0.gemfile
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
|
# Usage:
|
|
2
19
|
#
|
|
3
20
|
# $ BUNDLE_GEMFILE=./gemfiles/5.0.gemfile bundle install
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
# Usage:
|
|
19
|
+
#
|
|
20
|
+
# $ BUNDLE_GEMFILE=./gemfiles/6.0.gemfile bundle install
|
|
21
|
+
# $ BUNDLE_GEMFILE=./gemfiles/6.0.gemfile bundle exec rake test:integration
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
source 'https://rubygems.org'
|
|
25
|
+
|
|
26
|
+
gemspec path: '../'
|
|
27
|
+
|
|
28
|
+
gem 'activemodel', '6.0.0'
|
|
29
|
+
gem 'activerecord', '6.0.0'
|
|
30
|
+
gem 'sqlite3' unless defined?(JRUBY_VERSION)
|
|
31
|
+
#gem 'mongoid', '~> 6'
|
|
32
|
+
|
|
33
|
+
group :development, :testing do
|
|
34
|
+
gem 'rspec'
|
|
35
|
+
gem 'pry-nav'
|
|
36
|
+
end
|
|
@@ -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
|
#
|
|
@@ -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
|
|
|
@@ -13,7 +30,7 @@ module Elasticsearch
|
|
|
13
30
|
#
|
|
14
31
|
module Indexing
|
|
15
32
|
|
|
16
|
-
# Wraps the [index settings](
|
|
33
|
+
# Wraps the [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)
|
|
17
34
|
#
|
|
18
35
|
class Settings
|
|
19
36
|
attr_accessor :settings
|
|
@@ -31,7 +48,7 @@ module Elasticsearch
|
|
|
31
48
|
end
|
|
32
49
|
end
|
|
33
50
|
|
|
34
|
-
# Wraps the [index mappings](
|
|
51
|
+
# Wraps the [index mappings](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html)
|
|
35
52
|
#
|
|
36
53
|
class Mappings
|
|
37
54
|
attr_accessor :options, :type
|
|
@@ -39,9 +56,7 @@ module Elasticsearch
|
|
|
39
56
|
# @private
|
|
40
57
|
TYPES_WITH_EMBEDDED_PROPERTIES = %w(object nested)
|
|
41
58
|
|
|
42
|
-
def initialize(type, options={})
|
|
43
|
-
raise ArgumentError, "`type` is missing" if type.nil?
|
|
44
|
-
|
|
59
|
+
def initialize(type = nil, options={})
|
|
45
60
|
@type = type
|
|
46
61
|
@options = options
|
|
47
62
|
@mapping = {}
|
|
@@ -72,7 +87,11 @@ module Elasticsearch
|
|
|
72
87
|
end
|
|
73
88
|
|
|
74
89
|
def to_hash
|
|
75
|
-
|
|
90
|
+
if @type
|
|
91
|
+
{ @type.to_sym => @options.merge( properties: @mapping ) }
|
|
92
|
+
else
|
|
93
|
+
@options.merge( properties: @mapping )
|
|
94
|
+
end
|
|
76
95
|
end
|
|
77
96
|
|
|
78
97
|
def as_json(options={})
|
|
@@ -229,10 +248,12 @@ module Elasticsearch
|
|
|
229
248
|
delete_index!(options.merge index: target_index) if options[:force]
|
|
230
249
|
|
|
231
250
|
unless index_exists?(index: target_index)
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
251
|
+
options.delete(:force)
|
|
252
|
+
self.client.indices.create({ index: target_index,
|
|
253
|
+
body: {
|
|
254
|
+
settings: settings,
|
|
255
|
+
mappings: mappings }
|
|
256
|
+
}.merge(options))
|
|
236
257
|
end
|
|
237
258
|
end
|
|
238
259
|
|
|
@@ -287,7 +308,7 @@ module Elasticsearch
|
|
|
287
308
|
#
|
|
288
309
|
# Article.__elasticsearch__.refresh_index! index: 'my-index'
|
|
289
310
|
#
|
|
290
|
-
# @see
|
|
311
|
+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-refresh.html
|
|
291
312
|
#
|
|
292
313
|
def refresh_index!(options={})
|
|
293
314
|
target_index = options.delete(:index) || self.index_name
|
|
@@ -317,13 +338,17 @@ module Elasticsearch
|
|
|
317
338
|
#
|
|
318
339
|
# @see #update_document
|
|
319
340
|
#
|
|
320
|
-
base.before_save do |
|
|
321
|
-
if
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
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))
|
|
327
352
|
end
|
|
328
353
|
end if base.respond_to?(:before_save)
|
|
329
354
|
end
|
|
@@ -343,14 +368,13 @@ module Elasticsearch
|
|
|
343
368
|
# @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:index
|
|
344
369
|
#
|
|
345
370
|
def index_document(options={})
|
|
346
|
-
document =
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
)
|
|
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))
|
|
354
378
|
end
|
|
355
379
|
|
|
356
380
|
# Deletes the model instance from the index
|
|
@@ -367,11 +391,11 @@ module Elasticsearch
|
|
|
367
391
|
# @see http://rubydoc.info/gems/elasticsearch-api/Elasticsearch/API/Actions:delete
|
|
368
392
|
#
|
|
369
393
|
def delete_document(options={})
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
)
|
|
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))
|
|
375
399
|
end
|
|
376
400
|
|
|
377
401
|
# Tries to gather the changed attributes of a model instance
|
|
@@ -406,12 +430,14 @@ module Elasticsearch
|
|
|
406
430
|
attributes_in_database
|
|
407
431
|
end
|
|
408
432
|
|
|
409
|
-
|
|
410
|
-
{ index: index_name,
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
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
|
|
415
441
|
else
|
|
416
442
|
index_document(options)
|
|
417
443
|
end
|
|
@@ -432,12 +458,12 @@ module Elasticsearch
|
|
|
432
458
|
# @return [Hash] The response from Elasticsearch
|
|
433
459
|
#
|
|
434
460
|
def update_document_attributes(attributes, options={})
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
)
|
|
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))
|
|
441
467
|
end
|
|
442
468
|
end
|
|
443
469
|
|
|
@@ -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
|
|