Skip to content

Commit ba781fb

Browse files
meili-bors[bot]meili-botbrunoocasali
authored
Merge #389
389: Changes related to the next Meilisearch release (v0.30.0) r=brunoocasali a=meili-bot Related to this issue: meilisearch/integration-guides#221 This PR: - gathers the changes related to the next Meilisearch release (v0.30.0) so that this package is ready when the official release is out. - should pass the tests against the [latest pre-release of Meilisearch](https://github.com/meilisearch/meilisearch/releases). - might eventually contain test failures until the Meilisearch v0.30.0 is out. ⚠️ This PR should NOT be merged until the next release of Meilisearch (v0.30.0) is out. _This PR is auto-generated for the [pre-release week](https://github.com/meilisearch/integration-guides/blob/main/resources/pre-release-week.md) purpose._ Co-authored-by: meili-bot <[email protected]> Co-authored-by: Bruno Casali <[email protected]>
2 parents 54b5c70 + 45589bf commit ba781fb

File tree

11 files changed

+188
-16
lines changed

11 files changed

+188
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ JSON output:
200200

201201
## 🤖 Compatibility with Meilisearch
202202

203-
This package only guarantees the compatibility with the [version v0.29.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.29.0).
203+
This package only guarantees the compatibility with the [version v0.30.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.30.0).
204204

205205
## 💡 Learn more
206206

lib/meilisearch/client.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ def raw_indexes(options = {})
1212
http_get('/indexes', body)
1313
end
1414

15+
def swap_indexes(*options)
16+
mapped_array = options.map { |arr| { indexes: arr } }
17+
18+
http_post '/swap-indexes', mapped_array
19+
end
20+
1521
def indexes(options = {})
1622
response = raw_indexes(options)
1723

@@ -116,6 +122,14 @@ def create_dump
116122

117123
### TASKS
118124

125+
def cancel_tasks(options = {})
126+
task_endpoint.cancel_tasks(options)
127+
end
128+
129+
def delete_tasks(options = {})
130+
task_endpoint.delete_tasks(options)
131+
end
132+
119133
def tasks(options = {})
120134
task_endpoint.task_list(options)
121135
end

lib/meilisearch/http_request.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,12 @@ def http_patch(relative_path = '', body = nil, query_params = nil)
7373
)
7474
end
7575

