chewy 0.9.0 → 5.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.circleci/config.yml +214 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +41 -19
- data/.rubocop_todo.yml +2 -2
- data/.yardopts +5 -0
- data/Appraisals +58 -28
- data/CHANGELOG.md +153 -12
- data/Gemfile +20 -12
- data/LEGACY_DSL.md +497 -0
- data/LICENSE.txt +1 -1
- data/README.md +338 -528
- data/chewy.gemspec +11 -12
- data/gemfiles/rails.5.2.activerecord.gemfile +17 -0
- data/gemfiles/rails.5.2.mongoid.6.4.gemfile +17 -0
- data/gemfiles/rails.6.0.activerecord.gemfile +17 -0
- data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
- data/gemfiles/ruby3.gemfile +10 -0
- data/gemfiles/sequel.4.45.gemfile +11 -0
- data/lib/chewy.rb +79 -44
- data/lib/chewy/backports/duplicable.rb +1 -1
- data/lib/chewy/config.rb +43 -17
- data/lib/chewy/errors.rb +2 -2
- data/lib/chewy/fields/base.rb +56 -31
- data/lib/chewy/fields/root.rb +44 -11
- data/lib/chewy/index.rb +237 -149
- data/lib/chewy/index/actions.rb +100 -35
- data/lib/chewy/index/aliases.rb +2 -1
- data/lib/chewy/index/settings.rb +11 -5
- data/lib/chewy/index/specification.rb +60 -0
- data/lib/chewy/journal.rb +40 -92
- data/lib/chewy/minitest/helpers.rb +6 -6
- data/lib/chewy/minitest/search_index_receiver.rb +17 -17
- data/lib/chewy/query.rb +182 -122
- data/lib/chewy/query/compose.rb +13 -13
- data/lib/chewy/query/criteria.rb +13 -13
- data/lib/chewy/query/filters.rb +21 -4
- data/lib/chewy/query/loading.rb +1 -2
- data/lib/chewy/query/nodes/and.rb +2 -2
- data/lib/chewy/query/nodes/bool.rb +1 -1
- data/lib/chewy/query/nodes/equal.rb +2 -2
- data/lib/chewy/query/nodes/exists.rb +1 -1
- data/lib/chewy/query/nodes/field.rb +1 -1
- data/lib/chewy/query/nodes/has_relation.rb +2 -2
- data/lib/chewy/query/nodes/match_all.rb +1 -1
- data/lib/chewy/query/nodes/missing.rb +1 -1
- data/lib/chewy/query/nodes/not.rb +2 -2
- data/lib/chewy/query/nodes/or.rb +2 -2
- data/lib/chewy/query/nodes/prefix.rb +1 -1
- data/lib/chewy/query/nodes/query.rb +2 -2
- data/lib/chewy/query/nodes/range.rb +4 -4
- data/lib/chewy/query/nodes/regexp.rb +4 -4
- data/lib/chewy/query/nodes/script.rb +3 -3
- data/lib/chewy/query/pagination.rb +10 -1
- data/lib/chewy/railtie.rb +4 -3
- data/lib/chewy/rake_helper.rb +265 -48
- data/lib/chewy/rspec/update_index.rb +33 -27
- data/lib/chewy/search.rb +79 -26
- data/lib/chewy/search/loader.rb +83 -0
- data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
- data/lib/chewy/search/pagination/will_paginate.rb +43 -0
- data/lib/chewy/search/parameters.rb +168 -0
- data/lib/chewy/search/parameters/aggs.rb +16 -0
- data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
- data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
- data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
- data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
- data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
- data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
- data/lib/chewy/search/parameters/explain.rb +16 -0
- data/lib/chewy/search/parameters/filter.rb +47 -0
- data/lib/chewy/search/parameters/highlight.rb +16 -0
- data/lib/chewy/search/parameters/indices.rb +123 -0
- data/lib/chewy/search/parameters/indices_boost.rb +52 -0
- data/lib/chewy/search/parameters/limit.rb +17 -0
- data/lib/chewy/search/parameters/load.rb +32 -0
- data/lib/chewy/search/parameters/min_score.rb +16 -0
- data/lib/chewy/search/parameters/none.rb +27 -0
- data/lib/chewy/search/parameters/offset.rb +17 -0
- data/lib/chewy/search/parameters/order.rb +64 -0
- data/lib/chewy/search/parameters/post_filter.rb +19 -0
- data/lib/chewy/search/parameters/preference.rb +16 -0
- data/lib/chewy/search/parameters/profile.rb +16 -0
- data/lib/chewy/search/parameters/query.rb +19 -0
- data/lib/chewy/search/parameters/request_cache.rb +27 -0
- data/lib/chewy/search/parameters/rescore.rb +29 -0
- data/lib/chewy/search/parameters/script_fields.rb +16 -0
- data/lib/chewy/search/parameters/search_after.rb +20 -0
- data/lib/chewy/search/parameters/search_type.rb +16 -0
- data/lib/chewy/search/parameters/source.rb +73 -0
- data/lib/chewy/search/parameters/storage.rb +95 -0
- data/lib/chewy/search/parameters/stored_fields.rb +63 -0
- data/lib/chewy/search/parameters/suggest.rb +16 -0
- data/lib/chewy/search/parameters/terminate_after.rb +16 -0
- data/lib/chewy/search/parameters/timeout.rb +16 -0
- data/lib/chewy/search/parameters/track_scores.rb +16 -0
- data/lib/chewy/search/parameters/types.rb +20 -0
- data/lib/chewy/search/parameters/version.rb +16 -0
- data/lib/chewy/search/query_proxy.rb +257 -0
- data/lib/chewy/search/request.rb +1046 -0
- data/lib/chewy/search/response.rb +119 -0
- data/lib/chewy/search/scoping.rb +50 -0
- data/lib/chewy/search/scrolling.rb +134 -0
- data/lib/chewy/stash.rb +79 -0
- data/lib/chewy/strategy.rb +10 -3
- data/lib/chewy/strategy/active_job.rb +2 -1
- data/lib/chewy/strategy/atomic.rb +2 -4
- data/lib/chewy/strategy/bypass.rb +1 -1
- data/lib/chewy/strategy/resque.rb +1 -0
- data/lib/chewy/strategy/shoryuken.rb +40 -0
- data/lib/chewy/strategy/sidekiq.rb +13 -3
- data/lib/chewy/type.rb +29 -7
- data/lib/chewy/type/actions.rb +26 -2
- data/lib/chewy/type/adapter/active_record.rb +44 -29
- data/lib/chewy/type/adapter/base.rb +27 -7
- data/lib/chewy/type/adapter/mongoid.rb +19 -10
- data/lib/chewy/type/adapter/object.rb +187 -26
- data/lib/chewy/type/adapter/orm.rb +59 -32
- data/lib/chewy/type/adapter/sequel.rb +33 -19
- data/lib/chewy/type/crutch.rb +1 -1
- data/lib/chewy/type/import.rb +146 -191
- data/lib/chewy/type/import/bulk_builder.rb +122 -0
- data/lib/chewy/type/import/bulk_request.rb +78 -0
- data/lib/chewy/type/import/journal_builder.rb +45 -0
- data/lib/chewy/type/import/routine.rb +138 -0
- data/lib/chewy/type/mapping.rb +51 -35
- data/lib/chewy/type/observe.rb +17 -13
- data/lib/chewy/type/syncer.rb +222 -0
- data/lib/chewy/type/witchcraft.rb +32 -16
- data/lib/chewy/type/wrapper.rb +30 -4
- data/lib/chewy/version.rb +1 -1
- data/lib/sequel/plugins/chewy_observe.rb +4 -19
- data/lib/tasks/chewy.rake +84 -26
- data/spec/chewy/config_spec.rb +98 -1
- data/spec/chewy/fields/base_spec.rb +170 -135
- data/spec/chewy/fields/root_spec.rb +124 -20
- data/spec/chewy/fields/time_fields_spec.rb +2 -3
- data/spec/chewy/index/actions_spec.rb +214 -52
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +67 -38
- data/spec/chewy/index/specification_spec.rb +169 -0
- data/spec/chewy/index_spec.rb +108 -64
- data/spec/chewy/journal_spec.rb +150 -55
- data/spec/chewy/minitest/helpers_spec.rb +4 -4
- data/spec/chewy/minitest/search_index_receiver_spec.rb +1 -1
- data/spec/chewy/query/criteria_spec.rb +179 -179
- data/spec/chewy/query/filters_spec.rb +16 -16
- data/spec/chewy/query/loading_spec.rb +22 -20
- data/spec/chewy/query/nodes/and_spec.rb +2 -2
- data/spec/chewy/query/nodes/bool_spec.rb +4 -4
- data/spec/chewy/query/nodes/equal_spec.rb +19 -19
- data/spec/chewy/query/nodes/exists_spec.rb +6 -6
- data/spec/chewy/query/nodes/has_child_spec.rb +19 -19
- data/spec/chewy/query/nodes/has_parent_spec.rb +19 -19
- data/spec/chewy/query/nodes/missing_spec.rb +5 -5
- data/spec/chewy/query/nodes/not_spec.rb +4 -2
- data/spec/chewy/query/nodes/or_spec.rb +2 -2
- data/spec/chewy/query/nodes/prefix_spec.rb +5 -5
- data/spec/chewy/query/nodes/query_spec.rb +2 -2
- data/spec/chewy/query/nodes/range_spec.rb +18 -18
- data/spec/chewy/query/nodes/raw_spec.rb +1 -1
- data/spec/chewy/query/nodes/regexp_spec.rb +14 -14
- data/spec/chewy/query/nodes/script_spec.rb +4 -4
- data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
- data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
- data/spec/chewy/query/pagination_spec.rb +25 -21
- data/spec/chewy/query_spec.rb +503 -561
- data/spec/chewy/rake_helper_spec.rb +381 -0
- data/spec/chewy/repository_spec.rb +4 -4
- data/spec/chewy/rspec/update_index_spec.rb +89 -56
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/loader_spec.rb +117 -0
- data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
- data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
- data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
- data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
- data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
- data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
- data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/explain_spec.rb +5 -0
- data/spec/chewy/search/parameters/filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
- data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
- data/spec/chewy/search/parameters/indices_spec.rb +191 -0
- data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/limit_spec.rb +5 -0
- data/spec/chewy/search/parameters/load_spec.rb +60 -0
- data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
- data/spec/chewy/search/parameters/none_spec.rb +5 -0
- data/spec/chewy/search/parameters/offset_spec.rb +5 -0
- data/spec/chewy/search/parameters/order_spec.rb +65 -0
- data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/preference_spec.rb +5 -0
- data/spec/chewy/search/parameters/profile_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
- data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
- data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
- data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
- data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
- data/spec/chewy/search/parameters/source_spec.rb +156 -0
- data/spec/chewy/search/parameters/storage_spec.rb +60 -0
- data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
- data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
- data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
- data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
- data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
- data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
- data/spec/chewy/search/parameters/types_spec.rb +5 -0
- data/spec/chewy/search/parameters/version_spec.rb +5 -0
- data/spec/chewy/search/parameters_spec.rb +147 -0
- data/spec/chewy/search/query_proxy_spec.rb +68 -0
- data/spec/chewy/search/request_spec.rb +685 -0
- data/spec/chewy/search/response_spec.rb +198 -0
- data/spec/chewy/search/scrolling_spec.rb +169 -0
- data/spec/chewy/search_spec.rb +33 -16
- data/spec/chewy/stash_spec.rb +95 -0
- data/spec/chewy/strategy/active_job_spec.rb +21 -2
- data/spec/chewy/strategy/resque_spec.rb +6 -0
- data/spec/chewy/strategy/shoryuken_spec.rb +70 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +13 -1
- data/spec/chewy/strategy_spec.rb +6 -6
- data/spec/chewy/type/actions_spec.rb +29 -10
- data/spec/chewy/type/adapter/active_record_spec.rb +203 -91
- data/spec/chewy/type/adapter/mongoid_spec.rb +112 -54
- data/spec/chewy/type/adapter/object_spec.rb +101 -28
- data/spec/chewy/type/adapter/sequel_spec.rb +149 -82
- data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
- data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
- data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
- data/spec/chewy/type/import/routine_spec.rb +110 -0
- data/spec/chewy/type/import_spec.rb +356 -271
- data/spec/chewy/type/mapping_spec.rb +96 -29
- data/spec/chewy/type/observe_spec.rb +9 -5
- data/spec/chewy/type/syncer_spec.rb +123 -0
- data/spec/chewy/type/witchcraft_spec.rb +61 -29
- data/spec/chewy/type/wrapper_spec.rb +63 -23
- data/spec/chewy/type_spec.rb +28 -7
- data/spec/chewy_spec.rb +75 -7
- data/spec/spec_helper.rb +17 -3
- data/spec/support/active_record.rb +5 -1
- data/spec/support/class_helpers.rb +0 -14
- data/spec/support/mongoid.rb +15 -3
- data/spec/support/sequel.rb +6 -1
- metadata +219 -58
- data/.travis.yml +0 -36
- data/gemfiles/rails.3.2.activerecord.gemfile +0 -16
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -15
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.gemfile +0 -17
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.4.0.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.4.0.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.5.1.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
- data/gemfiles/rails.5.0.activerecord.gemfile +0 -17
- data/gemfiles/rails.5.0.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.activerecord.will_paginate.gemfile +0 -16
- data/gemfiles/sequel.4.38.gemfile +0 -14
- data/lib/chewy/journal/apply.rb +0 -31
- data/lib/chewy/journal/clean.rb +0 -24
- data/lib/chewy/journal/entry.rb +0 -83
- data/lib/chewy/journal/query.rb +0 -87
- data/lib/chewy/query/pagination/will_paginate.rb +0 -27
- data/lib/chewy/query/scoping.rb +0 -20
- data/spec/chewy/journal/apply_spec.rb +0 -120
- data/spec/chewy/journal/entry_spec.rb +0 -237
- data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -59
@@ -83,9 +83,7 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
|
|
83
83
|
# .to update_index(UsersIndex.user).and_reindex(user1).only }
|
84
84
|
#
|
85
85
|
chain(:only) do |*_args|
|
86
|
-
if @reindex.blank? && @delete.blank?
|
87
|
-
raise 'Use `only` in conjunction with `and_reindex` or `and_delete`'
|
88
|
-
end
|
86
|
+
raise 'Use `only` in conjunction with `and_reindex` or `and_delete`' if @reindex.blank? && @delete.blank?
|
89
87
|
|
90
88
|
@only = true
|
91
89
|
end
|
@@ -99,22 +97,19 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
|
|
99
97
|
@delete ||= {}
|
100
98
|
@missed_reindex = []
|
101
99
|
@missed_delete = []
|
102
|
-
@updated = []
|
103
100
|
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
RUBY
|
101
|
+
type = Chewy.derive_type(type_name)
|
102
|
+
if defined?(Mocha) && RSpec.configuration.mock_framework.to_s == 'RSpec::Core::MockingAdapters::Mocha'
|
103
|
+
Chewy::Type::Import::BulkRequest.stubs(:new).with(type, any_parameters).returns(mock_bulk_request)
|
104
|
+
else
|
105
|
+
mocked_already = ::RSpec::Mocks.space.proxy_for(Chewy::Type::Import::BulkRequest).method_double_if_exists_for_message(:new)
|
106
|
+
allow(Chewy::Type::Import::BulkRequest).to receive(:new).and_call_original unless mocked_already
|
107
|
+
allow(Chewy::Type::Import::BulkRequest).to receive(:new).with(type, any_args).and_return(mock_bulk_request)
|
108
|
+
end
|
113
109
|
|
114
|
-
ActiveSupport::Deprecation.warn('`atomic: false` option is removed and not effective anymore, use `strategy: :atomic` option instead') if options.key?(:atomic)
|
115
110
|
Chewy.strategy(options[:strategy] || :atomic) { block.call }
|
116
111
|
|
117
|
-
|
112
|
+
mock_bulk_request.updates.each do |updated_document|
|
118
113
|
if (body = updated_document[:index])
|
119
114
|
if (document = @reindex[body[:_id].to_s])
|
120
115
|
document[:real_count] += 1
|
@@ -131,18 +126,18 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
|
|
131
126
|
end
|
132
127
|
end
|
133
128
|
|
134
|
-
@reindex.
|
129
|
+
@reindex.each_value do |document|
|
135
130
|
document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
|
136
131
|
(document[:expected_count] && document[:expected_count] == document[:real_count])
|
137
132
|
document[:match_attributes] = document[:expected_attributes].blank? ||
|
138
133
|
compare_attributes(document[:expected_attributes], document[:real_attributes])
|
139
134
|
end
|
140
|
-
@delete.
|
135
|
+
@delete.each_value do |document|
|
141
136
|
document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
|
142
137
|
(document[:expected_count] && document[:expected_count] == document[:real_count])
|
143
138
|
end
|
144
139
|
|
145
|
-
|
140
|
+
mock_bulk_request.updates.present? && @missed_reindex.none? && @missed_delete.none? &&
|
146
141
|
@reindex.all? { |_, document| document[:match_count] && document[:match_attributes] } &&
|
147
142
|
@delete.all? { |_, document| document[:match_count] }
|
148
143
|
end
|
@@ -150,7 +145,7 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
|
|
150
145
|
failure_message do # rubocop:disable BlockLength
|
151
146
|
output = ''
|
152
147
|
|
153
|
-
if
|
148
|
+
if mock_bulk_request.updates.none?
|
154
149
|
output << "Expected index `#{type_name}` to be updated, but it was not\n"
|
155
150
|
elsif @missed_reindex.present? || @missed_delete.present?
|
156
151
|
message = "Expected index `#{type_name}` "
|
@@ -197,19 +192,15 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
|
|
197
192
|
end
|
198
193
|
|
199
194
|
failure_message_when_negated do
|
200
|
-
if
|
201
|
-
"Expected index `#{type_name}` not to be updated, but it was with #{
|
195
|
+
if mock_bulk_request.updates.present?
|
196
|
+
"Expected index `#{type_name}` not to be updated, but it was with #{mock_bulk_request.updates.map(&:values).flatten.group_by { |documents| documents[:_id] }.map do |id, documents|
|
202
197
|
"\n document id `#{id}` (#{documents.count} times)"
|
203
198
|
end.join}\n"
|
204
199
|
end
|
205
200
|
end
|
206
201
|
|
207
|
-
def
|
208
|
-
|
209
|
-
'type.stubs(:bulk).with'
|
210
|
-
else
|
211
|
-
'allow(type).to receive(:bulk)'
|
212
|
-
end
|
202
|
+
def mock_bulk_request
|
203
|
+
@mock_bulk_request ||= MockBulkRequest.new
|
213
204
|
end
|
214
205
|
|
215
206
|
def extract_documents(*args)
|
@@ -251,4 +242,19 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
|
|
251
242
|
end
|
252
243
|
difference.none?
|
253
244
|
end
|
245
|
+
|
246
|
+
# Collects request bodies coming through the perform method for
|
247
|
+
# the further analysis.
|
248
|
+
class MockBulkRequest
|
249
|
+
attr_reader :updates
|
250
|
+
|
251
|
+
def initialize
|
252
|
+
@updates = []
|
253
|
+
end
|
254
|
+
|
255
|
+
def perform(body)
|
256
|
+
@updates.concat(body.map(&:deep_symbolize_keys))
|
257
|
+
[]
|
258
|
+
end
|
259
|
+
end
|
254
260
|
end
|
data/lib/chewy/search.rb
CHANGED
@@ -1,47 +1,100 @@
|
|
1
|
+
require 'chewy/search/scoping'
|
1
2
|
require 'chewy/query'
|
3
|
+
require 'chewy/search/scrolling'
|
4
|
+
require 'chewy/search/query_proxy'
|
5
|
+
require 'chewy/search/parameters'
|
6
|
+
require 'chewy/search/response'
|
7
|
+
require 'chewy/search/loader'
|
8
|
+
require 'chewy/search/request'
|
9
|
+
require 'chewy/search/pagination/kaminari'
|
10
|
+
require 'chewy/search/pagination/will_paginate'
|
2
11
|
|
3
12
|
module Chewy
|
13
|
+
# This module being included to any provides an interface to the
|
14
|
+
# request DSL. By default it is included to {Chewy::Index} and
|
15
|
+
# {Chewy::Type}.
|
16
|
+
#
|
17
|
+
# The class used as a request DSL provider is
|
18
|
+
# inherited from {Chewy::Search::Request} by default, but if you
|
19
|
+
# need ES < 2.0 DSL support - you can switch it to {Chewy::Query}
|
20
|
+
# using {Chewy::Config#search_class}
|
21
|
+
#
|
22
|
+
# Also, the search class is refined with one of the pagination-
|
23
|
+
# providing modules: {Chewy::Search::Pagination::Kaminari} or
|
24
|
+
# {Chewy::Search::Pagination::WillPaginate}.
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# PlacesIndex.query(match: {name: 'Moscow'})
|
28
|
+
# PlacesIndex::City.query(match: {name: 'Moscow'})
|
29
|
+
# @see Chewy::Index
|
30
|
+
# @see Chewy::Type
|
31
|
+
# @see Chewy::Search::Request
|
32
|
+
# @see Chewy::Search::ClassMethods
|
33
|
+
# @see Chewy::Search::Pagination::Kaminari
|
34
|
+
# @see Chewy::Search::Pagination::WillPaginate
|
4
35
|
module Search
|
5
36
|
extend ActiveSupport::Concern
|
6
37
|
|
7
|
-
included do
|
8
|
-
singleton_class.delegate :explain, :query_mode, :filter_mode, :post_filter_mode,
|
9
|
-
:timeout, :limit, :offset, :highlight, :min_score, :rescore, :facets, :script_score,
|
10
|
-
:boost_factor, :weight, :random_score, :field_value_factor, :decay, :aggregations,
|
11
|
-
:suggest, :none, :strategy, :query, :filter, :post_filter, :boost_mode,
|
12
|
-
:score_mode, :order, :reorder, :only, :types, :delete_all, :find, :total,
|
13
|
-
:total_count, :total_entries, :unlimited, :script_fields, :track_scores, :preference,
|
14
|
-
to: :all
|
15
|
-
end
|
16
|
-
|
17
38
|
module ClassMethods
|
39
|
+
# This is the entry point for the request composition, however,
|
40
|
+
# most of the {Chewy::Search::Request} methods are delegated
|
41
|
+
# directly as well.
|
42
|
+
#
|
43
|
+
# This method also provides an ability to use names scopes.
|
44
|
+
#
|
45
|
+
# @example
|
46
|
+
# PlacesIndex.all.limit(10)
|
47
|
+
# # is basically the same as:
|
48
|
+
# PlacesIndex.limit(10)
|
49
|
+
# @see Chewy::Search::Request
|
50
|
+
# @see Chewy::Search::Scoping
|
51
|
+
# @return [Chewy::Search::Request] request instance
|
18
52
|
def all
|
19
|
-
|
53
|
+
search_class.scopes.last || search_class.new(self)
|
20
54
|
end
|
21
55
|
|
56
|
+
# A simple way to execute search string query.
|
57
|
+
#
|
58
|
+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html
|
59
|
+
# @return [Hash] the request result
|
22
60
|
def search_string(query, options = {})
|
23
|
-
options = options.merge(
|
24
|
-
index: all._indexes.map(&:index_name),
|
25
|
-
type: all._types.map(&:type_name),
|
26
|
-
q: query
|
27
|
-
)
|
61
|
+
options = options.merge(all.render.slice(:index, :type).merge(q: query))
|
28
62
|
Chewy.client.search(options)
|
29
63
|
end
|
30
64
|
|
65
|
+
# Delegates methods from the request class to the index or type class
|
66
|
+
#
|
67
|
+
# @example
|
68
|
+
# PlacesIndex.query(match: {name: 'Moscow'})
|
69
|
+
# PlacesIndex::City.query(match: {name: 'Moscow'})
|
70
|
+
def method_missing(name, *args, &block)
|
71
|
+
if search_class::DELEGATED_METHODS.include?(name)
|
72
|
+
all.send(name, *args, &block)
|
73
|
+
else
|
74
|
+
super
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def respond_to_missing?(name, _)
|
79
|
+
search_class::DELEGATED_METHODS.include?(name) || super
|
80
|
+
end
|
81
|
+
|
31
82
|
private
|
32
83
|
|
33
|
-
def
|
34
|
-
@
|
35
|
-
|
36
|
-
if self < Chewy::Type
|
37
|
-
index_scopes = index.scopes - scopes
|
84
|
+
def search_class
|
85
|
+
@search_class ||= build_search_class(Chewy.search_class)
|
86
|
+
end
|
38
87
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
88
|
+
def build_search_class(base)
|
89
|
+
search_class = Class.new(base)
|
90
|
+
|
91
|
+
if self < Chewy::Type
|
92
|
+
index_scopes = index.scopes - scopes
|
93
|
+
delegate_scoped index, search_class, index_scopes
|
44
94
|
end
|
95
|
+
|
96
|
+
delegate_scoped self, search_class, scopes
|
97
|
+
const_set('Query', search_class)
|
45
98
|
end
|
46
99
|
|
47
100
|
def delegate_scoped(source, destination, methods)
|
@@ -0,0 +1,83 @@
|
|
1
|
+
module Chewy
|
2
|
+
module Search
|
3
|
+
# This class is used for two different purposes: load ORM/ODM
|
4
|
+
# source objects.
|
5
|
+
#
|
6
|
+
# @see Chewy::Type::Import
|
7
|
+
# @see Chewy::Search::Request#load
|
8
|
+
# @see Chewy::Search::Response#objects
|
9
|
+
# @see Chewy::Search::Scrolling#scroll_objects
|
10
|
+
class Loader
|
11
|
+
# @param indexes [Array<Chewy::Index>] list of indexes to lookup types
|
12
|
+
# @param only [Array<String, Symbol>] list of selected type names to load
|
13
|
+
# @param except [Array<String, Symbol>] list of type names which will not be loaded
|
14
|
+
# @param options [Hash] adapter-specific load options
|
15
|
+
# @see Chewy::Type::Adapter::Base#load
|
16
|
+
def initialize(indexes: [], only: [], except: [], **options)
|
17
|
+
@indexes = indexes
|
18
|
+
@only = Array.wrap(only).map(&:to_s)
|
19
|
+
@except = Array.wrap(except).map(&:to_s)
|
20
|
+
@options = options
|
21
|
+
end
|
22
|
+
|
23
|
+
# Returns a {Chewy::Type} object for index name and type name passed. Caches
|
24
|
+
# the result for each pair to make lookup faster.
|
25
|
+
#
|
26
|
+
# @param index [String] index name
|
27
|
+
# @param type [String] type name
|
28
|
+
# @return [Chewy::Type]
|
29
|
+
# @raise [Chewy::UnderivableType] when index or hash were not found
|
30
|
+
def derive_type(index, type)
|
31
|
+
(@derive_type ||= {})[[index, type]] ||= begin
|
32
|
+
index_class = derive_index(index)
|
33
|
+
raise Chewy::UnderivableType, "Can not find index named `#{index}`" unless index_class
|
34
|
+
index_class.type_hash[type] or raise Chewy::UnderivableType, "Index `#{index}` doesn`t have type named `#{type}`"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# For each passed hit this method loads an ORM/ORD source object
|
39
|
+
# using `hit['_id']`. The returned array is exactly in the same order
|
40
|
+
# as hits were. If source object was not found for some hit, `nil`
|
41
|
+
# will be returned at the corresponding position in array.
|
42
|
+
#
|
43
|
+
# Records/documents are loaded in an efficient manner, performing
|
44
|
+
# a single query for each type present.
|
45
|
+
#
|
46
|
+
# @param hits [Array<Hash>] ES hits array
|
47
|
+
# @return [Array<Object, nil>] the array of corresponding ORM/ODM objects
|
48
|
+
def load(hits)
|
49
|
+
hit_groups = hits.group_by { |hit| [hit['_index'], hit['_type']] }
|
50
|
+
loaded_objects = hit_groups.each_with_object({}) do |((index_name, type_name), hit_group), result|
|
51
|
+
next if skip_type?(type_name)
|
52
|
+
|
53
|
+
type = derive_type(index_name, type_name)
|
54
|
+
ids = hit_group.map { |hit| hit['_id'] }
|
55
|
+
loaded = type.adapter.load(ids, **@options.merge(_type: type))
|
56
|
+
loaded ||= hit_group.map { |hit| type.build(hit) }
|
57
|
+
|
58
|
+
result.merge!(hit_group.zip(loaded).to_h)
|
59
|
+
end
|
60
|
+
|
61
|
+
hits.map { |hit| loaded_objects[hit] }
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def derive_index(index_name)
|
67
|
+
(@derive_index ||= {})[index_name] ||= indexes_hash[index_name] ||
|
68
|
+
indexes_hash[indexes_hash.keys.sort_by(&:length)
|
69
|
+
.reverse.detect do |name|
|
70
|
+
index_name.match(/#{name}(_.+|\z)/)
|
71
|
+
end]
|
72
|
+
end
|
73
|
+
|
74
|
+
def indexes_hash
|
75
|
+
@indexes_hash ||= @indexes.index_by(&:index_name)
|
76
|
+
end
|
77
|
+
|
78
|
+
def skip_type?(type_name)
|
79
|
+
@except.include?(type_name) || @only.present? && !@only.include?(type_name)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -1,6 +1,12 @@
|
|
1
1
|
module Chewy
|
2
|
-
|
2
|
+
module Search
|
3
3
|
module Pagination
|
4
|
+
# This module provides `Kaminari` support for {Chewy::Search::Request}
|
5
|
+
# It is included automatically if `Kaminari` is available.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# PlacesIndex.all.page(3).per(10).order(:name)
|
9
|
+
# # => <PlacesIndex::Query {..., :body=>{:size=>10, :from=>20, :sort=>["name"]}}>
|
4
10
|
module Kaminari
|
5
11
|
extend ActiveSupport::Concern
|
6
12
|
|
@@ -17,11 +23,11 @@ module Chewy
|
|
17
23
|
end
|
18
24
|
|
19
25
|
def limit_value
|
20
|
-
(
|
26
|
+
(raw_limit_value || default_per_page).to_i
|
21
27
|
end
|
22
28
|
|
23
29
|
def offset_value
|
24
|
-
|
30
|
+
raw_offset_value.to_i
|
25
31
|
end
|
26
32
|
|
27
33
|
private
|
@@ -29,9 +35,11 @@ module Chewy
|
|
29
35
|
def _kaminari_config
|
30
36
|
::Kaminari.config
|
31
37
|
end
|
38
|
+
|
39
|
+
def paginated_collection(collection)
|
40
|
+
::Kaminari.paginate_array(collection, limit: limit_value, offset: offset_value, total_count: total_count)
|
41
|
+
end
|
32
42
|
end
|
33
43
|
end
|
34
44
|
end
|
35
45
|
end
|
36
|
-
|
37
|
-
Chewy::Query.send :include, Chewy::Query::Pagination::Kaminari
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module Chewy
|
2
|
+
module Search
|
3
|
+
module Pagination
|
4
|
+
# This module provides `WillPaginate` support for {Chewy::Search::Request}
|
5
|
+
# It is included automatically if `WillPaginate` is available.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# PlacesIndex.all.paginate(page: 3, per_page: 10).order(:name)
|
9
|
+
# # => <PlacesIndex::Query {..., :body=>{:size=>10, :from=>20, :sort=>["name"]}}>
|
10
|
+
module WillPaginate
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
included do
|
14
|
+
include ::WillPaginate::CollectionMethods
|
15
|
+
attr_reader :current_page, :per_page
|
16
|
+
end
|
17
|
+
|
18
|
+
def paginate(options = {})
|
19
|
+
@current_page = ::WillPaginate::PageNumber(options[:page] || @current_page || 1)
|
20
|
+
@page_multiplier = @current_page - 1
|
21
|
+
@per_page = (options[:per_page] || @per_page || ::WillPaginate.per_page).to_i
|
22
|
+
|
23
|
+
# call Chewy::Query methods to limit results
|
24
|
+
limit(@per_page).offset(@page_multiplier * @per_page)
|
25
|
+
end
|
26
|
+
|
27
|
+
def page(page)
|
28
|
+
paginate(page: page)
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def paginated_collection(collection)
|
34
|
+
page = current_page || 1
|
35
|
+
per = per_page || ::WillPaginate.per_page
|
36
|
+
::WillPaginate::Collection.create(page, per, total_entries) do |pager|
|
37
|
+
pager.replace collection
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'parameters', 'concerns', '*.rb')) { |f| require f }
|
2
|
+
Dir.glob(File.join(File.dirname(__FILE__), 'parameters', '*.rb')) { |f| require f }
|
3
|
+
|
4
|
+
module Chewy
|
5
|
+
module Search
|
6
|
+
# This class is basically a compound storage of the request
|
7
|
+
# parameter storages. It encapsulates some storage-collection-handling
|
8
|
+
# logic.
|
9
|
+
#
|
10
|
+
# @see Chewy::Search::Request#parameters
|
11
|
+
# @see Chewy::Search::Parameters::Storage
|
12
|
+
class Parameters
|
13
|
+
QUERY_STRING_STORAGES = %i[indices search_type request_cache allow_partial_search_results].freeze
|
14
|
+
|
15
|
+
# Default storage classes warehouse. It is probably possible to
|
16
|
+
# add your own classes here if necessary, but I'm not sure it will work.
|
17
|
+
#
|
18
|
+
# @return [{Symbol => Chewy::Search::Parameters::Storage}]
|
19
|
+
def self.storages
|
20
|
+
@storages ||= Hash.new do |hash, name|
|
21
|
+
hash[name] = "Chewy::Search::Parameters::#{name.to_s.camelize}".constantize
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# @return [{Symbol => Chewy::Search::Parameters::Storage}]
|
26
|
+
attr_accessor :storages
|
27
|
+
delegate :[], :[]=, to: :storages
|
28
|
+
|
29
|
+
# Accepts an initial hash as basic values or parameter storages.
|
30
|
+
#
|
31
|
+
# @example
|
32
|
+
# Chewy::Search::Parameters.new(limit: 10, offset 10)
|
33
|
+
# Chewy::Search::Parameters.new(
|
34
|
+
# limit: Chewy::Search::Parameters::Limit.new(10),
|
35
|
+
# limit: Chewy::Search::Parameters::Offset.new(10)
|
36
|
+
# )
|
37
|
+
# @param initial [{Symbol => Object, Chewy::Search::Parameters::Storage}]
|
38
|
+
def initialize(initial = {}, **kinitial)
|
39
|
+
@storages = Hash.new do |hash, name|
|
40
|
+
hash[name] = self.class.storages[name].new
|
41
|
+
end
|
42
|
+
initial = initial.deep_dup.merge(kinitial)
|
43
|
+
initial.each_with_object(@storages) do |(name, value), result|
|
44
|
+
storage_class = self.class.storages[name]
|
45
|
+
storage = value.is_a?(storage_class) ? value : storage_class.new(value)
|
46
|
+
result[name] = storage
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
# Compares storages by their values.
|
51
|
+
#
|
52
|
+
# @param other [Object] any object
|
53
|
+
# @return [true, false]
|
54
|
+
def ==(other)
|
55
|
+
super || other.is_a?(self.class) && compare_storages(other)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Clones the specified storage, performs the operation
|
59
|
+
# defined by block on the clone.
|
60
|
+
#
|
61
|
+
# @param name [Symbol] parameter name
|
62
|
+
# @yield the block is executed in the cloned storage instance binding
|
63
|
+
# @return [Chewy::Search::Parameters::Storage]
|
64
|
+
def modify!(name, &block)
|
65
|
+
@storages[name] = @storages[name].clone.tap do |s|
|
66
|
+
s.instance_exec(&block)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# Removes specified storages from the storages hash.
|
71
|
+
#
|
72
|
+
# @param names [Array<String, Symbol>]
|
73
|
+
# @return [{Symbol => Chewy::Search::Parameters::Storage}] removed storages hash
|
74
|
+
def only!(names)
|
75
|
+
@storages.slice!(*assert_storages(names))
|
76
|
+
end
|
77
|
+
|
78
|
+
# Keeps only specified storages removing everything else.
|
79
|
+
#
|
80
|
+
# @param names [Array<String, Symbol>]
|
81
|
+
# @return [{Symbol => Chewy::Search::Parameters::Storage}] kept storages hash
|
82
|
+
def except!(names)
|
83
|
+
@storages.except!(*assert_storages(names))
|
84
|
+
end
|
85
|
+
|
86
|
+
# Takes all the storages and merges them one by one using
|
87
|
+
# {Chewy::Search::Parameters::Storage#merge!} method. Merging
|
88
|
+
# is implemented in different ways for different storages: for
|
89
|
+
# limit, offset and other single-value classes it is a simple
|
90
|
+
# value replacement, for boolean storages (explain, none) it uses
|
91
|
+
# a disjunction result, for compound values - merging and
|
92
|
+
# concatenation, for query, filter, post_filter - it is the
|
93
|
+
# "and" operation.
|
94
|
+
#
|
95
|
+
# @see Chewy::Search::Parameters::Storage#merge!
|
96
|
+
# @return [{Symbol => Chewy::Search::Parameters::Storage}] storages from other parameters
|
97
|
+
def merge!(other)
|
98
|
+
other.storages.each do |name, storage|
|
99
|
+
modify!(name) { merge!(storage) }
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# Renders and merges all the parameter storages into a single hash.
|
104
|
+
#
|
105
|
+
# @return [Hash] request body
|
106
|
+
def render
|
107
|
+
render_query_string_params.merge(render_body)
|
108
|
+
end
|
109
|
+
|
110
|
+
protected
|
111
|
+
|
112
|
+
def initialize_clone(origin)
|
113
|
+
@storages = origin.storages.clone
|
114
|
+
end
|
115
|
+
|
116
|
+
def compare_storages(other)
|
117
|
+
keys = (@storages.keys | other.storages.keys)
|
118
|
+
@storages.values_at(*keys) == other.storages.values_at(*keys)
|
119
|
+
end
|
120
|
+
|
121
|
+
def assert_storages(names)
|
122
|
+
raise ArgumentError, 'No storage names were specified' if names.empty?
|
123
|
+
names = names.map(&:to_sym)
|
124
|
+
self.class.storages.values_at(*names)
|
125
|
+
names
|
126
|
+
end
|
127
|
+
|
128
|
+
def render_query_string_params
|
129
|
+
query_string_storages = @storages.select do |storage_name, _|
|
130
|
+
QUERY_STRING_STORAGES.include?(storage_name)
|
131
|
+
end
|
132
|
+
|
133
|
+
query_string_storages.values.inject({}) do |result, storage|
|
134
|
+
result.merge!(storage.render || {})
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def render_body
|
139
|
+
exceptions = %i[filter query none] + QUERY_STRING_STORAGES
|
140
|
+
body = @storages.except(*exceptions).values.inject({}) do |result, storage|
|
141
|
+
result.merge!(storage.render || {})
|
142
|
+
end
|
143
|
+
body.merge!(render_query || {})
|
144
|
+
{body: body}
|
145
|
+
end
|
146
|
+
|
147
|
+
def render_query
|
148
|
+
none = @storages[:none].render
|
149
|
+
|
150
|
+
return none if none
|
151
|
+
|
152
|
+
filter = @storages[:filter].render
|
153
|
+
query = @storages[:query].render
|
154
|
+
|
155
|
+
return query unless filter
|
156
|
+
|
157
|
+
if query && query[:query][:bool]
|
158
|
+
query[:query][:bool].merge!(filter)
|
159
|
+
query
|
160
|
+
elsif query
|
161
|
+
{query: {bool: {must: query[:query]}.merge!(filter)}}
|
162
|
+
else
|
163
|
+
{query: {bool: filter}}
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|