elasticsearch-model 5.1.0 → 6.1.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.
Files changed (125) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +5 -0
  3. data/README.md +18 -11
  4. data/Rakefile +27 -36
  5. data/elasticsearch-model.gemspec +5 -5
  6. data/examples/activerecord_custom_analyzer.rb +135 -0
  7. data/examples/activerecord_mapping_completion.rb +3 -18
  8. data/examples/datamapper_article.rb +11 -1
  9. data/gemfiles/3.0.gemfile +6 -1
  10. data/gemfiles/4.0.gemfile +7 -1
  11. data/gemfiles/5.0.gemfile +7 -1
  12. data/lib/elasticsearch/model/adapters/active_record.rb +7 -23
  13. data/lib/elasticsearch/model/adapters/mongoid.rb +10 -3
  14. data/lib/elasticsearch/model/importing.rb +1 -1
  15. data/lib/elasticsearch/model/indexing.rb +6 -4
  16. data/lib/elasticsearch/model/naming.rb +9 -2
  17. data/lib/elasticsearch/model/response/aggregations.rb +1 -1
  18. data/lib/elasticsearch/model/response/base.rb +3 -2
  19. data/lib/elasticsearch/model/response/pagination/kaminari.rb +109 -0
  20. data/lib/elasticsearch/model/response/pagination/will_paginate.rb +95 -0
  21. data/lib/elasticsearch/model/response/pagination.rb +2 -192
  22. data/lib/elasticsearch/model/response/result.rb +1 -1
  23. data/lib/elasticsearch/model/response/suggestions.rb +1 -1
  24. data/lib/elasticsearch/model/response.rb +11 -10
  25. data/lib/elasticsearch/model/version.rb +1 -1
  26. data/lib/elasticsearch/model.rb +15 -8
  27. data/spec/elasticsearch/model/adapter_spec.rb +119 -0
  28. data/spec/elasticsearch/model/adapters/active_record/associations_spec.rb +334 -0
  29. data/spec/elasticsearch/model/adapters/active_record/basic_spec.rb +340 -0
  30. data/spec/elasticsearch/model/adapters/active_record/dynamic_index_name_spec.rb +18 -0
  31. data/spec/elasticsearch/model/adapters/active_record/import_spec.rb +187 -0
  32. data/spec/elasticsearch/model/adapters/active_record/multi_model_spec.rb +110 -0
  33. data/spec/elasticsearch/model/adapters/active_record/namespaced_model_spec.rb +38 -0
  34. data/spec/elasticsearch/model/adapters/active_record/pagination_spec.rb +315 -0
  35. data/spec/elasticsearch/model/adapters/active_record/parent_child_spec.rb +75 -0
  36. data/spec/elasticsearch/model/adapters/active_record/serialization_spec.rb +61 -0
  37. data/spec/elasticsearch/model/adapters/active_record_spec.rb +207 -0
  38. data/spec/elasticsearch/model/adapters/default_spec.rb +41 -0
  39. data/spec/elasticsearch/model/adapters/mongoid/basic_spec.rb +267 -0
  40. data/spec/elasticsearch/model/adapters/mongoid/multi_model_spec.rb +66 -0
  41. data/spec/elasticsearch/model/adapters/mongoid_spec.rb +235 -0
  42. data/spec/elasticsearch/model/adapters/multiple_spec.rb +125 -0
  43. data/spec/elasticsearch/model/callbacks_spec.rb +33 -0
  44. data/spec/elasticsearch/model/client_spec.rb +66 -0
  45. data/spec/elasticsearch/model/hash_wrapper_spec.rb +12 -0
  46. data/spec/elasticsearch/model/importing_spec.rb +214 -0
  47. data/spec/elasticsearch/model/indexing_spec.rb +918 -0
  48. data/spec/elasticsearch/model/module_spec.rb +101 -0
  49. data/spec/elasticsearch/model/multimodel_spec.rb +55 -0
  50. data/spec/elasticsearch/model/naming_inheritance_spec.rb +184 -0
  51. data/spec/elasticsearch/model/naming_spec.rb +186 -0
  52. data/spec/elasticsearch/model/proxy_spec.rb +107 -0
  53. data/spec/elasticsearch/model/response/aggregations_spec.rb +66 -0
  54. data/spec/elasticsearch/model/response/base_spec.rb +90 -0
  55. data/spec/elasticsearch/model/response/pagination/kaminari_spec.rb +410 -0
  56. data/spec/elasticsearch/model/response/pagination/will_paginate_spec.rb +262 -0
  57. data/spec/elasticsearch/model/response/records_spec.rb +118 -0
  58. data/spec/elasticsearch/model/response/response_spec.rb +131 -0
  59. data/spec/elasticsearch/model/response/result_spec.rb +122 -0
  60. data/spec/elasticsearch/model/response/results_spec.rb +56 -0
  61. data/spec/elasticsearch/model/searching_search_request_spec.rb +112 -0
  62. data/spec/elasticsearch/model/searching_spec.rb +49 -0
  63. data/spec/elasticsearch/model/serializing_spec.rb +22 -0
  64. data/spec/spec_helper.rb +161 -0
  65. data/spec/support/app/answer.rb +33 -0
  66. data/spec/support/app/article.rb +22 -0
  67. data/spec/support/app/article_for_pagination.rb +12 -0
  68. data/spec/support/app/article_with_custom_serialization.rb +13 -0
  69. data/spec/support/app/article_with_dynamic_index_name.rb +15 -0
  70. data/spec/support/app/author.rb +9 -0
  71. data/spec/support/app/authorship.rb +4 -0
  72. data/spec/support/app/category.rb +3 -0
  73. data/spec/support/app/comment.rb +3 -0
  74. data/spec/support/app/episode.rb +11 -0
  75. data/spec/support/app/image.rb +19 -0
  76. data/spec/support/app/import_article.rb +12 -0
  77. data/spec/support/app/mongoid_article.rb +21 -0
  78. data/spec/support/app/namespaced_book.rb +10 -0
  79. data/spec/support/app/parent_and_child_searchable.rb +24 -0
  80. data/spec/support/app/post.rb +14 -0
  81. data/spec/support/app/question.rb +27 -0
  82. data/spec/support/app/searchable.rb +48 -0
  83. data/spec/support/app/series.rb +11 -0
  84. data/spec/support/app.rb +21 -0
  85. data/spec/support/model.json +1 -0
  86. metadata +134 -90
  87. data/test/integration/active_record_associations_parent_child_test.rb +0 -147
  88. data/test/integration/active_record_associations_test.rb +0 -339
  89. data/test/integration/active_record_basic_test.rb +0 -251
  90. data/test/integration/active_record_custom_serialization_test.rb +0 -67
  91. data/test/integration/active_record_import_test.rb +0 -115
  92. data/test/integration/active_record_namespaced_model_test.rb +0 -54
  93. data/test/integration/active_record_pagination_test.rb +0 -149
  94. data/test/integration/dynamic_index_name_test.rb +0 -52
  95. data/test/integration/mongoid_basic_test.rb +0 -176
  96. data/test/integration/multiple_models_test.rb +0 -176
  97. data/test/support/model.json +0 -1
  98. data/test/test_helper.rb +0 -92
  99. data/test/unit/adapter_active_record_test.rb +0 -157
  100. data/test/unit/adapter_default_test.rb +0 -41
  101. data/test/unit/adapter_mongoid_test.rb +0 -104
  102. data/test/unit/adapter_multiple_test.rb +0 -106
  103. data/test/unit/adapter_test.rb +0 -69
  104. data/test/unit/callbacks_test.rb +0 -31
  105. data/test/unit/client_test.rb +0 -27
  106. data/test/unit/hash_wrapper_test.rb +0 -13
  107. data/test/unit/importing_test.rb +0 -203
  108. data/test/unit/indexing_test.rb +0 -687
  109. data/test/unit/module_test.rb +0 -68
  110. data/test/unit/multimodel_test.rb +0 -38
  111. data/test/unit/naming_inheritance_test.rb +0 -94
  112. data/test/unit/naming_test.rb +0 -103
  113. data/test/unit/proxy_test.rb +0 -98
  114. data/test/unit/response_aggregations_test.rb +0 -46
  115. data/test/unit/response_base_test.rb +0 -40
  116. data/test/unit/response_pagination_kaminari_test.rb +0 -433
  117. data/test/unit/response_pagination_will_paginate_test.rb +0 -398
  118. data/test/unit/response_records_test.rb +0 -91
  119. data/test/unit/response_result_test.rb +0 -90
  120. data/test/unit/response_results_test.rb +0 -31
  121. data/test/unit/response_test.rb +0 -104
  122. data/test/unit/searching_search_request_test.rb +0 -78
  123. data/test/unit/searching_test.rb +0 -41
  124. data/test/unit/serializing_test.rb +0 -17
  125. /data/{test → spec}/support/model.yml +0 -0
