elasticsearch-model 7.2.0 → 8.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/Gemfile +1 -0
- data/README.md +22 -22
- data/Rakefile +7 -6
- data/elasticsearch-model.gemspec +5 -5
- data/examples/activerecord_associations.rb +1 -1
- data/examples/activerecord_custom_analyzer.rb +2 -2
- data/gemfiles/{6.0.gemfile → 6.1.gemfile} +6 -6
- data/gemfiles/{5.0.gemfile → 7.0.gemfile} +8 -7
- data/gemfiles/{3.0.gemfile → 7.1.gemfile} +9 -8
- data/lib/elasticsearch/model/adapter.rb +0 -2
- data/lib/elasticsearch/model/adapters/active_record.rb +0 -4
- data/lib/elasticsearch/model/adapters/default.rb +0 -4
- data/lib/elasticsearch/model/adapters/mongoid.rb +9 -11
- data/lib/elasticsearch/model/adapters/multiple.rb +0 -1
- data/lib/elasticsearch/model/importing.rb +1 -12
- data/lib/elasticsearch/model/indexing.rb +6 -19
- data/lib/elasticsearch/model/multimodel.rb +1 -10
- data/lib/elasticsearch/model/naming.rb +7 -58
- data/lib/elasticsearch/model/proxy.rb +6 -7
- data/lib/elasticsearch/model/response/result.rb +0 -6
- data/lib/elasticsearch/model/searching.rb +2 -3
- data/lib/elasticsearch/model/version.rb +1 -1
- data/lib/elasticsearch/model.rb +6 -3
- data/spec/elasticsearch/model/adapter_spec.rb +0 -11
- data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +48 -76
- data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +6 -78
- data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +6 -2
- data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +1 -5
- data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +0 -8
- data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +1 -4
- data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +9 -11
- data/spec/elasticsearch/model/adapters/mongoid_spec.rb +1 -1
- data/spec/elasticsearch/model/adapters/multiple_spec.rb +1 -11
- data/spec/elasticsearch/model/importing_spec.rb +6 -35
- data/spec/elasticsearch/model/indexing_spec.rb +45 -170
- data/spec/elasticsearch/model/module_spec.rb +0 -1
- data/spec/elasticsearch/model/multimodel_spec.rb +2 -8
- data/spec/elasticsearch/model/naming_spec.rb +0 -68
- data/spec/elasticsearch/model/proxy_spec.rb +8 -2
- data/spec/elasticsearch/model/response/aggregations_spec.rb +4 -4
- data/spec/elasticsearch/model/response/base_spec.rb +0 -1
- data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +3 -4
- data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +0 -1
- data/spec/elasticsearch/model/response/records_spec.rb +0 -1
- data/spec/elasticsearch/model/response/response_spec.rb +0 -1
- data/spec/elasticsearch/model/response/result_spec.rb +0 -17
- data/spec/elasticsearch/model/response/results_spec.rb +0 -1
- data/spec/elasticsearch/model/searching_search_request_spec.rb +5 -6
- data/spec/spec_helper.rb +9 -11
- data/spec/support/app/answer.rb +0 -1
- data/spec/support/app/article.rb +0 -2
- data/spec/support/app/article_no_type.rb +1 -1
- data/spec/support/app/namespaced_book.rb +0 -2
- data/spec/support/app/parent_and_child_searchable.rb +6 -4
- data/spec/support/app/question.rb +0 -1
- metadata +15 -16
- data/gemfiles/4.0.gemfile +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 59f777d222c903faedd6455331f6d784114bda8c6bb5e5d135fd7bc3aaef6adc
|
4
|
+
data.tar.gz: eff30de335c6748463490b938dc583215939388b781f23b795b9549254c001b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a06372240d9f71e9549fb745ebed859dcd654ea1285b8081ab87edfcfac6f8cf912f9b94929f8ace1acef190407b517d8b55968615f6f368936f9d4f87f1e0d
|
7
|
+
data.tar.gz: 07b61d6418b834b044e5d85f744a1139d3c2b9808be6f0e3ce417f24b87f10f2a14cbfa368fee372d662d2f778c1858d63ddcef4b2ece7d70bb7dbe6404ec95d
|
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -6,17 +6,19 @@ It aims to simplify integration of Ruby classes ("models"), commonly found e.g.
|
|
6
6
|
|
7
7
|
## Compatibility
|
8
8
|
|
9
|
-
This library is compatible with Ruby
|
9
|
+
This library is compatible with Ruby 3 and higher.
|
10
10
|
|
11
|
-
The
|
11
|
+
The version numbers follow the Elasticsearch major versions. Currently the `main` branch is compatible with version `8.x` of the Elasticsearch stack.
|
12
12
|
|
13
|
-
| Rubygem
|
14
|
-
|
15
|
-
| 0.1
|
16
|
-
| 2.x
|
17
|
-
| 5.x
|
18
|
-
| 6.x
|
19
|
-
|
|
13
|
+
| Rubygem | | Elasticsearch |
|
14
|
+
|:-------:|:-:|:-------------:|
|
15
|
+
| 0.1 | → | 1.x |
|
16
|
+
| 2.x | → | 2.x |
|
17
|
+
| 5.x | → | 5.x |
|
18
|
+
| 6.x | → | 6.x |
|
19
|
+
| 7.x | → | 7.x |
|
20
|
+
| 8.x | → | 8.x |
|
21
|
+
| main | → | 8.x |
|
20
22
|
|
21
23
|
## Installation
|
22
24
|
|
@@ -116,7 +118,7 @@ See the `Elasticsearch::Model` module documentation for technical information.
|
|
116
118
|
|
117
119
|
### The Elasticsearch client
|
118
120
|
|
119
|
-
The module will set up a [client](https://github.com/elastic/elasticsearch-ruby/tree/
|
121
|
+
The module will set up a [client](https://github.com/elastic/elasticsearch-ruby/tree/main/elasticsearch),
|
120
122
|
connected to `localhost:9200`, by default. You can access and use it as any other `Elasticsearch::Client`:
|
121
123
|
|
122
124
|
```ruby
|
@@ -139,7 +141,7 @@ Elasticsearch::Model.client = Elasticsearch::Client.new log: true
|
|
139
141
|
You might want to do this during your application bootstrap process, e.g. in a Rails initializer.
|
140
142
|
|
141
143
|
Please refer to the
|
142
|
-
[`
|
144
|
+
[`elastic-transport`](https://github.com/elastic/elastic-transport-ruby/)
|
143
145
|
library documentation for all the configuration options, and to the
|
144
146
|
[`elasticsearch-api`](http://rubydoc.info/gems/elasticsearch-api) library documentation
|
145
147
|
for information about the Ruby client API.
|
@@ -248,7 +250,7 @@ response.records.order(:title).to_a
|
|
248
250
|
The `records` method returns the real instances of your model, which is useful when you want to access your
|
249
251
|
model methods -- at the expense of slowing down your application, of course.
|
250
252
|
In most cases, working with `results` coming from Elasticsearch is sufficient, and much faster. See the
|
251
|
-
[`elasticsearch-rails`](https://github.com/elastic/elasticsearch-rails/tree/
|
253
|
+
[`elasticsearch-rails`](https://github.com/elastic/elasticsearch-rails/tree/main/elasticsearch-rails)
|
252
254
|
library for more information about compatibility with the Ruby on Rails framework.
|
253
255
|
|
254
256
|
When you want to access both the database `records` and search `results`, use the `each_with_hit`
|
@@ -267,8 +269,8 @@ It is possible to search across multiple models with the module method:
|
|
267
269
|
```ruby
|
268
270
|
Elasticsearch::Model.search('fox', [Article, Comment]).results.to_a.map(&:to_hash)
|
269
271
|
# => [
|
270
|
-
# {"_index"=>"articles", "
|
271
|
-
# {"_index"=>"comments", "
|
272
|
+
# {"_index"=>"articles", "_id"=>"1", "_score"=>0.35136628, "_source"=>...},
|
273
|
+
# {"_index"=>"comments", "_id"=>"1", "_score"=>0.35136628, "_source"=>...}
|
272
274
|
# ]
|
273
275
|
|
274
276
|
Elasticsearch::Model.search('fox', [Article, Comment]).records.to_a
|
@@ -343,7 +345,7 @@ response.results.first.title
|
|
343
345
|
# => "Quick brown fox"
|
344
346
|
```
|
345
347
|
|
346
|
-
Also, you can use the [**`elasticsearch-dsl`**](https://github.com/elastic/elasticsearch-ruby
|
348
|
+
Also, you can use the [**`elasticsearch-dsl`**](https://github.com/elastic/elasticsearch-dsl-ruby) library, which provides a specialized Ruby API for the Elasticsearch Query DSL:
|
347
349
|
|
348
350
|
```ruby
|
349
351
|
require 'elasticsearch/dsl'
|
@@ -413,13 +415,11 @@ Article.__elasticsearch__.create_index! force: true
|
|
413
415
|
Article.__elasticsearch__.refresh_index!
|
414
416
|
```
|
415
417
|
|
416
|
-
By default, index name
|
417
|
-
you can set it explicitly, however:
|
418
|
+
By default, index name will be inferred from your class name, you can set it explicitly, however:
|
418
419
|
|
419
420
|
```ruby
|
420
421
|
class Article
|
421
422
|
index_name "articles-#{Rails.env}"
|
422
|
-
document_type "post"
|
423
423
|
end
|
424
424
|
```
|
425
425
|
|
@@ -527,11 +527,11 @@ class Indexer
|
|
527
527
|
case operation.to_s
|
528
528
|
when /index/
|
529
529
|
record = Article.find(record_id)
|
530
|
-
Client.index index: 'articles',
|
530
|
+
Client.index index: 'articles', id: record.id, body: record.__elasticsearch__.as_indexed_json
|
531
531
|
when /delete/
|
532
532
|
begin
|
533
|
-
Client.delete index: 'articles',
|
534
|
-
rescue
|
533
|
+
Client.delete index: 'articles', id: record_id
|
534
|
+
rescue Elastic::Transport::Transport::Errors::NotFound
|
535
535
|
logger.debug "Article not found, ID: #{record_id}"
|
536
536
|
end
|
537
537
|
else raise ArgumentError, "Unknown operation '#{operation}'"
|
@@ -553,7 +553,7 @@ You'll see the job being processed in the console where you started the _Sidekiq
|
|
553
553
|
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: ["index", "ID: 7"]
|
554
554
|
Indexer JID-eb7e2daf389a1e5e83697128 INFO: PUT http://localhost:9200/articles/article/1 [status:200, request:0.004s, query:n/a]
|
555
555
|
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: > {"id":1,"title":"Updated", ...}
|
556
|
-
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: < {"ok":true,"_index":"articles","
|
556
|
+
Indexer JID-eb7e2daf389a1e5e83697128 DEBUG: < {"ok":true,"_index":"articles","_id":"1","_version":6}
|
557
557
|
Indexer JID-eb7e2daf389a1e5e83697128 INFO: done: 0.006 sec
|
558
558
|
```
|
559
559
|
|
data/Rakefile
CHANGED
@@ -21,8 +21,7 @@ desc 'Run unit tests'
|
|
21
21
|
task default: 'test:all'
|
22
22
|
task test: 'test:all'
|
23
23
|
|
24
|
-
gemfiles = ['
|
25
|
-
gemfiles << '4.0.gemfile' if RUBY_VERSION < '2.7'
|
24
|
+
gemfiles = ['6.1.gemfile', '7.0.gemfile']
|
26
25
|
GEMFILES = gemfiles.freeze
|
27
26
|
|
28
27
|
namespace :bundle do
|
@@ -47,16 +46,18 @@ end
|
|
47
46
|
require 'rake/testtask'
|
48
47
|
namespace :test do
|
49
48
|
desc 'Run all tests. Optionally define env variable RAILS_VERSIONS. E.g. RAILS_VERSIONS=3.0,5.0'
|
50
|
-
task :all
|
51
|
-
gemfiles = ENV['RAILS_VERSIONS'] ? ENV['RAILS_VERSIONS'].split(',').map {|v| "#{v}.gemfile"} : GEMFILES
|
49
|
+
task :all do |task, args|
|
50
|
+
gemfiles = ENV['RAILS_VERSIONS'] ? ENV['RAILS_VERSIONS'].split(',').map { |v| "#{v}.gemfile" } : GEMFILES
|
52
51
|
puts '-' * 80
|
53
52
|
gemfiles.each do |gemfile|
|
54
53
|
puts "GEMFILE: #{gemfile}"
|
55
|
-
sh "BUNDLE_GEMFILE='#{File.expand_path("../gemfiles/#{gemfile}", __FILE__)}' "
|
56
|
-
|
54
|
+
sh "BUNDLE_GEMFILE='#{File.expand_path("../gemfiles/#{gemfile}", __FILE__)}' " \
|
55
|
+
' bundle exec rspec'
|
57
56
|
puts '-' * 80
|
58
57
|
end
|
59
58
|
end
|
59
|
+
|
60
|
+
task unit: :all
|
60
61
|
end
|
61
62
|
|
62
63
|
# ----- Documentation tasks ---------------------------------------------------
|
data/elasticsearch-model.gemspec
CHANGED
@@ -24,8 +24,8 @@ require 'elasticsearch/model/version'
|
|
24
24
|
Gem::Specification.new do |s|
|
25
25
|
s.name = 'elasticsearch-model'
|
26
26
|
s.version = Elasticsearch::Model::VERSION
|
27
|
-
s.authors = ['
|
28
|
-
s.email = ['
|
27
|
+
s.authors = ['Elastic Client Library Maintainers']
|
28
|
+
s.email = ['client-libs@elastic.co']
|
29
29
|
s.description = 'ActiveModel/Record integrations for Elasticsearch.'
|
30
30
|
s.summary = 'ActiveModel/Record integrations for Elasticsearch.'
|
31
31
|
s.homepage = 'https://github.com/elasticsearch/elasticsearch-rails/'
|
@@ -39,10 +39,10 @@ Gem::Specification.new do |s|
|
|
39
39
|
s.extra_rdoc_files = ['README.md', 'LICENSE.txt']
|
40
40
|
s.rdoc_options = ['--charset=UTF-8']
|
41
41
|
|
42
|
-
s.required_ruby_version = '>=
|
42
|
+
s.required_ruby_version = '>= 3'
|
43
43
|
|
44
44
|
s.add_dependency 'activesupport', '> 3'
|
45
|
-
s.add_dependency 'elasticsearch', '~>
|
45
|
+
s.add_dependency 'elasticsearch', '~> 8'
|
46
46
|
s.add_dependency 'hashie'
|
47
47
|
|
48
48
|
s.add_development_dependency 'activemodel', '> 3'
|
@@ -63,6 +63,6 @@ Gem::Specification.new do |s|
|
|
63
63
|
unless defined?(JRUBY_VERSION)
|
64
64
|
s.add_development_dependency 'oj'
|
65
65
|
s.add_development_dependency 'ruby-prof'
|
66
|
-
s.add_development_dependency 'sqlite3'
|
66
|
+
s.add_development_dependency 'sqlite3', '~> 1.4'
|
67
67
|
end
|
68
68
|
end
|
@@ -81,7 +81,7 @@ end
|
|
81
81
|
# ----- Elasticsearch client setup ----------------------------------------------------------------
|
82
82
|
|
83
83
|
Elasticsearch::Model.client = Elasticsearch::Client.new log: true
|
84
|
-
Elasticsearch::Model.client.transport.
|
84
|
+
Elasticsearch::Model.client.transport.logger.formatter = proc { |s, d, p, m| "\e[2m#{m}\n\e[0m" }
|
85
85
|
|
86
86
|
# ----- Search integration ------------------------------------------------------------------------
|
87
87
|
|
@@ -37,8 +37,8 @@ ActiveRecord::Schema.define(version: 1) do
|
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
40
|
-
Elasticsearch::Model.client.transport.
|
41
|
-
Elasticsearch::Model.client.transport.
|
40
|
+
Elasticsearch::Model.client.transport.logger = ActiveSupport::Logger.new(STDOUT)
|
41
|
+
Elasticsearch::Model.client.transport.logger.formatter = lambda { |s, d, p, m| "#{m.ansi(:faint)}\n" }
|
42
42
|
|
43
43
|
class Article < ActiveRecord::Base
|
44
44
|
include Elasticsearch::Model
|
@@ -20,17 +20,17 @@
|
|
20
20
|
# $ BUNDLE_GEMFILE=./gemfiles/6.0.gemfile bundle install
|
21
21
|
# $ BUNDLE_GEMFILE=./gemfiles/6.0.gemfile bundle exec rake test:integration
|
22
22
|
|
23
|
-
|
24
23
|
source 'https://rubygems.org'
|
25
24
|
|
26
25
|
gemspec path: '../'
|
27
26
|
|
28
|
-
gem 'activemodel', '6.
|
29
|
-
gem 'activerecord', '6.
|
30
|
-
gem 'sqlite3' unless defined?(JRUBY_VERSION)
|
31
|
-
#gem 'mongoid', '~> 6'
|
27
|
+
gem 'activemodel', '6.1'
|
28
|
+
gem 'activerecord', '6.1'
|
29
|
+
gem 'sqlite3', '~> 1.4' unless defined?(JRUBY_VERSION)
|
30
|
+
# gem 'mongoid', '~> 6'
|
32
31
|
|
33
32
|
group :development, :testing do
|
34
|
-
gem '
|
33
|
+
gem 'debug'
|
35
34
|
gem 'pry-nav'
|
35
|
+
gem 'rspec'
|
36
36
|
end
|
@@ -17,19 +17,20 @@
|
|
17
17
|
|
18
18
|
# Usage:
|
19
19
|
#
|
20
|
-
# $ BUNDLE_GEMFILE=./gemfiles/
|
21
|
-
# $ BUNDLE_GEMFILE=./gemfiles/
|
20
|
+
# $ BUNDLE_GEMFILE=./gemfiles/7.0.gemfile bundle install
|
21
|
+
# $ BUNDLE_GEMFILE=./gemfiles/7.0.gemfile bundle exec rake test:integration
|
22
22
|
|
23
23
|
source 'https://rubygems.org'
|
24
24
|
|
25
25
|
gemspec path: '../'
|
26
26
|
|
27
|
-
gem 'activemodel', '~>
|
28
|
-
gem 'activerecord', '~>
|
29
|
-
gem 'sqlite3' unless defined?(JRUBY_VERSION)
|
30
|
-
gem 'mongoid', '~> 6'
|
27
|
+
gem 'activemodel', '~> 7'
|
28
|
+
gem 'activerecord', '~> 7'
|
29
|
+
gem 'sqlite3', '~> 1.4' unless defined?(JRUBY_VERSION)
|
30
|
+
# gem 'mongoid', '~> 6'
|
31
31
|
|
32
32
|
group :development, :testing do
|
33
|
-
gem '
|
33
|
+
gem 'debug'
|
34
34
|
gem 'pry-nav'
|
35
|
+
gem 'rspec'
|
35
36
|
end
|
@@ -17,19 +17,20 @@
|
|
17
17
|
|
18
18
|
# Usage:
|
19
19
|
#
|
20
|
-
# $ BUNDLE_GEMFILE=./gemfiles/
|
21
|
-
# $ BUNDLE_GEMFILE=./gemfiles/
|
20
|
+
# $ BUNDLE_GEMFILE=./gemfiles/7.0.gemfile bundle install
|
21
|
+
# $ BUNDLE_GEMFILE=./gemfiles/7.0.gemfile bundle exec rake test:integration
|
22
22
|
|
23
23
|
source 'https://rubygems.org'
|
24
24
|
|
25
25
|
gemspec path: '../'
|
26
26
|
|
27
|
-
gem 'activemodel', '
|
28
|
-
gem 'activerecord', '~>
|
29
|
-
gem '
|
30
|
-
gem '
|
27
|
+
gem 'activemodel', '~> 7.1'
|
28
|
+
gem 'activerecord', '~> 7.1'
|
29
|
+
gem 'sqlite3', '~> 1.4' unless defined?(JRUBY_VERSION)
|
30
|
+
# gem 'mongoid', '~> 6'
|
31
31
|
|
32
32
|
group :development, :testing do
|
33
|
-
gem '
|
33
|
+
gem 'debug'
|
34
34
|
gem 'pry-nav'
|
35
|
-
|
35
|
+
gem 'rspec'
|
36
|
+
end
|
@@ -17,7 +17,6 @@
|
|
17
17
|
|
18
18
|
module Elasticsearch
|
19
19
|
module Model
|
20
|
-
|
21
20
|
# Contains an adapter which provides OxM-specific implementations for common behaviour:
|
22
21
|
#
|
23
22
|
# * {Adapter::Adapter#records_mixin Fetching records from the database}
|
@@ -29,7 +28,6 @@ module Elasticsearch
|
|
29
28
|
# @see Elasticsearch::Model::Adapter::Mongoid
|
30
29
|
#
|
31
30
|
module Adapter
|
32
|
-
|
33
31
|
# Returns an adapter based on the Ruby class passed
|
34
32
|
#
|
35
33
|
# @example Create an adapter for an ActiveRecord-based model
|
@@ -18,11 +18,9 @@
|
|
18
18
|
module Elasticsearch
|
19
19
|
module Model
|
20
20
|
module Adapter
|
21
|
-
|
22
21
|
# An adapter for ActiveRecord-based models
|
23
22
|
#
|
24
23
|
module ActiveRecord
|
25
|
-
|
26
24
|
Adapter.register self,
|
27
25
|
lambda { |klass| !!defined?(::ActiveRecord::Base) && klass.respond_to?(:ancestors) && klass.ancestors.include?(::ActiveRecord::Base) }
|
28
26
|
|
@@ -71,7 +69,6 @@ module Elasticsearch
|
|
71
69
|
end
|
72
70
|
|
73
71
|
module Callbacks
|
74
|
-
|
75
72
|
# Handle index updates (creating, updating or deleting documents)
|
76
73
|
# when the model changes, by hooking into the lifecycle
|
77
74
|
#
|
@@ -87,7 +84,6 @@ module Elasticsearch
|
|
87
84
|
end
|
88
85
|
|
89
86
|
module Importing
|
90
|
-
|
91
87
|
# Fetch batches of records from the database (used by the import method)
|
92
88
|
#
|
93
89
|
#
|
@@ -18,15 +18,12 @@
|
|
18
18
|
module Elasticsearch
|
19
19
|
module Model
|
20
20
|
module Adapter
|
21
|
-
|
22
21
|
# The default adapter for models which haven't one registered
|
23
22
|
#
|
24
23
|
module Default
|
25
|
-
|
26
24
|
# Module for implementing methods and logic related to fetching records from the database
|
27
25
|
#
|
28
26
|
module Records
|
29
|
-
|
30
27
|
# Return the collection of records fetched from the database
|
31
28
|
#
|
32
29
|
# By default uses `MyModel#find[1, 2, 3]`
|
@@ -60,7 +57,6 @@ module Elasticsearch
|
|
60
57
|
raise NotImplemented, "Method not implemented for default adapter"
|
61
58
|
end
|
62
59
|
end
|
63
|
-
|
64
60
|
end
|
65
61
|
end
|
66
62
|
end
|
@@ -18,18 +18,21 @@
|
|
18
18
|
module Elasticsearch
|
19
19
|
module Model
|
20
20
|
module Adapter
|
21
|
-
|
22
21
|
# An adapter for Mongoid-based models
|
23
22
|
#
|
24
23
|
# @see http://mongoid.org
|
25
24
|
#
|
26
25
|
module Mongoid
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
Adapter.register(
|
27
|
+
self,
|
28
|
+
lambda do |klass|
|
29
|
+
!!defined?(::Mongoid::Document) &&
|
30
|
+
klass.respond_to?(:ancestors) &&
|
31
|
+
klass.ancestors.include?(::Mongoid::Document)
|
32
|
+
end
|
33
|
+
)
|
30
34
|
|
31
35
|
module Records
|
32
|
-
|
33
36
|
# Return a `Mongoid::Criteria` instance
|
34
37
|
#
|
35
38
|
def records
|
@@ -59,7 +62,6 @@ module Elasticsearch
|
|
59
62
|
end
|
60
63
|
|
61
64
|
module Callbacks
|
62
|
-
|
63
65
|
# Handle index updates (creating, updating or deleting documents)
|
64
66
|
# when the model changes, by hooking into the lifecycle
|
65
67
|
#
|
@@ -73,7 +75,6 @@ module Elasticsearch
|
|
73
75
|
end
|
74
76
|
|
75
77
|
module Importing
|
76
|
-
|
77
78
|
# Fetch batches of records from the database
|
78
79
|
#
|
79
80
|
# @see https://github.com/mongoid/mongoid/issues/1334
|
@@ -88,19 +89,16 @@ module Elasticsearch
|
|
88
89
|
scope = all
|
89
90
|
scope = scope.send(named_scope) if named_scope
|
90
91
|
scope = query.is_a?(Proc) ? scope.class_exec(&query) : scope.where(query) if query
|
91
|
-
|
92
92
|
scope.no_timeout.each_slice(batch_size) do |items|
|
93
93
|
yield (preprocess ? self.__send__(preprocess, items) : items)
|
94
94
|
end
|
95
95
|
end
|
96
96
|
|
97
97
|
def __transform
|
98
|
-
lambda {|a|
|
98
|
+
lambda { |a| { index: { _id: a.id.to_s, data: a.as_indexed_json } } }
|
99
99
|
end
|
100
100
|
end
|
101
|
-
|
102
101
|
end
|
103
|
-
|
104
102
|
end
|
105
103
|
end
|
106
104
|
end
|
@@ -17,14 +17,12 @@
|
|
17
17
|
|
18
18
|
module Elasticsearch
|
19
19
|
module Model
|
20
|
-
|
21
20
|
# Provides support for easily and efficiently importing large amounts of
|
22
21
|
# records from the including class into the index.
|
23
22
|
#
|
24
23
|
# @see ClassMethods#import
|
25
24
|
#
|
26
25
|
module Importing
|
27
|
-
|
28
26
|
# When included in a model, adds the importing methods.
|
29
27
|
#
|
30
28
|
# @example Import all records from the `Article` model
|
@@ -42,13 +40,12 @@ module Elasticsearch
|
|
42
40
|
end
|
43
41
|
|
44
42
|
module ClassMethods
|
45
|
-
|
46
43
|
# Import all model records into the index
|
47
44
|
#
|
48
45
|
# The method will pick up correct strategy based on the `Importing` module
|
49
46
|
# defined in the corresponding adapter.
|
50
47
|
#
|
51
|
-
# @param options [Hash] Options passed to the underlying `__find_in_batches`method
|
48
|
+
# @param options [Hash] Options passed to the underlying `__find_in_batches` method
|
52
49
|
# @param block [Proc] Optional block to evaluate for each batch
|
53
50
|
#
|
54
51
|
# @yield [Hash] Gives the Hash with the Elasticsearch response to the block
|
@@ -62,7 +59,6 @@ module Elasticsearch
|
|
62
59
|
# "index" => {
|
63
60
|
# "error" => 'FAILED',
|
64
61
|
# "_index" => "test",
|
65
|
-
# "_type" => "_doc",
|
66
62
|
# "_id" => '1',
|
67
63
|
# "_version" => 1,
|
68
64
|
# "result" => "foo",
|
@@ -141,7 +137,6 @@ module Elasticsearch
|
|
141
137
|
errors = []
|
142
138
|
refresh = options.delete(:refresh) || false
|
143
139
|
target_index = options.delete(:index) || index_name
|
144
|
-
target_type = options.delete(:type) || document_type
|
145
140
|
transform = options.delete(:transform) || __transform
|
146
141
|
pipeline = options.delete(:pipeline)
|
147
142
|
return_value = options.delete(:return) || 'count'
|
@@ -161,14 +156,10 @@ module Elasticsearch
|
|
161
156
|
__find_in_batches(options) do |batch|
|
162
157
|
params = {
|
163
158
|
index: target_index,
|
164
|
-
type: target_type,
|
165
159
|
body: __batch_to_bulk(batch, transform)
|
166
160
|
}
|
167
|
-
|
168
161
|
params[:pipeline] = pipeline if pipeline
|
169
|
-
|
170
162
|
response = client.bulk params
|
171
|
-
|
172
163
|
yield response if block_given?
|
173
164
|
|
174
165
|
errors += response['items'].select { |k, v| k.values.first['error'] }
|
@@ -188,8 +179,6 @@ module Elasticsearch
|
|
188
179
|
batch.map { |model| transform.call(model) }
|
189
180
|
end
|
190
181
|
end
|
191
|
-
|
192
182
|
end
|
193
|
-
|
194
183
|
end
|
195
184
|
end
|
@@ -17,7 +17,6 @@
|
|
17
17
|
|
18
18
|
module Elasticsearch
|
19
19
|
module Model
|
20
|
-
|
21
20
|
# Provides the necessary support to set up index options (mappings, settings)
|
22
21
|
# as well as instance methods to create, update or delete documents in the index.
|
23
22
|
#
|
@@ -29,7 +28,6 @@ module Elasticsearch
|
|
29
28
|
# @see InstanceMethods#delete_document
|
30
29
|
#
|
31
30
|
module Indexing
|
32
|
-
|
33
31
|
# Wraps the [index settings](https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)
|
34
32
|
#
|
35
33
|
class Settings
|
@@ -51,13 +49,11 @@ module Elasticsearch
|
|
51
49
|
# Wraps the [index mappings](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html)
|
52
50
|
#
|
53
51
|
class Mappings
|
54
|
-
attr_accessor :options
|
52
|
+
attr_accessor :options
|
55
53
|
|
56
|
-
# @private
|
57
54
|
TYPES_WITH_EMBEDDED_PROPERTIES = %w(object nested)
|
58
55
|
|
59
|
-
def initialize(
|
60
|
-
@type = type
|
56
|
+
def initialize(options={})
|
61
57
|
@options = options
|
62
58
|
@mapping = {}
|
63
59
|
end
|
@@ -87,11 +83,7 @@ module Elasticsearch
|
|
87
83
|
end
|
88
84
|
|
89
85
|
def to_hash
|
90
|
-
|
91
|
-
{ @type.to_sym => @options.merge( properties: @mapping ) }
|
92
|
-
else
|
93
|
-
@options.merge( properties: @mapping )
|
94
|
-
end
|
86
|
+
@options.merge( properties: @mapping )
|
95
87
|
end
|
96
88
|
|
97
89
|
def as_json(options={})
|
@@ -100,7 +92,6 @@ module Elasticsearch
|
|
100
92
|
end
|
101
93
|
|
102
94
|
module ClassMethods
|
103
|
-
|
104
95
|
# Defines mappings for the index
|
105
96
|
#
|
106
97
|
# @example Define mapping for model
|
@@ -152,7 +143,7 @@ module Elasticsearch
|
|
152
143
|
# when it doesn't already define them. Use the `__elasticsearch__` proxy otherwise.
|
153
144
|
#
|
154
145
|
def mapping(options={}, &block)
|
155
|
-
@mapping ||= Mappings.new(
|
146
|
+
@mapping ||= Mappings.new(options)
|
156
147
|
|
157
148
|
@mapping.options.update(options) unless options.empty?
|
158
149
|
|
@@ -290,7 +281,7 @@ module Elasticsearch
|
|
290
281
|
self.client.indices.delete index: target_index
|
291
282
|
rescue Exception => e
|
292
283
|
if e.class.to_s =~ /NotFound/ && options[:force]
|
293
|
-
client.transport.
|
284
|
+
client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
|
294
285
|
nil
|
295
286
|
else
|
296
287
|
raise e
|
@@ -317,7 +308,7 @@ module Elasticsearch
|
|
317
308
|
self.client.indices.refresh index: target_index
|
318
309
|
rescue Exception => e
|
319
310
|
if e.class.to_s =~ /NotFound/ && options[:force]
|
320
|
-
client.transport.
|
311
|
+
client.transport.logger.debug("[!!!] Index does not exist (#{e.class})") if client.transport.logger
|
321
312
|
nil
|
322
313
|
else
|
323
314
|
raise e
|
@@ -372,7 +363,6 @@ module Elasticsearch
|
|
372
363
|
request = { index: index_name,
|
373
364
|
id: id,
|
374
365
|
body: document }
|
375
|
-
request.merge!(type: document_type) if document_type
|
376
366
|
|
377
367
|
client.index(request.merge!(options))
|
378
368
|
end
|
@@ -393,7 +383,6 @@ module Elasticsearch
|
|
393
383
|
def delete_document(options={})
|
394
384
|
request = { index: index_name,
|
395
385
|
id: self.id }
|
396
|
-
request.merge!(type: document_type) if document_type
|
397
386
|
|
398
387
|
client.delete(request.merge!(options))
|
399
388
|
end
|
@@ -434,7 +423,6 @@ module Elasticsearch
|
|
434
423
|
request = { index: index_name,
|
435
424
|
id: self.id,
|
436
425
|
body: { doc: attributes } }
|
437
|
-
request.merge!(type: document_type) if document_type
|
438
426
|
|
439
427
|
client.update(request.merge!(options))
|
440
428
|
end
|
@@ -461,7 +449,6 @@ module Elasticsearch
|
|
461
449
|
request = { index: index_name,
|
462
450
|
id: self.id,
|
463
451
|
body: { doc: attributes } }
|
464
|
-
request.merge!(type: document_type) if document_type
|
465
452
|
|
466
453
|
client.update(request.merge!(options))
|
467
454
|
end
|
@@ -17,7 +17,6 @@
|
|
17
17
|
|
18
18
|
module Elasticsearch
|
19
19
|
module Model
|
20
|
-
|
21
20
|
# Keeps a global registry of classes that include `Elasticsearch::Model`
|
22
21
|
#
|
23
22
|
class Registry
|
@@ -80,17 +79,9 @@ module Elasticsearch
|
|
80
79
|
models.map { |m| m.index_name }
|
81
80
|
end
|
82
81
|
|
83
|
-
# Get an Array of document types used for retrieving documents when doing a search across multiple models
|
84
|
-
#
|
85
|
-
# @return [Array] the list of document types used for retrieving documents
|
86
|
-
#
|
87
|
-
def document_type
|
88
|
-
models.map { |m| m.document_type }.compact.presence
|
89
|
-
end
|
90
|
-
|
91
82
|
# Get the client common for all models
|
92
83
|
#
|
93
|
-
# @return
|
84
|
+
# @return Elastic::Transport::Client
|
94
85
|
#
|
95
86
|
def client
|
96
87
|
Elasticsearch::Model.client
|