76-
def http_delete(relative_path = '')
76+
def http_delete(relative_path = '', query_params = nil)
7777
send_request(
7878
proc { |path, config| self.class.delete(path, config) },
7979
relative_path,
8080
config: {
81+
query_params: query_params,
8182
headers: remove_headers(@headers.dup, 'Content-Type'),
8283
options: @options
8384
}

lib/meilisearch/index.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ def document(document_id, fields: nil)
6464
alias get_one_document document
6565

6666
def documents(options = {})
67-
body = Utils.transform_attributes(options.transform_keys(&:to_sym).slice(:limit, :offset, :fields))
68-
body = body.transform_values { |v| v.respond_to?(:join) ? v.join(',') : v }
69-
70-
http_get "/indexes/#{@uid}/documents", body
67+
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
7168
end
7269
alias get_documents documents
7370

@@ -194,7 +191,8 @@ def search(query, options = {})
194191
parsed_options = Utils.transform_attributes({ q: query.to_s }.merge(options.compact))
195192

196193
response = http_post "/indexes/#{@uid}/search", parsed_options
197-
response['nbHits'] ||= response['estimatedTotalHits']
194+
195+
response['nbHits'] ||= response['estimatedTotalHits'] unless response.key?('totalPages')
198196

199197
response
200198
end

lib/meilisearch/task.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,37 @@
55

66
module MeiliSearch
77
class Task < HTTPRequest
8-
ALLOWED_PARAMS = [:limit, :from, :index_uid, :type, :status].freeze
8+
ALLOWED_PARAMS = [
9+
:limit, :from, :index_uids, :types, :statuses, :uids, :canceled_by,
10+
:before_enqueued_at, :after_enqueued_at, :before_started_at, :after_started_at,
11+
:before_finished_at, :after_finished_at
12+
].freeze
13+
ALLOWED_CANCELATION_PARAMS = (ALLOWED_PARAMS - [:limit, :from]).freeze
914

1015
def task_list(options = {})
11-
body = Utils.transform_attributes(options.transform_keys(&:to_sym).slice(*ALLOWED_PARAMS))
12-
body = body.transform_values { |v| v.respond_to?(:join) ? v.join(',') : v }
13-
14-
http_get '/tasks/', body
16+
http_get '/tasks/', Utils.parse_query(options, ALLOWED_PARAMS)
1517
end
1618

1719
def task(task_uid)
1820
http_get "/tasks/#{task_uid}"
1921
end
2022

2123
def index_tasks(index_uid)
22-
http_get '/tasks', { indexUid: [index_uid].flatten.join(',') }
24+
http_get '/tasks', { indexUids: [index_uid].flatten.join(',') }
2325
end
2426

2527
def index_task(task_uid)
2628
http_get "/tasks/#{task_uid}"
2729
end
2830

31+
def cancel_tasks(options)
32+
http_post '/tasks/cancel', nil, Utils.parse_query(options, ALLOWED_CANCELATION_PARAMS)
33+
end
34+
35+
def delete_tasks(options)
36+
http_delete '/tasks', Utils.parse_query(options, ALLOWED_CANCELATION_PARAMS)
37+
end
38+
2939
def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
3040
Timeout.timeout(timeout_in_ms.to_f / 1000) do
3141
loop do

lib/meilisearch/utils.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ def self.parse(body)
2323
end
2424
end
2525

26+
def self.parse_query(original_options, allowed_params = [])
27+
only_allowed_params = original_options.transform_keys(&:to_sym).slice(*allowed_params)
28+
29+
Utils.transform_attributes(only_allowed_params).then do |body|
30+
body.transform_values do |v|
31+
v.respond_to?(:join) ? v.join(',') : v.to_s
32+
end
33+
end
34+
end
35+
2636
private_class_method :parse
2737
end
2838
end

spec/meilisearch/client/indexes_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,15 @@
237237
end
238238
end
239239
end
240+
241+
describe '#swap_indexes' do
242+
it 'swaps two indexes' do
243+
task = client.swap_indexes(['indexA', 'indexB'], ['indexC', 'indexD'])
244+
task = client.wait_for_task(task['taskUid'])
245+
246+
expect(task['type']).to eq('indexSwap')
247+
expect(task['details']['swaps']).to eq([{ 'indexes' => ['indexA', 'indexB'] },
248+
{ 'indexes' => ['indexC', 'indexD'] }])
249+
end
250+
end
240251
end

spec/meilisearch/client/tasks_spec.rb

Lines changed: 84 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,35 @@
5050
expect(tasks['next']).to be_a(Integer)
5151
end
5252

53-
it 'filters tasks with index_uid/type/status' do
54-
tasks = client.tasks(index_uid: ['a-cool-index-name'])
53+
it 'filters tasks with index_uids/types/statuses' do
54+
tasks = client.tasks(index_uids: ['a-cool-index-name'])
5555

5656
expect(tasks['results'].count).to eq(0)
5757

58-
tasks = client.tasks(index_uid: ['books'], type: ['documentAdditionOrUpdate'], status: ['succeeded'])
58+
tasks = client.tasks(index_uids: ['books'], types: ['documentAdditionOrUpdate'], statuses: ['succeeded'])
5959

6060
expect(tasks['results'].count).to be > 1
6161
end
6262

63+
it 'ensures supports to all available filters' do
64+
allow(MeiliSearch::Utils).to receive(:transform_attributes).and_call_original
65+
66+
client.tasks(
67+
canceled_by: [1, 2], uids: [2], foo: 'bar',
68+
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
69+
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
70+
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
71+
)
72+
73+
expect(MeiliSearch::Utils).to have_received(:transform_attributes)
74+
.with(
75+
canceled_by: [1, 2], uids: [2],
76+
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
77+
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
78+
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
79+
)
80+
end
81+
6382
describe '#index.wait_for_task' do
6483
it 'waits for task with default values' do
6584
task = index.add_documents(documents)
@@ -133,4 +152,66 @@
133152
end.to raise_error(Timeout::Error)
134153
end
135154
end
155+
156+
describe '#client.cancel_tasks' do
157+
it 'ensures supports to all available filters' do
158+
allow(MeiliSearch::Utils).to receive(:transform_attributes).and_call_original
159+
160+
client.cancel_tasks(
161+
canceled_by: [1, 2], uids: [2], foo: 'bar',
162+
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
163+
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
164+
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
165+
)
166+
167+
expect(MeiliSearch::Utils).to have_received(:transform_attributes)
168+
.with(
169+
canceled_by: [1, 2], uids: [2],
170+
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
171+
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
172+
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
173+
)
174+
end
175+
176+
it 'has fields in the details field' do
177+
task = client.cancel_tasks(uids: [1, 2])
178+
task = client.wait_for_task(task['taskUid'])
179+
180+
expect(task['details']['originalFilter']).to eq('?uids=1%2C2')
181+
expect(task['details']['matchedTasks']).to be_a(Integer)
182+
expect(task['details']['canceledTasks']).to be_a(Integer)
183+
end
184+
end
185+
186+
describe '#client.delete_tasks' do
187+
it 'ensures supports to all available filters' do
188+
date = DateTime.new(2022, 1, 20)
189+
190+
allow(MeiliSearch::Utils).to receive(:transform_attributes).and_call_original
191+
192+
client.delete_tasks(
193+
canceled_by: [1, 2], uids: [2], foo: 'bar',
194+
before_enqueued_at: date, after_enqueued_at: date,
195+
before_started_at: date, after_started_at: date,
196+
before_finished_at: date, after_finished_at: date
197+
)
198+
199+
expect(MeiliSearch::Utils).to have_received(:transform_attributes)
200+
.with(
201+
canceled_by: [1, 2], uids: [2],
202+
before_enqueued_at: date, after_enqueued_at: date,
203+
before_started_at: date, after_started_at: date,
204+
before_finished_at: date, after_finished_at: date
205+
)
206+
end
207+
208+
it 'has fields in the details field' do
209+
task = client.delete_tasks(uids: [1, 2])
210+
task = client.wait_for_task(task['taskUid'])
211+
212+
expect(task['details']['originalFilter']).to eq('?uids=1%2C2')
213+
expect(task['details']['matchedTasks']).to be_a(Integer)
214+
expect(task['details']['deletedTasks']).to be_a(Integer)
215+
end
216+
end
136217
end

spec/meilisearch/index/search/q_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,18 @@
6969
expect(response['hits'].first['objectId']).to eq(4)
7070
expect(response['hits'].first).not_to have_key('_formatted')
7171
end
72+
73+
context 'with finite pagination params' do
74+
it 'responds with specialized fields' do
75+
response = index.search('coco', { page: 2, hits_per_page: 2 })
76+
77+
expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS)
78+
end
79+
80+
it 'responds with specialized fields' do
81+
response = index.search('coco', { page: 2, hitsPerPage: 2 })
82+
83+
expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS)
84+
end
85+
end
7286
end

spec/meilisearch/utils_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe MeiliSearch::Utils do
4+
describe '.parse_query' do
5+
it 'transforms arrays into strings' do
6+
data = described_class.parse_query({ array: [1, 2, 3], other: 'string' }, [:array, :other])
7+
8+
expect(data).to eq({ 'array' => '1,2,3', 'other' => 'string' })
9+
end
10+
11+
it 'cleans list based on another list' do
12+
data = described_class.parse_query({ array: [1, 2, 3], ignore: 'string' }, [:array])
13+
14+
expect(data).to eq({ 'array' => '1,2,3' })
15+
end
16+
17+
it 'transforms dates into strings' do
18+
data = described_class.parse_query({ date: DateTime.new(2012, 12, 21, 19, 5) }, [:date])
19+
20+
expect(data).to eq({ 'date' => '2012-12-21T19:05:00+00:00' })
21+
end
22+
end
23+
end

0 commit comments

Comments
 (0)