@@ -1,115 +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 ActiveRecordImportIntegrationTest < Elasticsearch::Test::IntegrationTestCase
12
-
13
- class ::ImportArticle < ActiveRecord::Base
14
- include Elasticsearch::Model
15
-
16
- scope :popular, -> { where('views >= 50') }
17
-
18
- mapping do
19
- indexes :title, type: 'text'
20
- indexes :views, type: 'integer'
21
- indexes :numeric, type: 'integer'
22
- indexes :created_at, type: 'date'
23
- end
24
- end
25
-
26
- context "ActiveRecord importing" do
27
- setup do
28
- ActiveRecord::Schema.define(:version => 1) do
29
- create_table :import_articles do |t|
30
- t.string :title
31
- t.integer :views
32
- t.string :numeric # For the sake of invalid data sent to Elasticsearch
33
- t.datetime :created_at, :default => 'NOW()'
34
- end
35
- end
36
-
37
- ImportArticle.delete_all
38
- ImportArticle.__elasticsearch__.create_index! force: true
39
- ImportArticle.__elasticsearch__.client.cluster.health wait_for_status: 'yellow'
40
-
41
- 100.times { |i| ImportArticle.create! title: "Test #{i}", views: i }
42
- end
43
-
44
- should "import all the documents" do
45
- assert_equal 100, ImportArticle.count
46
-
47
- ImportArticle.__elasticsearch__.refresh_index!
48
- assert_equal 0, ImportArticle.search('*').results.total
49
-
50
- batches = 0
51
- errors = ImportArticle.import(batch_size: 10) do |response|
52
- batches += 1
53
- end
54
-
55
- assert_equal 0, errors
56
- assert_equal 10, batches
57
-
58
- ImportArticle.__elasticsearch__.refresh_index!
59
- assert_equal 100, ImportArticle.search('*').results.total
60
- end
61
-
62
- should "import only documents from a specific scope" do
63
- assert_equal 100, ImportArticle.count
64
-
65
- assert_equal 0, ImportArticle.import(scope: 'popular')
66
-
67
- ImportArticle.__elasticsearch__.refresh_index!
68
- assert_equal 50, ImportArticle.search('*').results.total
69
- end
70
-
71
- should "import only documents from a specific query" do
72
- assert_equal 100, ImportArticle.count
73
-
74
- assert_equal 0, ImportArticle.import(query: -> { where('views >= 30') })
75
-
76
- ImportArticle.__elasticsearch__.refresh_index!
77
- assert_equal 70, ImportArticle.search('*').results.total
78
- end
79
-
80
- should "report and not store/index invalid documents" do
81
- ImportArticle.create! title: "Test INVALID", numeric: "INVALID"
82
-
83
- assert_equal 101, ImportArticle.count
84
-
85
- ImportArticle.__elasticsearch__.refresh_index!
86
- assert_equal 0, ImportArticle.search('*').results.total
87
-
88
- batches = 0
89
- errors = ImportArticle.__elasticsearch__.import(batch_size: 10) do |response|
90
- batches += 1
91
- end
92
-
93
- assert_equal 1, errors
94
- assert_equal 11, batches
95
-
96
- ImportArticle.__elasticsearch__.refresh_index!
97
- assert_equal 100, ImportArticle.search('*').results.total
98
- end
99
-
100
- should "transform documents with the option" do
101
- assert_equal 100, ImportArticle.count
102
-
103
- assert_equal 0, ImportArticle.import( transform: ->(a) {{ index: { data: { name: a.title, foo: 'BAR' } }}} )
104
-
105
- ImportArticle.__elasticsearch__.refresh_index!
106
- assert_contains ImportArticle.search('*').results.first._source.keys, 'name'
107
- assert_contains ImportArticle.search('*').results.first._source.keys, 'foo'
108
- assert_equal 100, ImportArticle.search('test').results.total
109
- assert_equal 100, ImportArticle.search('bar').results.total
110
- end
111
- end
112
-
113
- end
114
- end
115
- end
@@ -1,54 +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
- mapping { indexes :title }
26
- end
27
- end
28
-
29
- MyNamespace::Article.delete_all
30
- MyNamespace::Article.__elasticsearch__.create_index! force: true
31
-
32
- MyNamespace::Article.create! title: 'Test'
33
-
34
- MyNamespace::Article.__elasticsearch__.refresh_index!
35
- end
36
-
37
- should "have proper index name and document type" do
38
- assert_equal "my_namespace-articles", MyNamespace::Article.index_name
39
- assert_equal "article", MyNamespace::Article.document_type
40
- end
41
-
42
- should "save document into index on save and find it" do
43
- response = MyNamespace::Article.search 'title:test'
44
-
45
- assert response.any?, "No results returned: #{response.inspect}"
46
- assert_equal 1, response.size
47
-
48
- assert_equal 'Test', response.results.first.title
49
- end
50
- end
51
-
52
- end
53
- end
54
- 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,176 +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
- end
171
-
172
- end
173
- end
174
- end
175
-
176
- end