chewy 0.10.0 → 6.0.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 +240 -0
- data/.rubocop.yml +25 -25
- data/Appraisals +12 -10
- data/CHANGELOG.md +252 -263
- data/Gemfile +5 -1
- data/LICENSE.txt +1 -1
- data/README.md +142 -78
- data/chewy.gemspec +10 -12
- data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
- data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
- data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
- data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
- data/gemfiles/sequel.4.45.gemfile +2 -2
- data/lib/chewy.rb +2 -1
- data/lib/chewy/backports/duplicable.rb +1 -1
- data/lib/chewy/config.rb +10 -39
- data/lib/chewy/fields/base.rb +40 -28
- data/lib/chewy/fields/root.rb +18 -11
- data/lib/chewy/index.rb +3 -1
- data/lib/chewy/index/actions.rb +27 -15
- data/lib/chewy/index/settings.rb +2 -0
- data/lib/chewy/index/specification.rb +12 -10
- data/lib/chewy/minitest/helpers.rb +6 -6
- data/lib/chewy/minitest/search_index_receiver.rb +17 -17
- data/lib/chewy/multi_search.rb +62 -0
- data/lib/chewy/railtie.rb +4 -4
- data/lib/chewy/rake_helper.rb +5 -5
- data/lib/chewy/rspec/update_index.rb +3 -5
- data/lib/chewy/search.rb +4 -11
- data/lib/chewy/search/loader.rb +1 -1
- data/lib/chewy/search/pagination/will_paginate.rb +4 -2
- 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/concerns/query_storage.rb +4 -3
- data/lib/chewy/search/parameters/indices.rb +123 -0
- data/lib/chewy/search/parameters/none.rb +1 -3
- data/lib/chewy/search/request.rb +100 -74
- data/lib/chewy/search/scrolling.rb +7 -6
- data/lib/chewy/stash.rb +30 -21
- data/lib/chewy/strategy/active_job.rb +1 -1
- data/lib/chewy/strategy/atomic.rb +1 -1
- data/lib/chewy/strategy/sidekiq.rb +1 -1
- data/lib/chewy/type.rb +5 -2
- data/lib/chewy/type/adapter/active_record.rb +1 -1
- data/lib/chewy/type/adapter/base.rb +9 -9
- data/lib/chewy/type/adapter/mongoid.rb +2 -4
- data/lib/chewy/type/adapter/orm.rb +7 -4
- data/lib/chewy/type/adapter/sequel.rb +5 -7
- data/lib/chewy/type/crutch.rb +1 -1
- data/lib/chewy/type/import.rb +13 -11
- data/lib/chewy/type/import/bulk_builder.rb +1 -1
- data/lib/chewy/type/import/bulk_request.rb +4 -2
- data/lib/chewy/type/import/journal_builder.rb +3 -3
- data/lib/chewy/type/import/routine.rb +3 -3
- data/lib/chewy/type/mapping.rb +42 -36
- data/lib/chewy/type/observe.rb +16 -12
- data/lib/chewy/type/syncer.rb +15 -14
- data/lib/chewy/type/witchcraft.rb +11 -7
- data/lib/chewy/type/wrapper.rb +14 -4
- data/lib/chewy/version.rb +1 -1
- data/lib/sequel/plugins/chewy_observe.rb +4 -19
- data/migration_guide.md +18 -0
- data/spec/chewy/config_spec.rb +16 -21
- data/spec/chewy/fields/base_spec.rb +70 -70
- data/spec/chewy/fields/root_spec.rb +56 -9
- data/spec/chewy/index/actions_spec.rb +63 -7
- data/spec/chewy/index/specification_spec.rb +25 -16
- data/spec/chewy/index_spec.rb +75 -45
- data/spec/chewy/journal_spec.rb +33 -29
- 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 +123 -95
- data/spec/chewy/rspec/update_index_spec.rb +47 -46
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/pagination/kaminari_spec.rb +7 -3
- data/spec/chewy/search/pagination/will_paginate_spec.rb +9 -3
- 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 +101 -70
- data/spec/chewy/search/response_spec.rb +27 -17
- data/spec/chewy/search/scrolling_spec.rb +25 -16
- data/spec/chewy/search_spec.rb +49 -35
- data/spec/chewy/stash_spec.rb +15 -13
- data/spec/chewy/strategy/active_job_spec.rb +15 -2
- data/spec/chewy/strategy/shoryuken_spec.rb +8 -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 +17 -15
- data/spec/chewy/type/import_spec.rb +6 -0
- data/spec/chewy/type/mapping_spec.rb +51 -18
- data/spec/chewy/type/observe_spec.rb +4 -4
- data/spec/chewy/type/witchcraft_spec.rb +31 -0
- data/spec/chewy/type/wrapper_spec.rb +3 -1
- data/spec/chewy_spec.rb +0 -7
- data/spec/spec_helper.rb +5 -1
- data/spec/support/active_record.rb +20 -0
- metadata +46 -116
- data/.travis.yml +0 -53
- data/LEGACY_DSL.md +0 -497
- data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
- data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
- data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
- data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
- data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
- data/lib/chewy/query.rb +0 -1098
- data/lib/chewy/query/compose.rb +0 -68
- data/lib/chewy/query/criteria.rb +0 -191
- data/lib/chewy/query/filters.rb +0 -227
- data/lib/chewy/query/loading.rb +0 -111
- 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 -13
- 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 -636
- data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'chewy/search/parameters/storage'
|
2
|
+
|
3
|
+
module Chewy
|
4
|
+
module Search
|
5
|
+
class Parameters
|
6
|
+
# Stores boolean value, but has 3 states: `true`, `false` and `nil`.
|
7
|
+
#
|
8
|
+
# @see Chewy::Search::Request#allow_partial_search_results
|
9
|
+
# @see https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4
|
10
|
+
class AllowPartialSearchResults < Storage
|
11
|
+
# We don't want to render `nil`, but render `true` and `false` values.
|
12
|
+
#
|
13
|
+
# @see Chewy::Search::Parameters::Storage#render
|
14
|
+
# @return [{Symbol => Object}, nil]
|
15
|
+
def render
|
16
|
+
{self.class.param_name => value} unless value.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def normalize(value)
|
22
|
+
!!value unless value.nil?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -20,16 +20,17 @@ module Chewy
|
|
20
20
|
# rendering logic.
|
21
21
|
#
|
22
22
|
# @!attribute must
|
23
|
-
# @return [Array<Hash
|
23
|
+
# @return [Array<Hash>, Hash, nil]
|
24
24
|
# @!attribute should
|
25
|
-
# @return [Array<Hash
|
25
|
+
# @return [Array<Hash>, Hash, nil]
|
26
26
|
# @!attribute must_not
|
27
|
-
# @return [Array<Hash
|
27
|
+
# @return [[Array<Hash>, Hash, nil]
|
28
28
|
# @!attribute minimum_should_match
|
29
29
|
# @return [String, Integer, nil]
|
30
30
|
class Bool
|
31
31
|
# Acceptable bool query keys
|
32
32
|
KEYS = %i[must should must_not minimum_should_match].freeze
|
33
|
+
# @!ignorewarning
|
33
34
|
attr_reader(*KEYS)
|
34
35
|
|
35
36
|
# @param must [Array<Hash>, Hash, nil]
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'chewy/search/parameters/storage'
|
2
|
+
|
3
|
+
module Chewy
|
4
|
+
module Search
|
5
|
+
class Parameters
|
6
|
+
# Stores indices and/or types to query.
|
7
|
+
# Renders it to lists of string accepted by ElasticSearch
|
8
|
+
# API.
|
9
|
+
#
|
10
|
+
# The semantics behind it can be described in the
|
11
|
+
# following statements:
|
12
|
+
# 1. If index is added to the storage, no matter, a class
|
13
|
+
# or a string/symbol, it gets appended to the list.
|
14
|
+
# 2. If type is added to the storage, it filters out types
|
15
|
+
# assigned via indices.
|
16
|
+
# 3. But when a type class with non-existing index is added,
|
17
|
+
# this index got also added to the list if indices.
|
18
|
+
# 4. In cases when of an index identifier added, type
|
19
|
+
# indetifiers also got appended instead of filtering.
|
20
|
+
class Indices < Storage
|
21
|
+
# Two index storages are equal if they produce the
|
22
|
+
# same output on render.
|
23
|
+
#
|
24
|
+
# @see Chewy::Search::Parameters::Storage#==
|
25
|
+
# @param other [Chewy::Search::Parameters::Storage] any storage instance
|
26
|
+
# @return [true, false] the result of comparision
|
27
|
+
def ==(other)
|
28
|
+
super || other.class == self.class && other.render == render
|
29
|
+
end
|
30
|
+
|
31
|
+
# Just adds types to types and indices to indices.
|
32
|
+
#
|
33
|
+
# @see Chewy::Search::Parameters::Storage#update!
|
34
|
+
# @param other_value [{Symbol => Array<Chewy::Index, Chewy::Type, String, Symbol>}] any acceptable storage value
|
35
|
+
# @return [{Symbol => Array<Chewy::Index, Chewy::Type, String, Symbol>}] updated value
|
36
|
+
def update!(other_value)
|
37
|
+
new_value = normalize(other_value)
|
38
|
+
|
39
|
+
@value = {
|
40
|
+
indices: value[:indices] | new_value[:indices],
|
41
|
+
types: value[:types] | new_value[:types]
|
42
|
+
}
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns desired index and type names.
|
46
|
+
#
|
47
|
+
# @see Chewy::Search::Parameters::Storage#render
|
48
|
+
# @return [{Symbol => Array<String>}] rendered value with the parameter name
|
49
|
+
def render
|
50
|
+
{
|
51
|
+
index: index_names.uniq.sort,
|
52
|
+
type: type_names.uniq.sort
|
53
|
+
}.reject { |_, v| v.blank? }
|
54
|
+
end
|
55
|
+
|
56
|
+
# Returns index classes used for the request.
|
57
|
+
# No strings/symbos included.
|
58
|
+
#
|
59
|
+
# @return [Array<Chewy::Index>] a list of index classes
|
60
|
+
def indices
|
61
|
+
index_classes | type_classes.map(&:index)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Returns type classes used for the request.
|
65
|
+
# No strings/symbos included.
|
66
|
+
#
|
67
|
+
# @return [Array<Chewy::Type>] a list of types classes
|
68
|
+
def types
|
69
|
+
type_classes | (index_classes - type_classes.map(&:index)).flat_map(&:types)
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def initialize_clone(origin)
|
75
|
+
@value = origin.value.dup
|
76
|
+
end
|
77
|
+
|
78
|
+
def normalize(value)
|
79
|
+
value ||= {}
|
80
|
+
|
81
|
+
{
|
82
|
+
indices: Array.wrap(value[:indices]).flatten.compact,
|
83
|
+
types: Array.wrap(value[:types]).flatten.compact
|
84
|
+
}
|
85
|
+
end
|
86
|
+
|
87
|
+
def index_classes
|
88
|
+
value[:indices].select do |klass|
|
89
|
+
klass.is_a?(Class) && klass < Chewy::Index
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def index_identifiers
|
94
|
+
value[:indices] - index_classes
|
95
|
+
end
|
96
|
+
|
97
|
+
def index_names
|
98
|
+
indices.map(&:index_name) | index_identifiers.map(&:to_s)
|
99
|
+
end
|
100
|
+
|
101
|
+
def type_classes
|
102
|
+
value[:types].select do |klass|
|
103
|
+
klass.is_a?(Class) && klass < Chewy::Type
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def type_identifiers
|
108
|
+
value[:types] - type_classes
|
109
|
+
end
|
110
|
+
|
111
|
+
def type_names
|
112
|
+
type_names = types.map(&:type_name)
|
113
|
+
|
114
|
+
if index_identifiers.blank? && type_identifiers.present?
|
115
|
+
(type_names & type_identifiers.map(&:to_s)).presence || type_names
|
116
|
+
else
|
117
|
+
type_names | type_identifiers.map(&:to_s)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
@@ -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,50 @@ 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)
|
955
993
|
ActiveSupport::Notifications.instrument 'search_query.chewy',
|
956
|
-
request: request_body
|
957
|
-
|
958
|
-
|
959
|
-
|
960
|
-
|
961
|
-
|
962
|
-
{}
|
994
|
+
notification_payload(request: request_body) do
|
995
|
+
begin
|
996
|
+
Chewy.client.search(request_body)
|
997
|
+
rescue Elasticsearch::Transport::Transport::Errors::NotFound
|
998
|
+
{}
|
999
|
+
end
|
963
1000
|
end
|
964
|
-
end
|
965
1001
|
end
|
966
1002
|
|
967
|
-
def
|
968
|
-
|
969
|
-
|
970
|
-
|
971
|
-
|
972
|
-
|
1003
|
+
def notification_payload(additional)
|
1004
|
+
{
|
1005
|
+
indexes: _indices, types: _types,
|
1006
|
+
index: _indices.one? ? _indices.first : _indices,
|
1007
|
+
type: _types.one? ? _types.first : _types
|
1008
|
+
}.merge(additional)
|
973
1009
|
end
|
974
1010
|
|
975
|
-
def
|
976
|
-
|
1011
|
+
def _indices
|
1012
|
+
parameters[:indices].indices
|
977
1013
|
end
|
978
1014
|
|
979
|
-
def
|
980
|
-
|
981
|
-
_types.map(&:type_name).uniq & parameters[:types].value
|
982
|
-
else
|
983
|
-
_types.map(&:type_name).uniq
|
984
|
-
end
|
1015
|
+
def _types
|
1016
|
+
parameters[:indices].types
|
985
1017
|
end
|
986
1018
|
|
987
|
-
def
|
988
|
-
|
1019
|
+
def raw_limit_value
|
1020
|
+
parameters[:limit].value
|
989
1021
|
end
|
990
1022
|
|
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
|
1023
|
+
def raw_offset_value
|
1024
|
+
parameters[:offset].value
|
998
1025
|
end
|
999
1026
|
|
1000
1027
|
def loader
|
1001
|
-
@loader ||= Loader.new(
|
1028
|
+
@loader ||= Loader.new(
|
1029
|
+
indexes: parameters[:indices].indices,
|
1030
|
+
**parameters[:load].value
|
1031
|
+
)
|
1002
1032
|
end
|
1003
1033
|
|
1004
1034
|
def fetch_field(hit, field)
|
@@ -1009,10 +1039,6 @@ module Chewy
|
|
1009
1039
|
end
|
1010
1040
|
end
|
1011
1041
|
|
1012
|
-
def performed?
|
1013
|
-
!@response.nil?
|
1014
|
-
end
|
1015
|
-
|
1016
1042
|
def collection_paginator
|
1017
1043
|
method(:paginated_collection).to_proc if respond_to?(:paginated_collection, true)
|
1018
1044
|
end
|