chewy 5.0.0 → 7.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.circleci/config.yml +240 -0
- data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
- data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
- data/.github/workflows/ruby.yml +94 -0
- data/Appraisals +1 -17
- data/CHANGELOG.md +308 -356
- data/CODE_OF_CONDUCT.md +14 -0
- data/CONTRIBUTING.md +63 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +1 -1
- data/README.md +71 -55
- data/chewy.gemspec +5 -5
- data/gemfiles/rails.5.2.activerecord.gemfile +4 -3
- data/gemfiles/{rails.5.0.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +4 -3
- data/gemfiles/{rails.5.0.mongoid.6.1.gemfile → rails.6.0.activerecord.gemfile} +4 -3
- data/gemfiles/{rails.5.1.activerecord.gemfile → rails.6.1.activerecord.gemfile} +6 -3
- data/lib/chewy.rb +1 -1
- data/lib/chewy/backports/duplicable.rb +1 -1
- data/lib/chewy/config.rb +2 -20
- data/lib/chewy/fields/base.rb +1 -7
- data/lib/chewy/fields/root.rb +2 -2
- data/lib/chewy/index.rb +2 -0
- data/lib/chewy/index/actions.rb +15 -5
- data/lib/chewy/index/aliases.rb +16 -5
- data/lib/chewy/multi_search.rb +62 -0
- data/lib/chewy/railtie.rb +1 -1
- data/lib/chewy/search.rb +2 -9
- data/lib/chewy/search/loader.rb +1 -1
- data/lib/chewy/search/pagination/will_paginate.rb +1 -1
- data/lib/chewy/search/parameters.rb +24 -6
- data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
- data/lib/chewy/search/parameters/indices.rb +123 -0
- data/lib/chewy/search/parameters/none.rb +1 -3
- data/lib/chewy/search/request.rb +101 -74
- data/lib/chewy/search/scrolling.rb +7 -6
- data/lib/chewy/stash.rb +1 -1
- data/lib/chewy/strategy/active_job.rb +1 -1
- data/lib/chewy/strategy/sidekiq.rb +1 -1
- data/lib/chewy/type.rb +4 -1
- data/lib/chewy/type/adapter/active_record.rb +1 -1
- data/lib/chewy/type/adapter/mongoid.rb +1 -1
- data/lib/chewy/type/adapter/orm.rb +7 -4
- data/lib/chewy/type/adapter/sequel.rb +1 -1
- data/lib/chewy/type/import.rb +14 -4
- data/lib/chewy/type/import/bulk_request.rb +4 -2
- data/lib/chewy/type/import/journal_builder.rb +1 -1
- data/lib/chewy/type/import/routine.rb +1 -1
- data/lib/chewy/type/mapping.rb +4 -4
- data/lib/chewy/type/observe.rb +3 -3
- data/lib/chewy/type/syncer.rb +4 -5
- data/lib/chewy/type/witchcraft.rb +4 -2
- data/lib/chewy/type/wrapper.rb +12 -2
- data/lib/chewy/version.rb +1 -1
- data/migration_guide.md +56 -0
- data/spec/chewy/config_spec.rb +1 -22
- data/spec/chewy/fields/base_spec.rb +11 -9
- data/spec/chewy/index/actions_spec.rb +120 -33
- data/spec/chewy/index/aliases_spec.rb +3 -3
- data/spec/chewy/index_spec.rb +16 -39
- data/spec/chewy/journal_spec.rb +22 -18
- data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
- data/spec/chewy/multi_search_spec.rb +85 -0
- data/spec/chewy/rake_helper_spec.rb +102 -87
- data/spec/chewy/rspec/update_index_spec.rb +47 -46
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/parameters/indices_spec.rb +190 -0
- data/spec/chewy/search/parameters/none_spec.rb +1 -1
- data/spec/chewy/search/parameters_spec.rb +21 -4
- data/spec/chewy/search/request_spec.rb +103 -70
- data/spec/chewy/search/response_spec.rb +27 -17
- data/spec/chewy/search/scrolling_spec.rb +27 -17
- data/spec/chewy/search_spec.rb +45 -41
- data/spec/chewy/stash_spec.rb +14 -12
- data/spec/chewy/strategy/active_job_spec.rb +15 -2
- data/spec/chewy/strategy/shoryuken_spec.rb +6 -2
- data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
- data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
- data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
- data/spec/chewy/type/import/journal_builder_spec.rb +9 -7
- data/spec/chewy/type/import_spec.rb +9 -0
- data/spec/chewy/type/mapping_spec.rb +3 -1
- data/spec/chewy/type/observe_spec.rb +4 -4
- data/spec/chewy/type/witchcraft_spec.rb +15 -0
- data/spec/chewy/type/wrapper_spec.rb +3 -1
- data/spec/chewy_spec.rb +0 -7
- data/spec/spec_helper.rb +4 -8
- data/spec/support/active_record.rb +21 -0
- metadata +32 -97
- data/.travis.yml +0 -45
- data/LEGACY_DSL.md +0 -497
- data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
- data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
- data/gemfiles/rails.4.2.mongoid.5.2.gemfile +0 -16
- data/gemfiles/rails.5.1.mongoid.6.3.gemfile +0 -16
- data/lib/chewy/query.rb +0 -1137
- data/lib/chewy/query/compose.rb +0 -68
- data/lib/chewy/query/criteria.rb +0 -191
- data/lib/chewy/query/filters.rb +0 -244
- data/lib/chewy/query/loading.rb +0 -110
- data/lib/chewy/query/nodes/and.rb +0 -25
- data/lib/chewy/query/nodes/base.rb +0 -17
- data/lib/chewy/query/nodes/bool.rb +0 -34
- data/lib/chewy/query/nodes/equal.rb +0 -34
- data/lib/chewy/query/nodes/exists.rb +0 -20
- data/lib/chewy/query/nodes/expr.rb +0 -28
- data/lib/chewy/query/nodes/field.rb +0 -110
- data/lib/chewy/query/nodes/has_child.rb +0 -15
- data/lib/chewy/query/nodes/has_parent.rb +0 -15
- data/lib/chewy/query/nodes/has_relation.rb +0 -59
- data/lib/chewy/query/nodes/match_all.rb +0 -11
- data/lib/chewy/query/nodes/missing.rb +0 -20
- data/lib/chewy/query/nodes/not.rb +0 -25
- data/lib/chewy/query/nodes/or.rb +0 -25
- data/lib/chewy/query/nodes/prefix.rb +0 -19
- data/lib/chewy/query/nodes/query.rb +0 -20
- data/lib/chewy/query/nodes/range.rb +0 -63
- data/lib/chewy/query/nodes/raw.rb +0 -15
- data/lib/chewy/query/nodes/regexp.rb +0 -35
- data/lib/chewy/query/nodes/script.rb +0 -20
- data/lib/chewy/query/pagination.rb +0 -25
- data/spec/chewy/query/criteria_spec.rb +0 -700
- data/spec/chewy/query/filters_spec.rb +0 -201
- data/spec/chewy/query/loading_spec.rb +0 -124
- data/spec/chewy/query/nodes/and_spec.rb +0 -12
- data/spec/chewy/query/nodes/bool_spec.rb +0 -14
- data/spec/chewy/query/nodes/equal_spec.rb +0 -32
- data/spec/chewy/query/nodes/exists_spec.rb +0 -18
- data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
- data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
- data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
- data/spec/chewy/query/nodes/missing_spec.rb +0 -16
- data/spec/chewy/query/nodes/not_spec.rb +0 -14
- data/spec/chewy/query/nodes/or_spec.rb +0 -12
- data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
- data/spec/chewy/query/nodes/query_spec.rb +0 -12
- data/spec/chewy/query/nodes/range_spec.rb +0 -32
- data/spec/chewy/query/nodes/raw_spec.rb +0 -11
- data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
- data/spec/chewy/query/nodes/script_spec.rb +0 -15
- data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
- data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
- data/spec/chewy/query/pagination_spec.rb +0 -39
- data/spec/chewy/query_spec.rb +0 -637
- data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
@@ -12,14 +12,12 @@ module Chewy
|
|
12
12
|
include BoolStorage
|
13
13
|
|
14
14
|
# Renders `match_none` query if the values is set to true.
|
15
|
-
# Well, we can't really use match none because we need to support
|
16
|
-
# ES2, so we are simulating it with `match_all` negation.
|
17
15
|
#
|
18
16
|
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
|
19
17
|
# @see Chewy::Search::Request
|
20
18
|
# @see Chewy::Search::Request#response
|
21
19
|
def render
|
22
|
-
{query: {
|
20
|
+
{query: {match_none: {}}} if value.present?
|
23
21
|
end
|
24
22
|
end
|
25
23
|
end
|
data/lib/chewy/search/request.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Chewy
|
2
2
|
module Search
|
3
|
-
# The main
|
4
|
-
# Supports
|
3
|
+
# The main request DSL class. Supports multiple index requests.
|
4
|
+
# Supports ES5 search API and query DSL.
|
5
5
|
#
|
6
6
|
# @note The class tries to be as immutable as possible,
|
7
7
|
# so most of the methods return a new instance of the class.
|
@@ -26,8 +26,9 @@ module Chewy
|
|
26
26
|
timeout min_score source stored_fields search_after
|
27
27
|
load script_fields suggest aggs aggregations none
|
28
28
|
indices_boost rescore highlight total total_count
|
29
|
-
total_entries types delete_all count exists?
|
30
|
-
scroll_batches scroll_hits
|
29
|
+
total_entries indices types delete_all count exists?
|
30
|
+
exist? find pluck scroll_batches scroll_hits
|
31
|
+
scroll_results scroll_wrappers
|
31
32
|
].to_set.freeze
|
32
33
|
DEFAULT_BATCH_SIZE = 1000
|
33
34
|
DEFAULT_PLUCK_BATCH_SIZE = 10_000
|
@@ -51,24 +52,32 @@ module Chewy
|
|
51
52
|
alias_method :total_count, :total
|
52
53
|
alias_method :total_entries, :total
|
53
54
|
|
54
|
-
attr_reader :_indexes, :_types
|
55
|
-
|
56
55
|
# The class is initialized with the list of chewy indexes and/or
|
57
56
|
# types, which are later used to compose requests.
|
57
|
+
# Any symbol/string passed is treated as an index identifier.
|
58
58
|
#
|
59
59
|
# @example
|
60
|
+
# Chewy::Search::Request.new(:places)
|
61
|
+
# # => <Chewy::Search::Request {:index=>["places"]}>
|
60
62
|
# Chewy::Search::Request.new(PlacesIndex)
|
61
63
|
# # => <Chewy::Search::Request {:index=>["places"], :type=>["city", "country"]}>
|
62
64
|
# Chewy::Search::Request.new(PlacesIndex::City)
|
63
65
|
# # => <Chewy::Search::Request {:index=>["places"], :type=>["city"]}>
|
64
66
|
# Chewy::Search::Request.new(UsersIndex, PlacesIndex::City)
|
65
67
|
# # => <Chewy::Search::Request {:index=>["users", "places"], :type=>["city", "user"]}>
|
66
|
-
# @param indexes_or_types [Array<Chewy::Index, Chewy::Type>]
|
67
|
-
def initialize(*
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
68
|
+
# @param indexes_or_types [Array<Chewy::Index, Chewy::Type, String, Symbol>] indices and types in any combinations
|
69
|
+
def initialize(*indices_or_types)
|
70
|
+
indices = indices_or_types.reject do |klass|
|
71
|
+
klass.is_a?(Class) && klass < Chewy::Type
|
72
|
+
end
|
73
|
+
|
74
|
+
types = indices_or_types.select do |klass|
|
75
|
+
klass.is_a?(Class) && klass < Chewy::Type
|
76
|
+
end
|
77
|
+
|
78
|
+
parameters.modify!(:indices) do
|
79
|
+
replace!(indices: indices, types: types)
|
80
|
+
end
|
72
81
|
end
|
73
82
|
|
74
83
|
# Underlying parameter storage collection.
|
@@ -103,14 +112,23 @@ module Chewy
|
|
103
112
|
# @see Chewy::Search::Response
|
104
113
|
# @return [Chewy::Search::Response] a response object instance
|
105
114
|
def response
|
106
|
-
@response ||=
|
115
|
+
@response ||= build_response(perform)
|
116
|
+
end
|
117
|
+
|
118
|
+
# Wraps and sets the raw Elasticsearch response to provide access
|
119
|
+
# to convenience methods.
|
120
|
+
#
|
121
|
+
# @see Chewy::Search::Response
|
122
|
+
# @param from_elasticsearch [Hash] An Elasticsearch response
|
123
|
+
def response=(from_elasticsearch)
|
124
|
+
@response = build_response(from_elasticsearch)
|
107
125
|
end
|
108
126
|
|
109
127
|
# ES request body
|
110
128
|
#
|
111
129
|
# @return [Hash] request body
|
112
130
|
def render
|
113
|
-
@render ||=
|
131
|
+
@render ||= parameters.render
|
114
132
|
end
|
115
133
|
|
116
134
|
# Includes the class name and the result of rendering.
|
@@ -281,21 +299,39 @@ module Chewy
|
|
281
299
|
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-docvalue-fields.html
|
282
300
|
# @param values [Array<String, Symbol>] field names
|
283
301
|
# @return [Chewy::Search::Request]
|
302
|
+
%i[order docvalue_fields].each do |name|
|
303
|
+
define_method name do |value, *values|
|
304
|
+
modify(name) { update!([value, *values]) }
|
305
|
+
end
|
306
|
+
end
|
307
|
+
|
308
|
+
# @!method indices(*values)
|
309
|
+
# Modifies `index` request parameter. Updates the storage on every call.
|
310
|
+
# Added passed indexes to the parameter list.
|
311
|
+
#
|
312
|
+
# @example
|
313
|
+
# UsersIndex.indices(CitiesIndex).indices(:another)
|
314
|
+
# # => <UsersIndex::Query {:index=>["another", "cities", "users"], :type=>["city", "user"]}>
|
315
|
+
# @see Chewy::Search::Parameters::Indices
|
316
|
+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
|
317
|
+
# @param values [Array<Chewy::Index, String, Symbol>] index names
|
318
|
+
# @return [Chewy::Search::Request]
|
284
319
|
#
|
285
320
|
# @!method types(*values)
|
286
|
-
# Modifies `
|
287
|
-
# Constrains types passed on the request initialization
|
321
|
+
# Modifies `type` request parameter. Updates the storage on every call.
|
322
|
+
# Constrains types passed on the request initialization or adds them
|
323
|
+
# to the list depending on circumstances.
|
288
324
|
#
|
289
325
|
# @example
|
290
|
-
#
|
291
|
-
# # => <
|
292
|
-
# @see Chewy::Search::Parameters::
|
326
|
+
# UsersIndex.types(CitiesIndex::City).types(:unexistent)
|
327
|
+
# # => <UsersIndex::Query {:index=>["cities", "users"], :type=>["city", "user"]}>
|
328
|
+
# @see Chewy::Search::Parameters::Indices
|
293
329
|
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
|
294
|
-
# @param values [Array<String, Symbol>] type names
|
330
|
+
# @param values [Array<Chewy::Type, String, Symbol>] type names
|
295
331
|
# @return [Chewy::Search::Request]
|
296
|
-
%i[
|
332
|
+
%i[indices types].each do |name|
|
297
333
|
define_method name do |value, *values|
|
298
|
-
modify(
|
334
|
+
modify(:indices) { update!(name => [value, *values]) }
|
299
335
|
end
|
300
336
|
end
|
301
337
|
|
@@ -759,7 +795,7 @@ module Chewy
|
|
759
795
|
# @param values [Array<String, Symbol>]
|
760
796
|
# @return [Chewy::Search::Request] new scope
|
761
797
|
def only(*values)
|
762
|
-
chain { parameters.only!(values.flatten(1)) }
|
798
|
+
chain { parameters.only!(values.flatten(1) + [:indices]) }
|
763
799
|
end
|
764
800
|
|
765
801
|
# Returns a new scope containing all the storages except specified.
|
@@ -901,28 +937,28 @@ module Chewy
|
|
901
937
|
end
|
902
938
|
|
903
939
|
# Deletes all the documents from the specified scope it uses
|
904
|
-
# `delete_by_query`
|
905
|
-
# plugin, which requires additional installation effort.
|
940
|
+
# `delete_by_query`
|
906
941
|
#
|
907
942
|
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
|
908
|
-
# @see https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/plugins-delete-by-query.html
|
909
943
|
# @note The result hash is different for different API used.
|
910
944
|
# @param refresh [true, false] field names
|
911
945
|
# @return [Hash] the result of query execution
|
912
946
|
def delete_all(refresh: true)
|
913
947
|
request_body = only(WHERE_STORAGES).render.merge(refresh: refresh)
|
914
948
|
ActiveSupport::Notifications.instrument 'delete_query.chewy',
|
915
|
-
request: request_body
|
916
|
-
|
917
|
-
|
918
|
-
if Runtime.version < '5.0'
|
919
|
-
delete_by_query_plugin(request_body)
|
920
|
-
else
|
921
|
-
Chewy.client.delete_by_query(request_body)
|
922
|
-
end
|
949
|
+
notification_payload(request: request_body) do
|
950
|
+
request_body[:body] = {query: {match_all: {}}} if request_body[:body].empty?
|
951
|
+
Chewy.client.delete_by_query(request_body)
|
923
952
|
end
|
924
953
|
end
|
925
954
|
|
955
|
+
# Returns whether or not the query has been performed.
|
956
|
+
#
|
957
|
+
# @return [true, false]
|
958
|
+
def performed?
|
959
|
+
!@response.nil?
|
960
|
+
end
|
961
|
+
|
926
962
|
protected
|
927
963
|
|
928
964
|
def initialize_clone(origin)
|
@@ -932,10 +968,12 @@ module Chewy
|
|
932
968
|
|
933
969
|
private
|
934
970
|
|
971
|
+
def build_response(raw_response)
|
972
|
+
Response.new(raw_response, loader, collection_paginator)
|
973
|
+
end
|
974
|
+
|
935
975
|
def compare_internals(other)
|
936
|
-
|
937
|
-
_types.sort_by(&:derivable_name) == other._types.sort_by(&:derivable_name) &&
|
938
|
-
parameters == other.parameters
|
976
|
+
parameters == other.parameters
|
939
977
|
end
|
940
978
|
|
941
979
|
def modify(name, &block)
|
@@ -947,58 +985,51 @@ module Chewy
|
|
947
985
|
end
|
948
986
|
|
949
987
|
def reset
|
950
|
-
@response, @render, @
|
988
|
+
@response, @render, @loader = nil
|
951
989
|
end
|
952
990
|
|
953
991
|
def perform(additional = {})
|
954
992
|
request_body = render.merge(additional)
|
993
|
+
request_body[:rest_total_hits_as_int] = true if Chewy::Runtime::Version.new(Chewy.client.info['version']['number']) >= '7.0.0'
|
955
994
|
ActiveSupport::Notifications.instrument 'search_query.chewy',
|
956
|
-
request: request_body
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
{}
|
995
|
+
notification_payload(request: request_body) do
|
996
|
+
begin
|
997
|
+
Chewy.client.search(request_body)
|
998
|
+
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
999
|
+
{}
|
1000
|
+
end
|
963
1001
|
end
|
964
|
-
end
|
965
1002
|
end
|
966
1003
|
|
967
|
-
def
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
1004
|
+
def notification_payload(additional)
|
1005
|
+
{
|
1006
|
+
indexes: _indices, types: _types,
|
1007
|
+
index: _indices.one? ? _indices.first : _indices,
|
1008
|
+
type: _types.one? ? _types.first : _types
|
1009
|
+
}.merge(additional)
|
973
1010
|
end
|
974
1011
|
|
975
|
-
def
|
976
|
-
|
1012
|
+
def _indices
|
1013
|
+
parameters[:indices].indices
|
977
1014
|
end
|
978
1015
|
|
979
|
-
def
|
980
|
-
|
981
|
-
_types.map(&:type_name).uniq & parameters[:types].value
|
982
|
-
else
|
983
|
-
_types.map(&:type_name).uniq
|
984
|
-
end
|
1016
|
+
def _types
|
1017
|
+
parameters[:indices].types
|
985
1018
|
end
|
986
1019
|
|
987
|
-
def
|
988
|
-
|
1020
|
+
def raw_limit_value
|
1021
|
+
parameters[:limit].value
|
989
1022
|
end
|
990
1023
|
|
991
|
-
def
|
992
|
-
|
993
|
-
Elasticsearch::API::Utils.__listify(request[:index]),
|
994
|
-
Elasticsearch::API::Utils.__listify(request[:type]),
|
995
|
-
'_query'
|
996
|
-
)
|
997
|
-
Chewy.client.perform_request(Elasticsearch::API::HTTP_DELETE, path, {}, request[:body]).body
|
1024
|
+
def raw_offset_value
|
1025
|
+
parameters[:offset].value
|
998
1026
|
end
|
999
1027
|
|
1000
1028
|
def loader
|
1001
|
-
@loader ||= Loader.new(
|
1029
|
+
@loader ||= Loader.new(
|
1030
|
+
indexes: parameters[:indices].indices,
|
1031
|
+
**parameters[:load].value
|
1032
|
+
)
|
1002
1033
|
end
|
1003
1034
|
|
1004
1035
|
def fetch_field(hit, field)
|
@@ -1009,10 +1040,6 @@ module Chewy
|
|
1009
1040
|
end
|
1010
1041
|
end
|
1011
1042
|
|
1012
|
-
def performed?
|
1013
|
-
!@response.nil?
|
1014
|
-
end
|
1015
|
-
|
1016
1043
|
def collection_paginator
|
1017
1044
|
method(:paginated_collection).to_proc if respond_to?(:paginated_collection, true)
|
1018
1045
|
end
|
@@ -31,16 +31,19 @@ module Chewy
|
|
31
31
|
total = [raw_limit_value, result.fetch('hits', {}).fetch('total', 0)].compact.min
|
32
32
|
last_batch_size = total % batch_size
|
33
33
|
fetched = 0
|
34
|
+
scroll_id = nil
|
34
35
|
|
35
36
|
loop do
|
36
37
|
hits = result.fetch('hits', {}).fetch('hits', [])
|
37
38
|
fetched += hits.size
|
38
39
|
hits = hits.first(last_batch_size) if last_batch_size != 0 && fetched >= total
|
39
40
|
yield(hits) if hits.present?
|
40
|
-
break if fetched >= total
|
41
41
|
scroll_id = result['_scroll_id']
|
42
|
+
break if fetched >= total
|
42
43
|
result = perform_scroll(scroll: scroll, scroll_id: scroll_id)
|
43
44
|
end
|
45
|
+
ensure
|
46
|
+
Chewy.client.clear_scroll(scroll_id: scroll_id) if scroll_id
|
44
47
|
end
|
45
48
|
|
46
49
|
# @!method scroll_hits(batch_size: 1000, scroll: '1m')
|
@@ -125,11 +128,9 @@ module Chewy
|
|
125
128
|
|
126
129
|
def perform_scroll(body)
|
127
130
|
ActiveSupport::Notifications.instrument 'search_query.chewy',
|
128
|
-
request: body
|
129
|
-
|
130
|
-
|
131
|
-
Chewy.client.scroll(body)
|
132
|
-
end
|
131
|
+
notification_payload(request: body) do
|
132
|
+
Chewy.client.scroll(body)
|
133
|
+
end
|
133
134
|
end
|
134
135
|
end
|
135
136
|
end
|
data/lib/chewy/stash.rb
CHANGED
@@ -24,7 +24,7 @@ module Chewy
|
|
24
24
|
# @param since_time [Time, DateTime] a timestamp from which we load a journal
|
25
25
|
# @param only [Chewy::Index, Array<Chewy::Index>] journal entries related to these indices will be loaded only
|
26
26
|
def self.entries(since_time, only: [])
|
27
|
-
self.for(only).filter(range: {created_at: {gt: since_time}})
|
27
|
+
self.for(only).filter(range: {created_at: {gt: since_time}}).filter.minimum_should_match(1)
|
28
28
|
end
|
29
29
|
|
30
30
|
# Cleans up all the journal entries until the specified time. If nothing is
|
@@ -11,7 +11,7 @@ module Chewy
|
|
11
11
|
#
|
12
12
|
class ActiveJob < Atomic
|
13
13
|
class Worker < ::ActiveJob::Base
|
14
|
-
queue_as :chewy
|
14
|
+
queue_as { Chewy.settings.dig(:active_job, :queue) || 'chewy' }
|
15
15
|
|
16
16
|
def perform(type, ids, options = {})
|
17
17
|
options[:refresh] = !Chewy.disable_refresh_async if Chewy.disable_refresh_async
|
data/lib/chewy/type.rb
CHANGED
@@ -14,7 +14,10 @@ require 'chewy/type/witchcraft'
|
|
14
14
|
|
15
15
|
module Chewy
|
16
16
|
class Type
|
17
|
-
IMPORT_OPTIONS_KEYS = %i[
|
17
|
+
IMPORT_OPTIONS_KEYS = %i[
|
18
|
+
batch_size bulk_size consistency direct_import journal
|
19
|
+
pipeline raw_import refresh replication
|
20
|
+
].freeze
|
18
21
|
|
19
22
|
include Search
|
20
23
|
include Mapping
|
@@ -22,7 +22,7 @@ module Chewy
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def import_scope(scope, options)
|
25
|
-
pluck_in_batches(scope, options.slice(:batch_size)).inject(true) do |result, ids|
|
25
|
+
pluck_in_batches(scope, **options.slice(:batch_size)).inject(true) do |result, ids|
|
26
26
|
objects = if options[:raw_import]
|
27
27
|
raw_default_scope_where_ids_in(ids, options[:raw_import])
|
28
28
|
else
|
@@ -25,7 +25,7 @@ module Chewy
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def import_scope(scope, options)
|
28
|
-
pluck_in_batches(scope, options.slice(:batch_size)).map do |ids|
|
28
|
+
pluck_in_batches(scope, **options.slice(:batch_size)).map do |ids|
|
29
29
|
yield grouped_objects(default_scope_where_ids_in(ids))
|
30
30
|
end.all?
|
31
31
|
end
|
@@ -45,6 +45,7 @@ module Chewy
|
|
45
45
|
# Import options:
|
46
46
|
#
|
47
47
|
# <tt>:batch_size</tt> - import batch size, 1000 objects by default
|
48
|
+
# <tt>:direct_import</tt> - import objects without reloading
|
48
49
|
#
|
49
50
|
# Method handles destroyed objects as well. In case of objects ORM scope
|
50
51
|
# or array passed, objects, responding with true to `destroyed?` method will be deleted
|
@@ -75,10 +76,10 @@ module Chewy
|
|
75
76
|
def import(*args, &block)
|
76
77
|
collection, options = import_args(*args)
|
77
78
|
|
78
|
-
if collection.is_a?(relation_class)
|
79
|
-
import_scope(collection, options, &block)
|
80
|
-
else
|
79
|
+
if !collection.is_a?(relation_class) || options[:direct_import]
|
81
80
|
import_objects(collection, options, &block)
|
81
|
+
else
|
82
|
+
import_scope(collection, options, &block)
|
82
83
|
end
|
83
84
|
end
|
84
85
|
|
@@ -89,7 +90,7 @@ module Chewy
|
|
89
90
|
|
90
91
|
if options[:fields].present? || collection.is_a?(relation_class)
|
91
92
|
collection = all_scope_where_ids_in(identify(collection)) unless collection.is_a?(relation_class)
|
92
|
-
pluck_in_batches(collection, options.slice(:fields, :batch_size, :typecast), &block)
|
93
|
+
pluck_in_batches(collection, **options.slice(:fields, :batch_size, :typecast), &block)
|
93
94
|
else
|
94
95
|
identify(collection).each_slice(options[:batch_size]) do |batch|
|
95
96
|
yield batch
|
@@ -119,6 +120,8 @@ module Chewy
|
|
119
120
|
indexed = collection_ids.each_slice(options[:batch_size]).map do |ids|
|
120
121
|
batch = if options[:raw_import]
|
121
122
|
raw_default_scope_where_ids_in(ids, options[:raw_import])
|
123
|
+
elsif options[:direct_import]
|
124
|
+
hash.values_at(*ids.map(&:to_s))
|
122
125
|
else
|
123
126
|
default_scope_where_ids_in(ids)
|
124
127
|
end
|