elasticsearch-model 6.0.0 → 6.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +5 -0
- data/README.md +14 -7
- data/Rakefile +27 -36
- data/elasticsearch-model.gemspec +1 -1
- data/examples/activerecord_mapping_completion.rb +2 -15
- data/gemfiles/3.0.gemfile +6 -1
- data/gemfiles/4.0.gemfile +7 -1
- data/gemfiles/5.0.gemfile +6 -0
- data/lib/elasticsearch/model.rb +15 -8
- data/lib/elasticsearch/model/adapters/active_record.rb +7 -26
- data/lib/elasticsearch/model/indexing.rb +5 -3
- data/lib/elasticsearch/model/naming.rb +6 -1
- data/lib/elasticsearch/model/response.rb +2 -2
- data/lib/elasticsearch/model/response/pagination.rb +2 -192
- data/lib/elasticsearch/model/response/pagination/kaminari.rb +109 -0
- data/lib/elasticsearch/model/response/pagination/will_paginate.rb +95 -0
- data/lib/elasticsearch/model/response/result.rb +1 -1
- data/lib/elasticsearch/model/version.rb +1 -1
- data/spec/elasticsearch/model/adapter_spec.rb +119 -0
- data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +334 -0
- data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +340 -0
- data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +18 -0
- data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +187 -0
- data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +110 -0
- data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +38 -0
- data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +315 -0
- data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +75 -0
- data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +61 -0
- data/spec/elasticsearch/model/adapters/active_record_spec.rb +207 -0
- data/spec/elasticsearch/model/adapters/default_spec.rb +41 -0
- data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +267 -0
- data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +66 -0
- data/spec/elasticsearch/model/adapters/mongoid_spec.rb +235 -0
- data/spec/elasticsearch/model/adapters/multiple_spec.rb +125 -0
- data/spec/elasticsearch/model/callbacks_spec.rb +33 -0
- data/spec/elasticsearch/model/client_spec.rb +66 -0
- data/spec/elasticsearch/model/hash_wrapper_spec.rb +12 -0
- data/spec/elasticsearch/model/importing_spec.rb +214 -0
- data/spec/elasticsearch/model/indexing_spec.rb +918 -0
- data/spec/elasticsearch/model/module_spec.rb +101 -0
- data/spec/elasticsearch/model/multimodel_spec.rb +55 -0
- data/spec/elasticsearch/model/naming_inheritance_spec.rb +184 -0
- data/spec/elasticsearch/model/naming_spec.rb +186 -0
- data/spec/elasticsearch/model/proxy_spec.rb +107 -0
- data/spec/elasticsearch/model/response/aggregations_spec.rb +66 -0
- data/spec/elasticsearch/model/response/base_spec.rb +90 -0
- data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +410 -0
- data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +262 -0
- data/spec/elasticsearch/model/response/records_spec.rb +118 -0
- data/spec/elasticsearch/model/response/response_spec.rb +131 -0
- data/spec/elasticsearch/model/response/result_spec.rb +122 -0
- data/spec/elasticsearch/model/response/results_spec.rb +56 -0
- data/spec/elasticsearch/model/searching_search_request_spec.rb +112 -0
- data/spec/elasticsearch/model/searching_spec.rb +49 -0
- data/spec/elasticsearch/model/serializing_spec.rb +22 -0
- data/spec/spec_helper.rb +161 -0
- data/spec/support/app.rb +21 -0
- data/spec/support/app/answer.rb +33 -0
- data/spec/support/app/article.rb +22 -0
- data/spec/support/app/article_for_pagination.rb +12 -0
- data/spec/support/app/article_with_custom_serialization.rb +13 -0
- data/spec/support/app/article_with_dynamic_index_name.rb +15 -0
- data/spec/support/app/author.rb +9 -0
- data/spec/support/app/authorship.rb +4 -0
- data/spec/support/app/category.rb +3 -0
- data/spec/support/app/comment.rb +3 -0
- data/spec/support/app/episode.rb +11 -0
- data/spec/support/app/image.rb +19 -0
- data/spec/support/app/import_article.rb +12 -0
- data/spec/support/app/mongoid_article.rb +21 -0
- data/spec/support/app/namespaced_book.rb +10 -0
- data/spec/support/app/parent_and_child_searchable.rb +24 -0
- data/spec/support/app/post.rb +14 -0
- data/spec/support/app/question.rb +27 -0
- data/spec/support/app/searchable.rb +48 -0
- data/spec/support/app/series.rb +11 -0
- data/spec/support/model.json +1 -0
- data/{test → spec}/support/model.yml +0 -0
- metadata +129 -86
- data/test/integration/active_record_associations_parent_child_test.rb +0 -188
- data/test/integration/active_record_associations_test.rb +0 -339
- data/test/integration/active_record_basic_test.rb +0 -255
- data/test/integration/active_record_custom_serialization_test.rb +0 -67
- data/test/integration/active_record_import_test.rb +0 -168
- data/test/integration/active_record_namespaced_model_test.rb +0 -56
- data/test/integration/active_record_pagination_test.rb +0 -149
- data/test/integration/dynamic_index_name_test.rb +0 -52
- data/test/integration/mongoid_basic_test.rb +0 -240
- data/test/integration/multiple_models_test.rb +0 -176
- data/test/support/model.json +0 -1
- data/test/test_helper.rb +0 -92
- data/test/unit/adapter_active_record_test.rb +0 -157
- data/test/unit/adapter_default_test.rb +0 -41
- data/test/unit/adapter_mongoid_test.rb +0 -161
- data/test/unit/adapter_multiple_test.rb +0 -106
- data/test/unit/adapter_test.rb +0 -69
- data/test/unit/callbacks_test.rb +0 -31
- data/test/unit/client_test.rb +0 -27
- data/test/unit/hash_wrapper_test.rb +0 -13
- data/test/unit/importing_test.rb +0 -224
- data/test/unit/indexing_test.rb +0 -720
- data/test/unit/module_test.rb +0 -68
- data/test/unit/multimodel_test.rb +0 -38
- data/test/unit/naming_inheritance_test.rb +0 -94
- data/test/unit/naming_test.rb +0 -103
- data/test/unit/proxy_test.rb +0 -98
- data/test/unit/response_aggregations_test.rb +0 -46
- data/test/unit/response_base_test.rb +0 -40
- data/test/unit/response_pagination_kaminari_test.rb +0 -433
- data/test/unit/response_pagination_will_paginate_test.rb +0 -398
- data/test/unit/response_records_test.rb +0 -91
- data/test/unit/response_result_test.rb +0 -90
- data/test/unit/response_results_test.rb +0 -34
- data/test/unit/response_test.rb +0 -104
- data/test/unit/searching_search_request_test.rb +0 -78
- data/test/unit/searching_test.rb +0 -41
- data/test/unit/serializing_test.rb +0 -17
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
# Needed for ActiveRecord 3.x ?
|
5
|
-
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
|
6
|
-
|
7
|
-
::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
|
8
|
-
|
9
|
-
module Elasticsearch
|
10
|
-
module Model
|
11
|
-
class ActiveRecordNamespacedModelIntegrationTest < Elasticsearch::Test::IntegrationTestCase
|
12
|
-
context "Namespaced ActiveRecord model integration" do
|
13
|
-
setup do
|
14
|
-
ActiveRecord::Schema.define(:version => 1) do
|
15
|
-
create_table :articles do |t|
|
16
|
-
t.string :title
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
module ::MyNamespace
|
21
|
-
class Article < ActiveRecord::Base
|
22
|
-
include Elasticsearch::Model
|
23
|
-
include Elasticsearch::Model::Callbacks
|
24
|
-
|
25
|
-
document_type 'article'
|
26
|
-
|
27
|
-
mapping { indexes :title }
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
MyNamespace::Article.delete_all
|
32
|
-
MyNamespace::Article.__elasticsearch__.create_index! force: true
|
33
|
-
|
34
|
-
MyNamespace::Article.create! title: 'Test'
|
35
|
-
|
36
|
-
MyNamespace::Article.__elasticsearch__.refresh_index!
|
37
|
-
end
|
38
|
-
|
39
|
-
should "have proper index name and document type" do
|
40
|
-
assert_equal "my_namespace-articles", MyNamespace::Article.index_name
|
41
|
-
assert_equal "article", MyNamespace::Article.document_type
|
42
|
-
end
|
43
|
-
|
44
|
-
should "save document into index on save and find it" do
|
45
|
-
response = MyNamespace::Article.search 'title:test'
|
46
|
-
|
47
|
-
assert response.any?, "No results returned: #{response.inspect}"
|
48
|
-
assert_equal 1, response.size
|
49
|
-
|
50
|
-
assert_equal 'Test', response.results.first.title
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
@@ -1,149 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
# Needed for ActiveRecord 3.x ?
|
5
|
-
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
|
6
|
-
|
7
|
-
::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
|
8
|
-
|
9
|
-
module Elasticsearch
|
10
|
-
module Model
|
11
|
-
class ActiveRecordPaginationTest < Elasticsearch::Test::IntegrationTestCase
|
12
|
-
class ::ArticleForPagination < ActiveRecord::Base
|
13
|
-
include Elasticsearch::Model
|
14
|
-
|
15
|
-
scope :published, -> { where(published: true) }
|
16
|
-
|
17
|
-
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
|
18
|
-
mapping do
|
19
|
-
indexes :title, type: 'text', analyzer: 'snowball'
|
20
|
-
indexes :created_at, type: 'date'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
context "ActiveRecord pagination" do
|
26
|
-
setup do
|
27
|
-
ActiveRecord::Schema.define(:version => 1) do
|
28
|
-
create_table ::ArticleForPagination.table_name do |t|
|
29
|
-
t.string :title
|
30
|
-
t.datetime :created_at, :default => 'NOW()'
|
31
|
-
t.boolean :published
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
Kaminari::Hooks.init if defined?(Kaminari::Hooks)
|
36
|
-
|
37
|
-
ArticleForPagination.delete_all
|
38
|
-
ArticleForPagination.__elasticsearch__.create_index! force: true
|
39
|
-
|
40
|
-
68.times do |i|
|
41
|
-
::ArticleForPagination.create! title: "Test #{i}", published: (i % 2 == 0)
|
42
|
-
end
|
43
|
-
|
44
|
-
ArticleForPagination.import
|
45
|
-
ArticleForPagination.__elasticsearch__.refresh_index!
|
46
|
-
end
|
47
|
-
|
48
|
-
should "be on the first page by default" do
|
49
|
-
records = ArticleForPagination.search('title:test').page(1).records
|
50
|
-
|
51
|
-
assert_equal 25, records.size
|
52
|
-
assert_equal 1, records.current_page
|
53
|
-
assert_equal nil, records.prev_page
|
54
|
-
assert_equal 2, records.next_page
|
55
|
-
assert_equal 3, records.total_pages
|
56
|
-
|
57
|
-
assert records.first_page?, "Should be the first page"
|
58
|
-
assert ! records.last_page?, "Should NOT be the last page"
|
59
|
-
assert ! records.out_of_range?, "Should NOT be out of range"
|
60
|
-
end
|
61
|
-
|
62
|
-
should "load next page" do
|
63
|
-
records = ArticleForPagination.search('title:test').page(2).records
|
64
|
-
|
65
|
-
assert_equal 25, records.size
|
66
|
-
assert_equal 2, records.current_page
|
67
|
-
assert_equal 1, records.prev_page
|
68
|
-
assert_equal 3, records.next_page
|
69
|
-
assert_equal 3, records.total_pages
|
70
|
-
|
71
|
-
assert ! records.first_page?, "Should NOT be the first page"
|
72
|
-
assert ! records.last_page?, "Should NOT be the last page"
|
73
|
-
assert ! records.out_of_range?, "Should NOT be out of range"
|
74
|
-
end
|
75
|
-
|
76
|
-
should "load last page" do
|
77
|
-
records = ArticleForPagination.search('title:test').page(3).records
|
78
|
-
|
79
|
-
assert_equal 18, records.size
|
80
|
-
assert_equal 3, records.current_page
|
81
|
-
assert_equal 2, records.prev_page
|
82
|
-
assert_equal nil, records.next_page
|
83
|
-
assert_equal 3, records.total_pages
|
84
|
-
|
85
|
-
assert ! records.first_page?, "Should NOT be the first page"
|
86
|
-
assert records.last_page?, "Should be the last page"
|
87
|
-
assert ! records.out_of_range?, "Should NOT be out of range"
|
88
|
-
end
|
89
|
-
|
90
|
-
should "not load invalid page" do
|
91
|
-
records = ArticleForPagination.search('title:test').page(6).records
|
92
|
-
|
93
|
-
assert_equal 0, records.size
|
94
|
-
assert_equal 6, records.current_page
|
95
|
-
|
96
|
-
assert_equal nil, records.next_page
|
97
|
-
assert_equal 3, records.total_pages
|
98
|
-
|
99
|
-
assert ! records.first_page?, "Should NOT be the first page"
|
100
|
-
assert records.out_of_range?, "Should be out of range"
|
101
|
-
end
|
102
|
-
|
103
|
-
should "be combined with scopes" do
|
104
|
-
records = ArticleForPagination.search('title:test').page(2).records.published
|
105
|
-
assert records.all? { |r| r.published? }
|
106
|
-
assert_equal 12, records.size
|
107
|
-
end
|
108
|
-
|
109
|
-
should "respect sort" do
|
110
|
-
search = ArticleForPagination.search({ query: { match: { title: 'test' } }, sort: [ { id: 'desc' } ] })
|
111
|
-
|
112
|
-
records = search.page(2).records
|
113
|
-
assert_equal 43, records.first.id # 68 - 25 = 42
|
114
|
-
|
115
|
-
records = search.page(3).records
|
116
|
-
assert_equal 18, records.first.id # 68 - (2 * 25) = 18
|
117
|
-
|
118
|
-
records = search.page(2).per(5).records
|
119
|
-
assert_equal 63, records.first.id # 68 - 5 = 63
|
120
|
-
end
|
121
|
-
|
122
|
-
should "set the limit per request" do
|
123
|
-
records = ArticleForPagination.search('title:test').limit(50).page(2).records
|
124
|
-
|
125
|
-
assert_equal 18, records.size
|
126
|
-
assert_equal 2, records.current_page
|
127
|
-
assert_equal 1, records.prev_page
|
128
|
-
assert_equal nil, records.next_page
|
129
|
-
assert_equal 2, records.total_pages
|
130
|
-
|
131
|
-
assert records.last_page?, "Should be the last page"
|
132
|
-
end
|
133
|
-
|
134
|
-
context "with specific model settings" do
|
135
|
-
teardown do
|
136
|
-
ArticleForPagination.instance_variable_set(:@_default_per_page, nil)
|
137
|
-
end
|
138
|
-
|
139
|
-
should "respect paginates_per" do
|
140
|
-
ArticleForPagination.paginates_per 50
|
141
|
-
|
142
|
-
assert_equal 50, ArticleForPagination.search('*').page(1).records.size
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
require 'active_record'
|
3
|
-
|
4
|
-
# Needed for ActiveRecord 3.x ?
|
5
|
-
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" ) unless ActiveRecord::Base.connected?
|
6
|
-
|
7
|
-
::ActiveRecord::Base.raise_in_transactional_callbacks = true if ::ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks) && ::ActiveRecord::VERSION::MAJOR.to_s < '5'
|
8
|
-
|
9
|
-
module Elasticsearch
|
10
|
-
module Model
|
11
|
-
class DynamicIndexNameTest < Elasticsearch::Test::IntegrationTestCase
|
12
|
-
context "Dynamic index name" do
|
13
|
-
setup do
|
14
|
-
class ::ArticleWithDynamicIndexName < ActiveRecord::Base
|
15
|
-
include Elasticsearch::Model
|
16
|
-
include Elasticsearch::Model::Callbacks
|
17
|
-
|
18
|
-
def self.counter=(value)
|
19
|
-
@counter = 0
|
20
|
-
end
|
21
|
-
|
22
|
-
def self.counter
|
23
|
-
(@counter ||= 0) && @counter += 1
|
24
|
-
end
|
25
|
-
|
26
|
-
mapping { indexes :title }
|
27
|
-
index_name { "articles-#{counter}" }
|
28
|
-
end
|
29
|
-
|
30
|
-
::ActiveRecord::Schema.define(:version => 1) do
|
31
|
-
create_table ::ArticleWithDynamicIndexName.table_name do |t|
|
32
|
-
t.string :title
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
::ArticleWithDynamicIndexName.counter = 0
|
37
|
-
end
|
38
|
-
|
39
|
-
should 'evaluate the index_name value' do
|
40
|
-
assert_equal ArticleWithDynamicIndexName.index_name, "articles-1"
|
41
|
-
end
|
42
|
-
|
43
|
-
should 're-evaluate the index_name value each time' do
|
44
|
-
assert_equal ArticleWithDynamicIndexName.index_name, "articles-1"
|
45
|
-
assert_equal ArticleWithDynamicIndexName.index_name, "articles-2"
|
46
|
-
assert_equal ArticleWithDynamicIndexName.index_name, "articles-3"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,240 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
MongoDB.setup!
|
3
|
-
|
4
|
-
if MongoDB.available?
|
5
|
-
MongoDB.connect_to 'mongoid_articles'
|
6
|
-
|
7
|
-
module Elasticsearch
|
8
|
-
module Model
|
9
|
-
class MongoidBasicIntegrationTest < Elasticsearch::Test::IntegrationTestCase
|
10
|
-
|
11
|
-
class ::MongoidArticle
|
12
|
-
include Mongoid::Document
|
13
|
-
include Elasticsearch::Model
|
14
|
-
include Elasticsearch::Model::Callbacks
|
15
|
-
|
16
|
-
field :id, type: String
|
17
|
-
field :title, type: String
|
18
|
-
attr_accessible :title if respond_to? :attr_accessible
|
19
|
-
|
20
|
-
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
|
21
|
-
mapping do
|
22
|
-
indexes :title, type: 'text', analyzer: 'snowball'
|
23
|
-
indexes :created_at, type: 'date'
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def as_indexed_json(options={})
|
28
|
-
as_json(except: [:id, :_id])
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
context "Mongoid integration" do
|
33
|
-
setup do
|
34
|
-
Elasticsearch::Model::Adapter.register \
|
35
|
-
Elasticsearch::Model::Adapter::Mongoid,
|
36
|
-
lambda { |klass| !!defined?(::Mongoid::Document) && klass.respond_to?(:ancestors) && klass.ancestors.include?(::Mongoid::Document) }
|
37
|
-
|
38
|
-
MongoidArticle.__elasticsearch__.create_index! force: true
|
39
|
-
|
40
|
-
MongoidArticle.delete_all
|
41
|
-
|
42
|
-
MongoidArticle.create! title: 'Test'
|
43
|
-
MongoidArticle.create! title: 'Testing Coding'
|
44
|
-
MongoidArticle.create! title: 'Coding'
|
45
|
-
|
46
|
-
MongoidArticle.__elasticsearch__.refresh_index!
|
47
|
-
MongoidArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
|
48
|
-
end
|
49
|
-
|
50
|
-
should "index and find a document" do
|
51
|
-
response = MongoidArticle.search('title:test')
|
52
|
-
|
53
|
-
assert response.any?
|
54
|
-
|
55
|
-
assert_equal 2, response.results.size
|
56
|
-
assert_equal 2, response.records.size
|
57
|
-
|
58
|
-
assert_instance_of Elasticsearch::Model::Response::Result, response.results.first
|
59
|
-
assert_instance_of MongoidArticle, response.records.first
|
60
|
-
|
61
|
-
assert_equal 'Test', response.results.first.title
|
62
|
-
assert_equal 'Test', response.records.first.title
|
63
|
-
end
|
64
|
-
|
65
|
-
should "iterate over results" do
|
66
|
-
response = MongoidArticle.search('title:test')
|
67
|
-
|
68
|
-
assert_equal ['Test', 'Testing Coding'], response.results.map(&:title)
|
69
|
-
assert_equal ['Test', 'Testing Coding'], response.records.map(&:title)
|
70
|
-
end
|
71
|
-
|
72
|
-
should "access results from records" do
|
73
|
-
response = MongoidArticle.search('title:test')
|
74
|
-
|
75
|
-
response.records.each_with_hit do |r, h|
|
76
|
-
assert_not_nil h._score
|
77
|
-
assert_not_nil h._source.title
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
should "preserve the search results order for records" do
|
82
|
-
response = MongoidArticle.search('title:code')
|
83
|
-
|
84
|
-
response.records.each_with_hit do |r, h|
|
85
|
-
assert_equal h._id, r.id.to_s
|
86
|
-
end
|
87
|
-
|
88
|
-
response.records.map_with_hit do |r, h|
|
89
|
-
assert_equal h._id, r.id.to_s
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
should "remove document from index on destroy" do
|
94
|
-
article = MongoidArticle.first
|
95
|
-
|
96
|
-
article.destroy
|
97
|
-
assert_equal 2, MongoidArticle.count
|
98
|
-
|
99
|
-
MongoidArticle.__elasticsearch__.refresh_index!
|
100
|
-
|
101
|
-
response = MongoidArticle.search 'title:test'
|
102
|
-
|
103
|
-
assert_equal 1, response.results.size
|
104
|
-
assert_equal 1, response.records.size
|
105
|
-
end
|
106
|
-
|
107
|
-
should "index updates to the document" do
|
108
|
-
article = MongoidArticle.first
|
109
|
-
|
110
|
-
article.title = 'Writing'
|
111
|
-
article.save
|
112
|
-
|
113
|
-
MongoidArticle.__elasticsearch__.refresh_index!
|
114
|
-
|
115
|
-
response = MongoidArticle.search 'title:write'
|
116
|
-
|
117
|
-
assert_equal 1, response.results.size
|
118
|
-
assert_equal 1, response.records.size
|
119
|
-
end
|
120
|
-
|
121
|
-
should "return results for a DSL search" do
|
122
|
-
response = MongoidArticle.search query: { match: { title: { query: 'test' } } }
|
123
|
-
|
124
|
-
assert_equal 2, response.results.size
|
125
|
-
assert_equal 2, response.records.size
|
126
|
-
end
|
127
|
-
|
128
|
-
should "return a paged collection" do
|
129
|
-
response = MongoidArticle.search query: { match: { title: { query: 'test' } } },
|
130
|
-
size: 2,
|
131
|
-
from: 1
|
132
|
-
|
133
|
-
assert_equal 1, response.results.size
|
134
|
-
assert_equal 1, response.records.size
|
135
|
-
|
136
|
-
assert_equal 'Testing Coding', response.results.first.title
|
137
|
-
assert_equal 'Testing Coding', response.records.first.title
|
138
|
-
end
|
139
|
-
|
140
|
-
|
141
|
-
context "importing" do
|
142
|
-
setup do
|
143
|
-
MongoidArticle.delete_all
|
144
|
-
97.times { |i| MongoidArticle.create! title: "Test #{i}" }
|
145
|
-
MongoidArticle.__elasticsearch__.create_index! force: true
|
146
|
-
MongoidArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
|
147
|
-
end
|
148
|
-
|
149
|
-
should "import all the documents" do
|
150
|
-
assert_equal 97, MongoidArticle.count
|
151
|
-
|
152
|
-
MongoidArticle.__elasticsearch__.refresh_index!
|
153
|
-
assert_equal 0, MongoidArticle.search('*').results.total
|
154
|
-
|
155
|
-
batches = 0
|
156
|
-
errors = MongoidArticle.import(batch_size: 10) do |response|
|
157
|
-
batches += 1
|
158
|
-
end
|
159
|
-
|
160
|
-
assert_equal 0, errors
|
161
|
-
assert_equal 10, batches
|
162
|
-
|
163
|
-
MongoidArticle.__elasticsearch__.refresh_index!
|
164
|
-
assert_equal 97, MongoidArticle.search('*').results.total
|
165
|
-
|
166
|
-
response = MongoidArticle.search('test')
|
167
|
-
assert response.results.any?, "Search has not returned results: #{response.to_a}"
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
context "importing when the model has a default scope" do
|
172
|
-
class ::MongoidArticleWithDefaultScope
|
173
|
-
include Mongoid::Document
|
174
|
-
include Elasticsearch::Model
|
175
|
-
|
176
|
-
default_scope -> { where(status: 'active') }
|
177
|
-
|
178
|
-
field :id, type: String
|
179
|
-
field :title, type: String
|
180
|
-
field :status, type: String, default: 'active'
|
181
|
-
|
182
|
-
attr_accessible :title if respond_to? :attr_accessible
|
183
|
-
attr_accessible :status if respond_to? :attr_accessible
|
184
|
-
|
185
|
-
settings index: { number_of_shards: 1, number_of_replicas: 0 } do
|
186
|
-
mapping do
|
187
|
-
indexes :title, type: 'text', analyzer: 'snowball'
|
188
|
-
indexes :status, type: 'text'
|
189
|
-
indexes :created_at, type: 'date'
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
def as_indexed_json(options={})
|
194
|
-
as_json(except: [:id, :_id])
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
setup do
|
199
|
-
Elasticsearch::Model::Adapter.register \
|
200
|
-
Elasticsearch::Model::Adapter::Mongoid,
|
201
|
-
lambda { |klass| !!defined?(::Mongoid::Document) && klass.respond_to?(:ancestors) && klass.ancestors.include?(::Mongoid::Document) }
|
202
|
-
|
203
|
-
MongoidArticleWithDefaultScope.__elasticsearch__.create_index! force: true
|
204
|
-
|
205
|
-
MongoidArticleWithDefaultScope.delete_all
|
206
|
-
|
207
|
-
MongoidArticleWithDefaultScope.create! title: 'Test'
|
208
|
-
MongoidArticleWithDefaultScope.create! title: 'Testing Coding'
|
209
|
-
MongoidArticleWithDefaultScope.create! title: 'Coding'
|
210
|
-
MongoidArticleWithDefaultScope.create! title: 'Test legacy code', status: 'removed'
|
211
|
-
|
212
|
-
MongoidArticleWithDefaultScope.__elasticsearch__.refresh_index!
|
213
|
-
MongoidArticleWithDefaultScope.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
|
214
|
-
end
|
215
|
-
|
216
|
-
should "import only documents from the default scope" do
|
217
|
-
assert_equal 3, MongoidArticleWithDefaultScope.count
|
218
|
-
|
219
|
-
assert_equal 0, MongoidArticleWithDefaultScope.import
|
220
|
-
|
221
|
-
MongoidArticleWithDefaultScope.__elasticsearch__.refresh_index!
|
222
|
-
assert_equal 3, MongoidArticleWithDefaultScope.search('*').results.total
|
223
|
-
end
|
224
|
-
|
225
|
-
should "import only documents from a specific query combined with the default scope" do
|
226
|
-
assert_equal 3, MongoidArticleWithDefaultScope.count
|
227
|
-
|
228
|
-
assert_equal 0, MongoidArticleWithDefaultScope.import(query: -> { where(title: /^Test/) })
|
229
|
-
|
230
|
-
MongoidArticleWithDefaultScope.__elasticsearch__.refresh_index!
|
231
|
-
assert_equal 2, MongoidArticleWithDefaultScope.search('*').results.total
|
232
|
-
end
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
|
240
|
-
end
|