chewy 0.9.0 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/.rubocop.yml +24 -2
- data/.rubocop_todo.yml +2 -2
- data/.travis.yml +38 -21
- data/.yardopts +5 -0
- data/Appraisals +55 -27
- data/CHANGELOG.md +57 -12
- data/Gemfile +14 -10
- data/LEGACY_DSL.md +497 -0
- data/README.md +249 -515
- data/chewy.gemspec +5 -4
- data/gemfiles/rails.4.0.activerecord.gemfile +14 -0
- data/gemfiles/rails.4.1.activerecord.gemfile +14 -0
- data/gemfiles/rails.4.2.activerecord.gemfile +8 -10
- data/gemfiles/rails.4.2.mongoid.5.1.gemfile +9 -10
- data/gemfiles/rails.5.0.activerecord.gemfile +8 -10
- data/gemfiles/rails.5.0.mongoid.6.0.gemfile +15 -0
- data/gemfiles/rails.5.1.activerecord.gemfile +15 -0
- data/gemfiles/rails.5.1.mongoid.6.1.gemfile +15 -0
- data/gemfiles/sequel.4.45.gemfile +11 -0
- data/lib/chewy.rb +77 -43
- data/lib/chewy/config.rb +44 -7
- data/lib/chewy/errors.rb +2 -2
- data/lib/chewy/fields/base.rb +39 -32
- data/lib/chewy/fields/root.rb +33 -7
- data/lib/chewy/index.rb +237 -149
- data/lib/chewy/index/actions.rb +85 -28
- data/lib/chewy/index/aliases.rb +2 -1
- data/lib/chewy/index/settings.rb +9 -5
- data/lib/chewy/index/specification.rb +58 -0
- data/lib/chewy/journal.rb +40 -92
- data/lib/chewy/query.rb +43 -27
- data/lib/chewy/query/compose.rb +13 -13
- data/lib/chewy/query/criteria.rb +13 -13
- data/lib/chewy/query/filters.rb +1 -1
- data/lib/chewy/query/loading.rb +1 -1
- 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/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 +1 -0
- data/lib/chewy/rake_helper.rb +265 -48
- data/lib/chewy/rspec/update_index.rb +30 -22
- data/lib/chewy/search.rb +78 -21
- 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 +41 -0
- data/lib/chewy/search/parameters.rb +150 -0
- data/lib/chewy/search/parameters/aggs.rb +16 -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 +237 -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_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 +1021 -0
- data/lib/chewy/search/response.rb +119 -0
- data/lib/chewy/search/scoping.rb +50 -0
- data/lib/chewy/search/scrolling.rb +136 -0
- data/lib/chewy/stash.rb +70 -0
- data/lib/chewy/strategy.rb +10 -3
- data/lib/chewy/strategy/active_job.rb +1 -0
- data/lib/chewy/strategy/atomic.rb +1 -3
- 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 +18 -7
- 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 +32 -16
- data/lib/chewy/type/import.rb +145 -191
- data/lib/chewy/type/import/bulk_builder.rb +122 -0
- data/lib/chewy/type/import/bulk_request.rb +76 -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 +11 -1
- data/lib/chewy/type/observe.rb +1 -1
- data/lib/chewy/type/syncer.rb +220 -0
- data/lib/chewy/type/witchcraft.rb +27 -13
- data/lib/chewy/type/wrapper.rb +28 -2
- data/lib/chewy/version.rb +1 -1
- data/lib/tasks/chewy.rake +84 -26
- data/spec/chewy/config_spec.rb +82 -1
- data/spec/chewy/fields/base_spec.rb +147 -112
- data/spec/chewy/fields/root_spec.rb +75 -18
- data/spec/chewy/fields/time_fields_spec.rb +2 -3
- data/spec/chewy/index/actions_spec.rb +180 -50
- 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 +160 -0
- data/spec/chewy/index_spec.rb +57 -66
- data/spec/chewy/journal_spec.rb +149 -54
- 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 +15 -15
- 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 +3 -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 +501 -560
- data/spec/chewy/rake_helper_spec.rb +368 -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 +17 -0
- data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
- data/spec/chewy/search/pagination/will_paginate_spec.rb +17 -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_boost_spec.rb +83 -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 +130 -0
- data/spec/chewy/search/query_proxy_spec.rb +68 -0
- data/spec/chewy/search/request_spec.rb +669 -0
- data/spec/chewy/search/response_spec.rb +192 -0
- data/spec/chewy/search/scrolling_spec.rb +169 -0
- data/spec/chewy/search_spec.rb +13 -6
- data/spec/chewy/stash_spec.rb +95 -0
- data/spec/chewy/strategy/active_job_spec.rb +6 -0
- data/spec/chewy/strategy/resque_spec.rb +6 -0
- data/spec/chewy/strategy/shoryuken_spec.rb +64 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +8 -0
- 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 +350 -271
- data/spec/chewy/type/mapping_spec.rb +54 -18
- data/spec/chewy/type/observe_spec.rb +5 -1
- data/spec/chewy/type/syncer_spec.rb +123 -0
- data/spec/chewy/type/witchcraft_spec.rb +45 -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 +5 -2
- 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 +198 -37
- 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.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.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.will_paginate.gemfile +0 -15
- 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
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chewy::Search::Response, :orm do
|
4
|
+
before { Chewy.massacre }
|
5
|
+
|
6
|
+
before do
|
7
|
+
stub_model(:city)
|
8
|
+
stub_model(:country)
|
9
|
+
|
10
|
+
stub_index(:places) do
|
11
|
+
define_type City do
|
12
|
+
field :name
|
13
|
+
field :rating, type: 'integer'
|
14
|
+
end
|
15
|
+
|
16
|
+
define_type Country do
|
17
|
+
field :name
|
18
|
+
field :rating, type: 'integer'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
before { PlacesIndex.import!(cities: cities, countries: countries) }
|
24
|
+
|
25
|
+
let(:cities) { Array.new(2) { |i| City.create!(rating: i, name: "city #{i}") } }
|
26
|
+
let(:countries) { Array.new(2) { |i| Country.create!(rating: i + 2, name: "country #{i}") } }
|
27
|
+
|
28
|
+
let(:request) { Chewy::Search::Request.new(PlacesIndex).order(:rating) }
|
29
|
+
let(:raw_response) { request.send(:perform) }
|
30
|
+
let(:load_options) { {} }
|
31
|
+
let(:loader) { Chewy::Search::Loader.new(indexes: [PlacesIndex], **load_options) }
|
32
|
+
subject { described_class.new(raw_response, loader) }
|
33
|
+
|
34
|
+
describe '#hits' do
|
35
|
+
specify { expect(subject.hits).to be_a(Array) }
|
36
|
+
specify { expect(subject.hits).to have(4).items }
|
37
|
+
specify { expect(subject.hits).to all be_a(Hash) }
|
38
|
+
specify do
|
39
|
+
expect(subject.hits.flat_map(&:keys).uniq)
|
40
|
+
.to match_array(%w[_id _index _type _score _source sort])
|
41
|
+
end
|
42
|
+
|
43
|
+
context do
|
44
|
+
let(:raw_response) { {} }
|
45
|
+
specify { expect(subject.hits).to eq([]) }
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
describe '#total' do
|
50
|
+
specify { expect(subject.total).to eq(4) }
|
51
|
+
|
52
|
+
context do
|
53
|
+
let(:raw_response) { {} }
|
54
|
+
specify { expect(subject.total).to eq(0) }
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#max_score' do
|
59
|
+
specify { expect(subject.max_score).to be_nil }
|
60
|
+
|
61
|
+
context do
|
62
|
+
let(:request) { Chewy::Search::Request.new(PlacesIndex).query(range: {rating: {lte: 42}}) }
|
63
|
+
specify { expect(subject.max_score).to eq(1.0) }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
describe '#took' do
|
68
|
+
specify { expect(subject.took).to be >= 0 }
|
69
|
+
|
70
|
+
context do
|
71
|
+
let(:request) do
|
72
|
+
Chewy::Search::Request.new(PlacesIndex)
|
73
|
+
.query(script: {script: {inline: 'sleep(100); true', lang: 'groovy'}})
|
74
|
+
end
|
75
|
+
specify { expect(subject.took).to be > 100 }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe '#timed_out?' do
|
80
|
+
specify { expect(subject.timed_out?).to eq(false) }
|
81
|
+
|
82
|
+
context do
|
83
|
+
let(:request) do
|
84
|
+
Chewy::Search::Request.new(PlacesIndex)
|
85
|
+
.query(script: {script: {inline: 'sleep(100); true', lang: 'groovy'}}).timeout('10ms')
|
86
|
+
end
|
87
|
+
specify { expect(subject.timed_out?).to eq(true) }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe '#suggest' do
|
92
|
+
specify { expect(subject.suggest).to eq({}) }
|
93
|
+
|
94
|
+
context do
|
95
|
+
let(:request) do
|
96
|
+
Chewy::Search::Request.new(PlacesIndex).suggest(
|
97
|
+
my_suggestion: {
|
98
|
+
text: 'city country',
|
99
|
+
term: {
|
100
|
+
field: 'name'
|
101
|
+
}
|
102
|
+
}
|
103
|
+
)
|
104
|
+
end
|
105
|
+
specify do
|
106
|
+
expect(subject.suggest).to eq(
|
107
|
+
'my_suggestion' => [
|
108
|
+
{'text' => 'city', 'offset' => 0, 'length' => 4, 'options' => []},
|
109
|
+
{'text' => 'country', 'offset' => 5, 'length' => 7, 'options' => []}
|
110
|
+
]
|
111
|
+
)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
describe '#aggs' do
|
117
|
+
specify { expect(subject.aggs).to eq({}) }
|
118
|
+
|
119
|
+
context do
|
120
|
+
let(:request) { Chewy::Search::Request.new(PlacesIndex).aggs(avg_rating: {avg: {field: :rating}}) }
|
121
|
+
specify { expect(subject.aggs).to eq('avg_rating' => {'value' => 1.5}) }
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
describe '#wrappers' do
|
126
|
+
specify { expect(subject.wrappers).to be_a(Array) }
|
127
|
+
specify { expect(subject.wrappers).to have(4).items }
|
128
|
+
specify do
|
129
|
+
expect(subject.wrappers.map(&:class).uniq)
|
130
|
+
.to contain_exactly(PlacesIndex::City, PlacesIndex::Country)
|
131
|
+
end
|
132
|
+
specify { expect(subject.wrappers.map(&:_data)).to eq(subject.hits) }
|
133
|
+
|
134
|
+
context do
|
135
|
+
let(:raw_response) { {} }
|
136
|
+
specify { expect(subject.wrappers).to eq([]) }
|
137
|
+
end
|
138
|
+
|
139
|
+
context do
|
140
|
+
let(:raw_response) { {'hits' => {}} }
|
141
|
+
specify { expect(subject.wrappers).to eq([]) }
|
142
|
+
end
|
143
|
+
|
144
|
+
context do
|
145
|
+
let(:raw_response) { {'hits' => {'hits' => []}} }
|
146
|
+
specify { expect(subject.wrappers).to eq([]) }
|
147
|
+
end
|
148
|
+
|
149
|
+
context do
|
150
|
+
let(:raw_response) do
|
151
|
+
{'hits' => {'hits' => [
|
152
|
+
{'_index' => 'places',
|
153
|
+
'_type' => 'city',
|
154
|
+
'_id' => '1',
|
155
|
+
'_score' => 1.3,
|
156
|
+
'_source' => {'id' => 2, 'rating' => 0}}
|
157
|
+
]}}
|
158
|
+
end
|
159
|
+
specify { expect(subject.wrappers.first).to be_a(PlacesIndex::City) }
|
160
|
+
specify { expect(subject.wrappers.first.id).to eq(2) }
|
161
|
+
specify { expect(subject.wrappers.first.rating).to eq(0) }
|
162
|
+
specify { expect(subject.wrappers.first._score).to eq(1.3) }
|
163
|
+
specify { expect(subject.wrappers.first._explanation).to be_nil }
|
164
|
+
end
|
165
|
+
|
166
|
+
context do
|
167
|
+
let(:raw_response) do
|
168
|
+
{'hits' => {'hits' => [
|
169
|
+
{'_index' => 'places',
|
170
|
+
'_type' => 'country',
|
171
|
+
'_id' => '2',
|
172
|
+
'_score' => 1.2,
|
173
|
+
'_explanation' => {foo: 'bar'}}
|
174
|
+
]}}
|
175
|
+
end
|
176
|
+
specify { expect(subject.wrappers.first).to be_a(PlacesIndex::Country) }
|
177
|
+
specify { expect(subject.wrappers.first.id).to eq('2') }
|
178
|
+
specify { expect(subject.wrappers.first.rating).to be_nil }
|
179
|
+
specify { expect(subject.wrappers.first._score).to eq(1.2) }
|
180
|
+
specify { expect(subject.wrappers.first._explanation).to eq(foo: 'bar') }
|
181
|
+
end
|
182
|
+
end
|
183
|
+
|
184
|
+
describe '#objects' do
|
185
|
+
specify { expect(subject.objects).to eq([*cities, *countries]) }
|
186
|
+
end
|
187
|
+
|
188
|
+
describe '#object_hash' do
|
189
|
+
specify { expect(subject.object_hash.keys).to eq(subject.wrappers) }
|
190
|
+
specify { expect(subject.object_hash.values).to eq(subject.objects) }
|
191
|
+
end
|
192
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chewy::Search::Scrolling, :orm do
|
4
|
+
before { Chewy.massacre }
|
5
|
+
|
6
|
+
before do
|
7
|
+
stub_model(:city)
|
8
|
+
stub_model(:country)
|
9
|
+
|
10
|
+
stub_index(:places) do
|
11
|
+
define_type City do
|
12
|
+
field :name
|
13
|
+
field :rating, type: 'integer'
|
14
|
+
end
|
15
|
+
|
16
|
+
define_type Country do
|
17
|
+
field :name
|
18
|
+
field :rating, type: 'integer'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
let(:request) { Chewy::Search::Request.new(PlacesIndex).order(:rating) }
|
24
|
+
|
25
|
+
specify { expect(request.scroll_batches.to_a).to eq([]) }
|
26
|
+
|
27
|
+
context do
|
28
|
+
before { PlacesIndex.import!(cities: cities, countries: countries) }
|
29
|
+
|
30
|
+
let(:cities) { Array.new(2) { |i| City.create!(rating: i, name: "city #{i}") } }
|
31
|
+
let(:countries) { Array.new(3) { |i| Country.create!(rating: i + 2, name: "country #{i}") } }
|
32
|
+
|
33
|
+
describe '#scroll_batches' do
|
34
|
+
context do
|
35
|
+
before { expect(Chewy.client).to receive(:scroll).twice.and_call_original }
|
36
|
+
specify do
|
37
|
+
expect(request.scroll_batches(batch_size: 2).map do |batch|
|
38
|
+
batch.map { |hit| hit['_source']['rating'] }
|
39
|
+
end).to eq([[0, 1], [2, 3], [4]])
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context do
|
44
|
+
before { expect(Chewy.client).to receive(:scroll).once.and_call_original }
|
45
|
+
specify do
|
46
|
+
expect(request.scroll_batches(batch_size: 3).map do |batch|
|
47
|
+
batch.map { |hit| hit['_source']['rating'] }
|
48
|
+
end).to eq([[0, 1, 2], [3, 4]])
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context do
|
53
|
+
before { expect(Chewy.client).to receive(:scroll).once.and_call_original }
|
54
|
+
it 'respects limit' do
|
55
|
+
expect(request.limit(4).scroll_batches(batch_size: 3).map do |batch|
|
56
|
+
batch.map { |hit| hit['_source']['rating'] }
|
57
|
+
end).to eq([[0, 1, 2], [3]])
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context do
|
62
|
+
before { expect(Chewy.client).not_to receive(:scroll) }
|
63
|
+
it 'respects limit and terminate_after' do
|
64
|
+
expect(request.terminate_after(2).limit(4).scroll_batches(batch_size: 3).map do |batch|
|
65
|
+
batch.map { |hit| hit['_source']['rating'] }
|
66
|
+
end).to eq([[0, 1]])
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context do
|
71
|
+
before { expect(Chewy.client).not_to receive(:scroll) }
|
72
|
+
it 'respects limit' do
|
73
|
+
expect(request.limit(3).scroll_batches(batch_size: 3).map do |batch|
|
74
|
+
batch.map { |hit| hit['_source']['rating'] }
|
75
|
+
end).to eq([[0, 1, 2]])
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context do
|
80
|
+
before { expect(Chewy.client).not_to receive(:scroll) }
|
81
|
+
it 'respects limit' do
|
82
|
+
expect(request.limit(2).scroll_batches(batch_size: 3).map do |batch|
|
83
|
+
batch.map { |hit| hit['_source']['rating'] }
|
84
|
+
end).to eq([[0, 1]])
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
context do
|
89
|
+
before { expect(Chewy.client).not_to receive(:scroll) }
|
90
|
+
specify do
|
91
|
+
expect(request.scroll_batches(batch_size: 5).map do |batch|
|
92
|
+
batch.map { |hit| hit['_source']['rating'] }
|
93
|
+
end).to eq([[0, 1, 2, 3, 4]])
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context do
|
98
|
+
before { expect(Chewy.client).not_to receive(:scroll) }
|
99
|
+
specify do
|
100
|
+
expect(request.scroll_batches(batch_size: 10).map do |batch|
|
101
|
+
batch.map { |hit| hit['_source']['rating'] }
|
102
|
+
end).to eq([[0, 1, 2, 3, 4]])
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
context 'instrumentation' do
|
107
|
+
specify do
|
108
|
+
outer_payload = []
|
109
|
+
ActiveSupport::Notifications.subscribe('search_query.chewy') do |_name, _start, _finish, _id, payload|
|
110
|
+
outer_payload << payload
|
111
|
+
end
|
112
|
+
request.scroll_batches(batch_size: 3).to_a
|
113
|
+
|
114
|
+
expect(outer_payload).to match_array([
|
115
|
+
hash_including(
|
116
|
+
index: PlacesIndex,
|
117
|
+
indexes: [PlacesIndex],
|
118
|
+
request: {index: ['places'], type: %w[city country], body: {sort: ['rating']}, size: 3, scroll: '1m'},
|
119
|
+
type: [PlacesIndex::City, PlacesIndex::Country],
|
120
|
+
types: [PlacesIndex::City, PlacesIndex::Country]
|
121
|
+
),
|
122
|
+
hash_including(
|
123
|
+
index: PlacesIndex,
|
124
|
+
indexes: [PlacesIndex],
|
125
|
+
request: {scroll: '1m', scroll_id: an_instance_of(String)},
|
126
|
+
type: [PlacesIndex::City, PlacesIndex::Country],
|
127
|
+
types: [PlacesIndex::City, PlacesIndex::Country]
|
128
|
+
)
|
129
|
+
])
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe '#scroll_hits' do
|
135
|
+
before { expect(Chewy.client).to receive(:scroll).twice.and_call_original }
|
136
|
+
specify do
|
137
|
+
expect(request.scroll_hits(batch_size: 2).map do |hit|
|
138
|
+
hit['_source']['rating']
|
139
|
+
end).to eq([0, 1, 2, 3, 4])
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
describe '#scroll_wrappers' do
|
144
|
+
before { expect(Chewy.client).to receive(:scroll).twice.and_call_original }
|
145
|
+
|
146
|
+
specify do
|
147
|
+
expect(request.scroll_wrappers(batch_size: 2).map(&:rating))
|
148
|
+
.to eq([0, 1, 2, 3, 4])
|
149
|
+
end
|
150
|
+
specify do
|
151
|
+
expect(request.scroll_wrappers(batch_size: 2).map(&:class).uniq)
|
152
|
+
.to eq([PlacesIndex::City, PlacesIndex::Country])
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
describe '#scroll_objects' do
|
157
|
+
before { expect(Chewy.client).to receive(:scroll).twice.and_call_original }
|
158
|
+
|
159
|
+
specify do
|
160
|
+
expect(request.scroll_objects(batch_size: 2).map(&:rating))
|
161
|
+
.to eq([0, 1, 2, 3, 4])
|
162
|
+
end
|
163
|
+
specify do
|
164
|
+
expect(request.scroll_objects(batch_size: 2).map(&:class).uniq)
|
165
|
+
.to eq([City, Country])
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
data/spec/chewy/search_spec.rb
CHANGED
@@ -13,8 +13,15 @@ describe Chewy::Search do
|
|
13
13
|
let(:product) { ProductsIndex::Product }
|
14
14
|
|
15
15
|
describe '.all' do
|
16
|
-
specify { expect(ProductsIndex.all).to be_a(Chewy::
|
17
|
-
specify { expect(product.all).to be_a(Chewy::
|
16
|
+
specify { expect(ProductsIndex.all).to be_a(Chewy::Search::Request) }
|
17
|
+
specify { expect(product.all).to be_a(Chewy::Search::Request) }
|
18
|
+
|
19
|
+
context do
|
20
|
+
before { allow(Chewy).to receive_messages(search_class: Chewy::Search::Request) }
|
21
|
+
|
22
|
+
specify { expect(ProductsIndex.all).to be_a(Chewy::Search::Request) }
|
23
|
+
specify { expect(product.all).to be_a(Chewy::Search::Request) }
|
24
|
+
end
|
18
25
|
end
|
19
26
|
|
20
27
|
describe '.search_string' do
|
@@ -31,7 +38,7 @@ describe Chewy::Search do
|
|
31
38
|
end
|
32
39
|
|
33
40
|
specify do
|
34
|
-
expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'], type: []))
|
41
|
+
expect(ProductsIndex.client).to receive(:search).with(hash_including(index: ['products'], type: %w[product product2]))
|
35
42
|
ProductsIndex.search_string('hello')
|
36
43
|
end
|
37
44
|
|
@@ -48,16 +55,16 @@ describe Chewy::Search do
|
|
48
55
|
|
49
56
|
stub_index(:places) do
|
50
57
|
def self.by_rating(value)
|
51
|
-
filter { rating
|
58
|
+
filter { match rating: value }
|
52
59
|
end
|
53
60
|
|
54
61
|
def self.by_name(index)
|
55
|
-
filter { name
|
62
|
+
filter { match name: "Name#{index}" }
|
56
63
|
end
|
57
64
|
|
58
65
|
define_type City do
|
59
66
|
def self.by_rating
|
60
|
-
filter { rating
|
67
|
+
filter { match rating: yield }
|
61
68
|
end
|
62
69
|
|
63
70
|
field :name, index: 'not_analyzed'
|
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chewy::Stash::Journal, :orm do
|
4
|
+
def fetch_deleted_number(response)
|
5
|
+
response['deleted'] || response['_indices']['_all']['deleted']
|
6
|
+
end
|
7
|
+
|
8
|
+
before { Chewy.massacre }
|
9
|
+
|
10
|
+
before do
|
11
|
+
stub_model(:city)
|
12
|
+
stub_index(:places) do
|
13
|
+
define_type City
|
14
|
+
define_type :country
|
15
|
+
end
|
16
|
+
stub_index(:users) { define_type :user }
|
17
|
+
stub_index(:borogoves)
|
18
|
+
end
|
19
|
+
|
20
|
+
before { Timecop.freeze }
|
21
|
+
after { Timecop.return }
|
22
|
+
|
23
|
+
before do
|
24
|
+
PlacesIndex::City.import!(City.new(id: 1, name: 'City'), journal: true)
|
25
|
+
Timecop.travel(Time.now + 1.minute) do
|
26
|
+
PlacesIndex::Country.import!([id: 2, name: 'Country'], journal: true)
|
27
|
+
end
|
28
|
+
Timecop.travel(Time.now + 2.minutes) do
|
29
|
+
UsersIndex::User.import!([id: 3, name: 'User'], journal: true)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '.entries' do
|
34
|
+
specify do
|
35
|
+
expect(described_class.entries(Time.now - 30.seconds).map(&:references))
|
36
|
+
.to contain_exactly([1], [{'id' => 2, 'name' => 'Country'}], [{'id' => 3, 'name' => 'User'}])
|
37
|
+
end
|
38
|
+
specify do
|
39
|
+
expect(described_class.entries(Time.now + 30.seconds).map(&:references))
|
40
|
+
.to contain_exactly([{'id' => 2, 'name' => 'Country'}], [{'id' => 3, 'name' => 'User'}])
|
41
|
+
end
|
42
|
+
specify do
|
43
|
+
expect(described_class.entries(Time.now + 90.seconds).map(&:references))
|
44
|
+
.to contain_exactly([{'id' => 3, 'name' => 'User'}])
|
45
|
+
end
|
46
|
+
|
47
|
+
specify do
|
48
|
+
expect(described_class.entries(Time.now - 30.seconds, only: UsersIndex).map(&:references))
|
49
|
+
.to contain_exactly([{'id' => 3, 'name' => 'User'}])
|
50
|
+
end
|
51
|
+
specify do
|
52
|
+
expect(described_class.entries(Time.now - 30.seconds, only: [PlacesIndex::City, UsersIndex]).map(&:references))
|
53
|
+
.to contain_exactly([1], [{'id' => 3, 'name' => 'User'}])
|
54
|
+
end
|
55
|
+
specify do
|
56
|
+
expect(described_class.entries(Time.now + 30.seconds, only: [PlacesIndex::City, UsersIndex]).map(&:references))
|
57
|
+
.to contain_exactly([{'id' => 3, 'name' => 'User'}])
|
58
|
+
end
|
59
|
+
specify do
|
60
|
+
expect(described_class.entries(Time.now + 30.seconds, only: [BorogovesIndex])).to eq([])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '.clean' do
|
65
|
+
specify { expect(fetch_deleted_number(described_class.clean)).to eq(3) }
|
66
|
+
specify { expect(fetch_deleted_number(described_class.clean(Time.now - 30.seconds))).to eq(0) }
|
67
|
+
specify { expect(fetch_deleted_number(described_class.clean(Time.now + 30.seconds))).to eq(1) }
|
68
|
+
specify { expect(fetch_deleted_number(described_class.clean(Time.now + 90.seconds))).to eq(2) }
|
69
|
+
specify { expect(fetch_deleted_number(described_class.clean(only: BorogovesIndex))).to eq(0) }
|
70
|
+
specify { expect(fetch_deleted_number(described_class.clean(only: UsersIndex))).to eq(1) }
|
71
|
+
specify { expect(fetch_deleted_number(described_class.clean(only: [PlacesIndex::City, UsersIndex]))).to eq(2) }
|
72
|
+
|
73
|
+
specify { expect(fetch_deleted_number(described_class.clean(Time.now + 30.seconds, only: PlacesIndex::Country))).to eq(0) }
|
74
|
+
specify { expect(fetch_deleted_number(described_class.clean(Time.now + 30.seconds, only: PlacesIndex::City))).to eq(1) }
|
75
|
+
end
|
76
|
+
|
77
|
+
describe '.for' do
|
78
|
+
specify { expect(described_class.for(UsersIndex).map(&:index_name)).to eq(['users']) }
|
79
|
+
specify { expect(described_class.for(PlacesIndex).map(&:type_name)).to contain_exactly('city', 'country') }
|
80
|
+
specify { expect(described_class.for(PlacesIndex::City, UsersIndex).map(&:index_name)).to contain_exactly('places', 'users') }
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#type' do
|
84
|
+
let(:index_name) { 'users' }
|
85
|
+
let(:type_name) { 'city' }
|
86
|
+
subject { described_class.new('index_name' => index_name, 'type_name' => type_name).type }
|
87
|
+
|
88
|
+
specify { expect { subject }.to raise_error(Chewy::UnderivableType) }
|
89
|
+
|
90
|
+
context do
|
91
|
+
let(:index_name) { 'places' }
|
92
|
+
it { is_expected.to eq(PlacesIndex::City) }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|