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.
- checksums.yaml +4 -4
- data/Gemfile +5 -0
- data/README.md +18 -11
- data/Rakefile +27 -36
- data/elasticsearch-model.gemspec +5 -5
- data/examples/activerecord_custom_analyzer.rb +135 -0
- data/examples/activerecord_mapping_completion.rb +3 -18
- data/examples/datamapper_article.rb +11 -1
- data/gemfiles/3.0.gemfile +6 -1
- data/gemfiles/4.0.gemfile +7 -1
- data/gemfiles/5.0.gemfile +7 -1
- data/lib/elasticsearch/model/adapters/active_record.rb +7 -23
- data/lib/elasticsearch/model/adapters/mongoid.rb +10 -3
- data/lib/elasticsearch/model/importing.rb +1 -1
- data/lib/elasticsearch/model/indexing.rb +6 -4
- data/lib/elasticsearch/model/naming.rb +9 -2
- data/lib/elasticsearch/model/response/aggregations.rb +1 -1
- data/lib/elasticsearch/model/response/base.rb +3 -2
- 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/pagination.rb +2 -192
- data/lib/elasticsearch/model/response/result.rb +1 -1
- data/lib/elasticsearch/model/response/suggestions.rb +1 -1
- data/lib/elasticsearch/model/response.rb +11 -10
- data/lib/elasticsearch/model/version.rb +1 -1
- data/lib/elasticsearch/model.rb +15 -8
- 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/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/app.rb +21 -0
- data/spec/support/model.json +1 -0
- metadata +134 -90
- data/test/integration/active_record_associations_parent_child_test.rb +0 -147
- data/test/integration/active_record_associations_test.rb +0 -339
- data/test/integration/active_record_basic_test.rb +0 -251
- data/test/integration/active_record_custom_serialization_test.rb +0 -67
- data/test/integration/active_record_import_test.rb +0 -115
- data/test/integration/active_record_namespaced_model_test.rb +0 -54
- 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 -176
- 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 -104
- 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 -203
- data/test/unit/indexing_test.rb +0 -687
- 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 -31
- 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
- /data/{test → spec}/support/model.yml +0 -0
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
module Elasticsearch
|
|
2
|
+
module Model
|
|
3
|
+
module Response
|
|
4
|
+
|
|
5
|
+
# Pagination for search results/records
|
|
6
|
+
#
|
|
7
|
+
module Pagination
|
|
8
|
+
# Allow models to be paginated with the "kaminari" gem [https://github.com/amatsuda/kaminari]
|
|
9
|
+
#
|
|
10
|
+
module Kaminari
|
|
11
|
+
def self.included(base)
|
|
12
|
+
# Include the Kaminari configuration and paging method in response
|
|
13
|
+
#
|
|
14
|
+
base.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
|
|
15
|
+
base.__send__ :include, ::Kaminari::PageScopeMethods
|
|
16
|
+
|
|
17
|
+
# Include the Kaminari paging methods in results and records
|
|
18
|
+
#
|
|
19
|
+
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
|
|
20
|
+
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::PageScopeMethods
|
|
21
|
+
Elasticsearch::Model::Response::Records.__send__ :include, ::Kaminari::PageScopeMethods
|
|
22
|
+
|
|
23
|
+
Elasticsearch::Model::Response::Results.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
|
|
24
|
+
Elasticsearch::Model::Response::Records.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
|
|
25
|
+
|
|
26
|
+
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
27
|
+
# Define the `page` Kaminari method
|
|
28
|
+
#
|
|
29
|
+
def #{::Kaminari.config.page_method_name}(num=nil)
|
|
30
|
+
@results = nil
|
|
31
|
+
@records = nil
|
|
32
|
+
@response = nil
|
|
33
|
+
@page = [num.to_i, 1].max
|
|
34
|
+
@per_page ||= __default_per_page
|
|
35
|
+
|
|
36
|
+
self.search.definition.update size: @per_page,
|
|
37
|
+
from: @per_page * (@page - 1)
|
|
38
|
+
|
|
39
|
+
self
|
|
40
|
+
end
|
|
41
|
+
RUBY
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Returns the current "limit" (`size`) value
|
|
45
|
+
#
|
|
46
|
+
def limit_value
|
|
47
|
+
case
|
|
48
|
+
when search.definition[:size]
|
|
49
|
+
search.definition[:size]
|
|
50
|
+
else
|
|
51
|
+
__default_per_page
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Returns the current "offset" (`from`) value
|
|
56
|
+
#
|
|
57
|
+
def offset_value
|
|
58
|
+
case
|
|
59
|
+
when search.definition[:from]
|
|
60
|
+
search.definition[:from]
|
|
61
|
+
else
|
|
62
|
+
0
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Set the "limit" (`size`) value
|
|
67
|
+
#
|
|
68
|
+
def limit(value)
|
|
69
|
+
return self if value.to_i <= 0
|
|
70
|
+
@results = nil
|
|
71
|
+
@records = nil
|
|
72
|
+
@response = nil
|
|
73
|
+
@per_page = value.to_i
|
|
74
|
+
|
|
75
|
+
search.definition.update :size => @per_page
|
|
76
|
+
search.definition.update :from => @per_page * (@page - 1) if @page
|
|
77
|
+
self
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Set the "offset" (`from`) value
|
|
81
|
+
#
|
|
82
|
+
def offset(value)
|
|
83
|
+
return self if value.to_i < 0
|
|
84
|
+
@results = nil
|
|
85
|
+
@records = nil
|
|
86
|
+
@response = nil
|
|
87
|
+
@page = nil
|
|
88
|
+
search.definition.update :from => value.to_i
|
|
89
|
+
self
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# Returns the total number of results
|
|
93
|
+
#
|
|
94
|
+
def total_count
|
|
95
|
+
results.total
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Returns the models's `per_page` value or the default
|
|
99
|
+
#
|
|
100
|
+
# @api private
|
|
101
|
+
#
|
|
102
|
+
def __default_per_page
|
|
103
|
+
klass.respond_to?(:default_per_page) && klass.default_per_page || ::Kaminari.config.default_per_page
|
|
104
|
+
end
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
module Elasticsearch
|
|
2
|
+
module Model
|
|
3
|
+
module Response
|
|
4
|
+
|
|
5
|
+
# Pagination for search results/records
|
|
6
|
+
#
|
|
7
|
+
module Pagination
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
# Allow models to be paginated with the "will_paginate" gem [https://github.com/mislav/will_paginate]
|
|
11
|
+
#
|
|
12
|
+
module WillPaginate
|
|
13
|
+
def self.included(base)
|
|
14
|
+
base.__send__ :include, ::WillPaginate::CollectionMethods
|
|
15
|
+
|
|
16
|
+
# Include the paging methods in results and records
|
|
17
|
+
#
|
|
18
|
+
methods = [:current_page, :offset, :length, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
|
|
19
|
+
Elasticsearch::Model::Response::Results.__send__ :delegate, *methods, to: :response
|
|
20
|
+
Elasticsearch::Model::Response::Records.__send__ :delegate, *methods, to: :response
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def offset
|
|
24
|
+
(current_page - 1) * per_page
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def length
|
|
28
|
+
search.definition[:size]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# Main pagination method
|
|
32
|
+
#
|
|
33
|
+
# @example
|
|
34
|
+
#
|
|
35
|
+
# Article.search('foo').paginate(page: 1, per_page: 30)
|
|
36
|
+
#
|
|
37
|
+
def paginate(options)
|
|
38
|
+
param_name = options[:param_name] || :page
|
|
39
|
+
page = [options[param_name].to_i, 1].max
|
|
40
|
+
per_page = (options[:per_page] || __default_per_page).to_i
|
|
41
|
+
|
|
42
|
+
search.definition.update size: per_page,
|
|
43
|
+
from: (page - 1) * per_page
|
|
44
|
+
self
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Return the current page
|
|
48
|
+
#
|
|
49
|
+
def current_page
|
|
50
|
+
search.definition[:from] / per_page + 1 if search.definition[:from] && per_page
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Pagination method
|
|
54
|
+
#
|
|
55
|
+
# @example
|
|
56
|
+
#
|
|
57
|
+
# Article.search('foo').page(2)
|
|
58
|
+
#
|
|
59
|
+
def page(num)
|
|
60
|
+
paginate(page: num, per_page: per_page) # shorthand
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Return or set the "size" value
|
|
64
|
+
#
|
|
65
|
+
# @example
|
|
66
|
+
#
|
|
67
|
+
# Article.search('foo').per_page(15).page(2)
|
|
68
|
+
#
|
|
69
|
+
def per_page(num = nil)
|
|
70
|
+
if num.nil?
|
|
71
|
+
search.definition[:size]
|
|
72
|
+
else
|
|
73
|
+
paginate(page: current_page, per_page: num) # shorthand
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Returns the total number of results
|
|
78
|
+
#
|
|
79
|
+
def total_entries
|
|
80
|
+
results.total
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# Returns the models's `per_page` value or the default
|
|
84
|
+
#
|
|
85
|
+
# @api private
|
|
86
|
+
#
|
|
87
|
+
def __default_per_page
|
|
88
|
+
klass.respond_to?(:per_page) && klass.per_page || ::WillPaginate.per_page
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -1,192 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
module Response
|
|
4
|
-
|
|
5
|
-
# Pagination for search results/records
|
|
6
|
-
#
|
|
7
|
-
module Pagination
|
|
8
|
-
# Allow models to be paginated with the "kaminari" gem [https://github.com/amatsuda/kaminari]
|
|
9
|
-
#
|
|
10
|
-
module Kaminari
|
|
11
|
-
def self.included(base)
|
|
12
|
-
# Include the Kaminari configuration and paging method in response
|
|
13
|
-
#
|
|
14
|
-
base.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
|
|
15
|
-
base.__send__ :include, ::Kaminari::PageScopeMethods
|
|
16
|
-
|
|
17
|
-
# Include the Kaminari paging methods in results and records
|
|
18
|
-
#
|
|
19
|
-
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::ConfigurationMethods::ClassMethods
|
|
20
|
-
Elasticsearch::Model::Response::Results.__send__ :include, ::Kaminari::PageScopeMethods
|
|
21
|
-
Elasticsearch::Model::Response::Records.__send__ :include, ::Kaminari::PageScopeMethods
|
|
22
|
-
|
|
23
|
-
Elasticsearch::Model::Response::Results.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
|
|
24
|
-
Elasticsearch::Model::Response::Records.__send__ :delegate, :limit_value, :offset_value, :total_count, :max_pages, to: :response
|
|
25
|
-
|
|
26
|
-
base.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
|
27
|
-
# Define the `page` Kaminari method
|
|
28
|
-
#
|
|
29
|
-
def #{::Kaminari.config.page_method_name}(num=nil)
|
|
30
|
-
@results = nil
|
|
31
|
-
@records = nil
|
|
32
|
-
@response = nil
|
|
33
|
-
@page = [num.to_i, 1].max
|
|
34
|
-
@per_page ||= __default_per_page
|
|
35
|
-
|
|
36
|
-
self.search.definition.update size: @per_page,
|
|
37
|
-
from: @per_page * (@page - 1)
|
|
38
|
-
|
|
39
|
-
self
|
|
40
|
-
end
|
|
41
|
-
RUBY
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
# Returns the current "limit" (`size`) value
|
|
45
|
-
#
|
|
46
|
-
def limit_value
|
|
47
|
-
case
|
|
48
|
-
when search.definition[:size]
|
|
49
|
-
search.definition[:size]
|
|
50
|
-
else
|
|
51
|
-
__default_per_page
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
# Returns the current "offset" (`from`) value
|
|
56
|
-
#
|
|
57
|
-
def offset_value
|
|
58
|
-
case
|
|
59
|
-
when search.definition[:from]
|
|
60
|
-
search.definition[:from]
|
|
61
|
-
else
|
|
62
|
-
0
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# Set the "limit" (`size`) value
|
|
67
|
-
#
|
|
68
|
-
def limit(value)
|
|
69
|
-
return self if value.to_i <= 0
|
|
70
|
-
@results = nil
|
|
71
|
-
@records = nil
|
|
72
|
-
@response = nil
|
|
73
|
-
@per_page = value.to_i
|
|
74
|
-
|
|
75
|
-
search.definition.update :size => @per_page
|
|
76
|
-
search.definition.update :from => @per_page * (@page - 1) if @page
|
|
77
|
-
self
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
# Set the "offset" (`from`) value
|
|
81
|
-
#
|
|
82
|
-
def offset(value)
|
|
83
|
-
return self if value.to_i < 0
|
|
84
|
-
@results = nil
|
|
85
|
-
@records = nil
|
|
86
|
-
@response = nil
|
|
87
|
-
@page = nil
|
|
88
|
-
search.definition.update :from => value.to_i
|
|
89
|
-
self
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
# Returns the total number of results
|
|
93
|
-
#
|
|
94
|
-
def total_count
|
|
95
|
-
results.total
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
# Returns the models's `per_page` value or the default
|
|
99
|
-
#
|
|
100
|
-
# @api private
|
|
101
|
-
#
|
|
102
|
-
def __default_per_page
|
|
103
|
-
klass.respond_to?(:default_per_page) && klass.default_per_page || ::Kaminari.config.default_per_page
|
|
104
|
-
end
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
# Allow models to be paginated with the "will_paginate" gem [https://github.com/mislav/will_paginate]
|
|
108
|
-
#
|
|
109
|
-
module WillPaginate
|
|
110
|
-
def self.included(base)
|
|
111
|
-
base.__send__ :include, ::WillPaginate::CollectionMethods
|
|
112
|
-
|
|
113
|
-
# Include the paging methods in results and records
|
|
114
|
-
#
|
|
115
|
-
methods = [:current_page, :offset, :length, :per_page, :total_entries, :total_pages, :previous_page, :next_page, :out_of_bounds?]
|
|
116
|
-
Elasticsearch::Model::Response::Results.__send__ :delegate, *methods, to: :response
|
|
117
|
-
Elasticsearch::Model::Response::Records.__send__ :delegate, *methods, to: :response
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def offset
|
|
121
|
-
(current_page - 1) * per_page
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def length
|
|
125
|
-
search.definition[:size]
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
# Main pagination method
|
|
129
|
-
#
|
|
130
|
-
# @example
|
|
131
|
-
#
|
|
132
|
-
# Article.search('foo').paginate(page: 1, per_page: 30)
|
|
133
|
-
#
|
|
134
|
-
def paginate(options)
|
|
135
|
-
param_name = options[:param_name] || :page
|
|
136
|
-
page = [options[param_name].to_i, 1].max
|
|
137
|
-
per_page = (options[:per_page] || __default_per_page).to_i
|
|
138
|
-
|
|
139
|
-
search.definition.update size: per_page,
|
|
140
|
-
from: (page - 1) * per_page
|
|
141
|
-
self
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
# Return the current page
|
|
145
|
-
#
|
|
146
|
-
def current_page
|
|
147
|
-
search.definition[:from] / per_page + 1 if search.definition[:from] && per_page
|
|
148
|
-
end
|
|
149
|
-
|
|
150
|
-
# Pagination method
|
|
151
|
-
#
|
|
152
|
-
# @example
|
|
153
|
-
#
|
|
154
|
-
# Article.search('foo').page(2)
|
|
155
|
-
#
|
|
156
|
-
def page(num)
|
|
157
|
-
paginate(page: num, per_page: per_page) # shorthand
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
# Return or set the "size" value
|
|
161
|
-
#
|
|
162
|
-
# @example
|
|
163
|
-
#
|
|
164
|
-
# Article.search('foo').per_page(15).page(2)
|
|
165
|
-
#
|
|
166
|
-
def per_page(num = nil)
|
|
167
|
-
if num.nil?
|
|
168
|
-
search.definition[:size]
|
|
169
|
-
else
|
|
170
|
-
paginate(page: current_page, per_page: num) # shorthand
|
|
171
|
-
end
|
|
172
|
-
end
|
|
173
|
-
|
|
174
|
-
# Returns the total number of results
|
|
175
|
-
#
|
|
176
|
-
def total_entries
|
|
177
|
-
results.total
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
# Returns the models's `per_page` value or the default
|
|
181
|
-
#
|
|
182
|
-
# @api private
|
|
183
|
-
#
|
|
184
|
-
def __default_per_page
|
|
185
|
-
klass.respond_to?(:per_page) && klass.per_page || ::WillPaginate.per_page
|
|
186
|
-
end
|
|
187
|
-
end
|
|
188
|
-
end
|
|
189
|
-
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
end
|
|
1
|
+
require 'elasticsearch/model/response/pagination/kaminari'
|
|
2
|
+
require 'elasticsearch/model/response/pagination/will_paginate'
|
|
@@ -46,7 +46,7 @@ module Elasticsearch
|
|
|
46
46
|
|
|
47
47
|
# Respond to methods from `@result` or `@result._source`
|
|
48
48
|
#
|
|
49
|
-
def
|
|
49
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
50
50
|
@result.respond_to?(method_name.to_sym) || \
|
|
51
51
|
@result._source && @result._source.respond_to?(method_name.to_sym) || \
|
|
52
52
|
super
|
|
@@ -10,8 +10,7 @@ module Elasticsearch
|
|
|
10
10
|
# Implements Enumerable and forwards its methods to the {#results} object.
|
|
11
11
|
#
|
|
12
12
|
class Response
|
|
13
|
-
attr_reader :klass, :search
|
|
14
|
-
:took, :timed_out, :shards
|
|
13
|
+
attr_reader :klass, :search
|
|
15
14
|
|
|
16
15
|
include Enumerable
|
|
17
16
|
|
|
@@ -27,9 +26,7 @@ module Elasticsearch
|
|
|
27
26
|
# @return [Hash]
|
|
28
27
|
#
|
|
29
28
|
def response
|
|
30
|
-
@response ||=
|
|
31
|
-
HashWrapper.new(search.execute!)
|
|
32
|
-
end
|
|
29
|
+
@response ||= HashWrapper.new(search.execute!)
|
|
33
30
|
end
|
|
34
31
|
|
|
35
32
|
# Returns the collection of "hits" from Elasticsearch
|
|
@@ -51,31 +48,35 @@ module Elasticsearch
|
|
|
51
48
|
# Returns the "took" time
|
|
52
49
|
#
|
|
53
50
|
def took
|
|
54
|
-
|
|
51
|
+
raw_response['took']
|
|
55
52
|
end
|
|
56
53
|
|
|
57
54
|
# Returns whether the response timed out
|
|
58
55
|
#
|
|
59
56
|
def timed_out
|
|
60
|
-
|
|
57
|
+
raw_response['timed_out']
|
|
61
58
|
end
|
|
62
59
|
|
|
63
60
|
# Returns the statistics on shards
|
|
64
61
|
#
|
|
65
62
|
def shards
|
|
66
|
-
|
|
63
|
+
@shards ||= response['_shards']
|
|
67
64
|
end
|
|
68
65
|
|
|
69
66
|
# Returns a Hashie::Mash of the aggregations
|
|
70
67
|
#
|
|
71
68
|
def aggregations
|
|
72
|
-
Aggregations.new(
|
|
69
|
+
@aggregations ||= Aggregations.new(raw_response['aggregations'])
|
|
73
70
|
end
|
|
74
71
|
|
|
75
72
|
# Returns a Hashie::Mash of the suggestions
|
|
76
73
|
#
|
|
77
74
|
def suggestions
|
|
78
|
-
Suggestions.new(
|
|
75
|
+
@suggestions ||= Suggestions.new(raw_response['suggest'])
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def raw_response
|
|
79
|
+
@raw_response ||= @response ? @response.to_hash : search.execute!
|
|
79
80
|
end
|
|
80
81
|
end
|
|
81
82
|
end
|
data/lib/elasticsearch/model.rb
CHANGED
|
@@ -131,12 +131,6 @@ module Elasticsearch
|
|
|
131
131
|
end
|
|
132
132
|
end
|
|
133
133
|
|
|
134
|
-
# Access the module settings
|
|
135
|
-
#
|
|
136
|
-
def self.settings
|
|
137
|
-
@settings ||= {}
|
|
138
|
-
end
|
|
139
|
-
|
|
140
134
|
module ClassMethods
|
|
141
135
|
# Get the client common for all models
|
|
142
136
|
#
|
|
@@ -193,7 +187,7 @@ module Elasticsearch
|
|
|
193
187
|
# @note Inheritance is disabled by default.
|
|
194
188
|
#
|
|
195
189
|
def inheritance_enabled
|
|
196
|
-
@inheritance_enabled ||= false
|
|
190
|
+
@settings[:inheritance_enabled] ||= false
|
|
197
191
|
end
|
|
198
192
|
|
|
199
193
|
# Enable inheritance of index_name and document_type
|
|
@@ -203,8 +197,21 @@ module Elasticsearch
|
|
|
203
197
|
# Elasticsearch::Model.inheritance_enabled = true
|
|
204
198
|
#
|
|
205
199
|
def inheritance_enabled=(inheritance_enabled)
|
|
206
|
-
|
|
200
|
+
warn STI_DEPRECATION_WARNING if inheritance_enabled
|
|
201
|
+
@settings[:inheritance_enabled] = inheritance_enabled
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
# Access the module settings
|
|
205
|
+
#
|
|
206
|
+
def settings
|
|
207
|
+
@settings ||= {}
|
|
207
208
|
end
|
|
209
|
+
|
|
210
|
+
private
|
|
211
|
+
|
|
212
|
+
STI_DEPRECATION_WARNING = "DEPRECATION WARNING: Support for Single Table Inheritance (STI) is deprecated " +
|
|
213
|
+
"and will be removed in version 7.0.0.\nPlease save different model documents in separate indices and refer " +
|
|
214
|
+
"to the Elasticsearch documentation for more information.".freeze
|
|
208
215
|
end
|
|
209
216
|
extend ClassMethods
|
|
210
217
|
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Elasticsearch::Model::Adapter do
|
|
4
|
+
|
|
5
|
+
before(:all) do
|
|
6
|
+
class ::DummyAdapterClass; end
|
|
7
|
+
class ::DummyAdapterClassWithAdapter; end
|
|
8
|
+
class ::DummyAdapter
|
|
9
|
+
Records = Module.new
|
|
10
|
+
Callbacks = Module.new
|
|
11
|
+
Importing = Module.new
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
after(:all) do
|
|
16
|
+
[DummyAdapterClassWithAdapter, DummyAdapterClass, DummyAdapter].each do |adapter|
|
|
17
|
+
Elasticsearch::Model::Adapter::Adapter.adapters.delete(adapter)
|
|
18
|
+
end
|
|
19
|
+
remove_classes(DummyAdapterClass, DummyAdapterClassWithAdapter, DummyAdapter)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe '#from_class' do
|
|
23
|
+
|
|
24
|
+
it 'should return an Adapter instance' do
|
|
25
|
+
expect(Elasticsearch::Model::Adapter.from_class(DummyAdapterClass)).to be_a(Elasticsearch::Model::Adapter::Adapter)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe 'register' do
|
|
30
|
+
|
|
31
|
+
before do
|
|
32
|
+
expect(Elasticsearch::Model::Adapter::Adapter).to receive(:register).and_call_original
|
|
33
|
+
Elasticsearch::Model::Adapter.register(:foo, lambda { |c| false })
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it 'should register an adapter' do
|
|
37
|
+
expect(Elasticsearch::Model::Adapter::Adapter.adapters[:foo]).to be_a(Proc)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
context 'when a specific adapter class is set' do
|
|
41
|
+
|
|
42
|
+
before do
|
|
43
|
+
expect(Elasticsearch::Model::Adapter::Adapter).to receive(:register).and_call_original
|
|
44
|
+
Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
|
|
45
|
+
lambda { |c| c == DummyAdapterClassWithAdapter })
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
let(:adapter) do
|
|
49
|
+
Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it 'should register the adapter' do
|
|
53
|
+
expect(adapter.adapter).to eq(DummyAdapter)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
describe 'default adapter' do
|
|
59
|
+
|
|
60
|
+
let(:adapter) do
|
|
61
|
+
Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClass)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it 'sets a default adapter' do
|
|
65
|
+
expect(adapter.adapter).to eq(Elasticsearch::Model::Adapter::Default)
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
describe '#records_mixin' do
|
|
70
|
+
|
|
71
|
+
before do
|
|
72
|
+
Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
|
|
73
|
+
lambda { |c| c == DummyAdapterClassWithAdapter })
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
let(:adapter) do
|
|
78
|
+
Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it 'returns a Module' do
|
|
82
|
+
expect(adapter.records_mixin).to be_a(Module)
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
describe '#callbacks_mixin' do
|
|
87
|
+
|
|
88
|
+
before do
|
|
89
|
+
Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
|
|
90
|
+
lambda { |c| c == DummyAdapterClassWithAdapter })
|
|
91
|
+
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
let(:adapter) do
|
|
95
|
+
Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it 'returns a Module' do
|
|
99
|
+
expect(adapter.callbacks_mixin).to be_a(Module)
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
describe '#importing_mixin' do
|
|
104
|
+
|
|
105
|
+
before do
|
|
106
|
+
Elasticsearch::Model::Adapter::Adapter.register(DummyAdapter,
|
|
107
|
+
lambda { |c| c == DummyAdapterClassWithAdapter })
|
|
108
|
+
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
let(:adapter) do
|
|
112
|
+
Elasticsearch::Model::Adapter::Adapter.new(DummyAdapterClassWithAdapter)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
it 'returns a Module' do
|
|
116
|
+
expect(adapter.importing_mixin).to be_a(Module)
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
end
|