Skip to content

Commit 38730fb

Browse files
committed
Deprecate flattened access on multi search results
Update #each to mimmick #each_result, not #each_hit
1 parent 3734a11 commit 38730fb

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

lib/meilisearch/rails/multi_search/result.rb

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,41 @@ def initialize(searches, raw_results)
2323
include Enumerable
2424

2525
def each_hit(&block)
26-
@results.each do |_index_target, results|
26+
MeiliSearch::Rails.logger.warn(
27+
<<~DEPRECATION
28+
[meilisearch-rails] Flattening multi search results is deprecated.
29+
If you do not want the results to be grouped, please use federated search instead.
30+
DEPRECATION
31+
)
32+
33+
@results.each_value do |results|
2734
results.each(&block)
2835
end
2936
end
30-
alias each each_hit
37+
38+
def each(&block)
39+
MeiliSearch::Rails.logger.info(
40+
<<~INFO
41+
[meilisearch-rails] #each on a multi search now iterates through grouped results.
42+
If you do not want the results to be grouped, please use federated search instead.
43+
To quickly go back to the old deprecated behavior, use `#each_hit`.
44+
INFO
45+
)
46+
47+
@results.each(&block)
48+
end
3149

3250
def each_result(&block)
3351
@results.each(&block)
3452
end
3553

3654
def to_a
55+
MeiliSearch::Rails.logger.warn(
56+
<<~DEPRECATION
57+
[meilisearch-rails] Flattening multi search results is deprecated.
58+
If you do not want the results to be grouped, please use federated search instead.
59+
DEPRECATION
60+
)
3761
@results.values.flatten(1)
3862
end
3963
alias to_ary to_a

spec/multi_search/result_spec.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,57 @@
6060
)
6161
end
6262

63+
describe '#each' do
64+
let(:logger) { instance_double('Logger', info: nil) }
65+
66+
before do
67+
allow(MeiliSearch::Rails).to receive(:logger).and_return(logger)
68+
end
69+
70+
it 'has the same behavior as #each_result' do
71+
expect(result.each).to be_an(Enumerator)
72+
expect(result.each).to contain_exactly(
73+
['books_index', contain_exactly(
74+
a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs')
75+
)],
76+
['products_index', contain_exactly(
77+
a_hash_including('name' => 'palm pixi plus')
78+
)],
79+
['color_index', contain_exactly(
80+
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
81+
a_hash_including('name' => 'black', 'short_name' => 'bla')
82+
)]
83+
)
84+
end
85+
86+
it 'warns about changed behavior' do
87+
result.each(&:to_s)
88+
89+
expect(logger).to have_received(:info).with(a_string_including('#each on a multi search now iterates through grouped results.'))
90+
end
91+
end
92+
93+
describe '#each_hit' do
94+
let(:logger) { instance_double('Logger', warn: nil) }
95+
96+
before do
97+
allow(MeiliSearch::Rails).to receive(:logger).and_return(logger)
98+
end
99+
100+
it 'warns about deprecation' do
101+
result.each_hit(&:to_s)
102+
103+
expect(logger).to have_received(:warn).with(a_string_including('Flattening multi search'))
104+
end
105+
end
106+
63107
describe '#to_a' do
108+
let(:logger) { instance_double('Logger', warn: nil) }
109+
110+
before do
111+
allow(MeiliSearch::Rails).to receive(:logger).and_return(logger)
112+
end
113+
64114
it 'returns the hits' do
65115
expect(result.to_a).to contain_exactly(
66116
a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs'),
@@ -73,6 +123,12 @@
73123
it 'aliases as #to_ary' do
74124
expect(result.method(:to_ary).original_name).to eq :to_a
75125
end
126+
127+
it 'warns about deprecation' do
128+
result.to_a
129+
130+
expect(logger).to have_received(:warn).with(a_string_including('Flattening multi search'))
131+
end
76132
end
77133

78134
describe '#to_h' do

0 commit comments

Comments
 (0)