Skip to content

Changes related to the next Meilisearch release (v0.30.0) #389

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
7d5f4c9
Update README.md
meili-bot Nov 7, 2022
f7e0f5d
Fix tasks specs with v0.30rc1
brunoocasali Nov 14, 2022
05e1374
Add new filters to /tasks endpoints
brunoocasali Nov 14, 2022
8c088eb
Extract to Utils a method to parse queries
brunoocasali Nov 14, 2022
4769d56
Use Utils.parse_query to create query strings
brunoocasali Nov 14, 2022
c6d4148
Create client#cancel_tasks method
brunoocasali Nov 14, 2022
f0d1154
Add Client#swap_indexes method
brunoocasali Nov 14, 2022
ec79b53
Add unit tests to Client#cancel_tasks
brunoocasali Nov 14, 2022
982f047
Fix test description after code review
brunoocasali Nov 15, 2022
c71882c
Merge pull request #391 from meilisearch/bump-meilisearch-v0.30.0-add…
brunoocasali Nov 16, 2022
468c5b2
Merge pull request #392 from meilisearch/bump-meilisearch-v0.30.0-add…
brunoocasali Nov 17, 2022
fafaa20
Merge pull request #393 from meilisearch/bump-meilisearch-v0.30.0-add…
brunoocasali Nov 17, 2022
6f920dc
Add Client#delete_tasks method
brunoocasali Nov 14, 2022
2dd1681
Merge pull request #394 from meilisearch/bump-meilisearch-v0.30.0-add…
brunoocasali Nov 17, 2022
29e8607
Add support to finite pagination
brunoocasali Nov 16, 2022
390a7f6
Merge pull request #397 from meilisearch/bump-meilisearch-v0.30.0-add…
brunoocasali Nov 17, 2022
42dc4ff
Add support to receive also DateTime instances as parameters
brunoocasali Nov 26, 2022
7513552
Merge pull request #402 from meilisearch/bump-meilisearch-v0.30.0-imp…
brunoocasali Nov 26, 2022
9a027dd
Fix rubocop offenses after update
brunoocasali Nov 26, 2022
2b1ddde
Merge pull request #403 from meilisearch/bump-meilisearch-v0.30.0-fix…
brunoocasali Nov 26, 2022
45589bf
Merge branch 'main' into bump-meilisearch-v0.30.0
brunoocasali Nov 26, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ JSON output:

## 🤖 Compatibility with Meilisearch

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

## 💡 Learn more

Expand Down
14 changes: 14 additions & 0 deletions lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ def raw_indexes(options = {})
http_get('/indexes', body)
end

def swap_indexes(*options)
mapped_array = options.map { |arr| { indexes: arr } }

http_post '/swap-indexes', mapped_array
end

def indexes(options = {})
response = raw_indexes(options)

Expand Down Expand Up @@ -116,6 +122,14 @@ def create_dump

### TASKS

def cancel_tasks(options = {})
task_endpoint.cancel_tasks(options)
end

def delete_tasks(options = {})
task_endpoint.delete_tasks(options)
end

def tasks(options = {})
task_endpoint.task_list(options)
end
Expand Down
3 changes: 2 additions & 1 deletion lib/meilisearch/http_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ def http_patch(relative_path = '', body = nil, query_params = nil)
)
end

def http_delete(relative_path = '')
def http_delete(relative_path = '', query_params = nil)
send_request(
proc { |path, config| self.class.delete(path, config) },
relative_path,
config: {
query_params: query_params,
headers: remove_headers(@headers.dup, 'Content-Type'),
options: @options
}
Expand Down
8 changes: 3 additions & 5 deletions lib/meilisearch/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ def document(document_id, fields: nil)
alias get_one_document document

def documents(options = {})
body = Utils.transform_attributes(options.transform_keys(&:to_sym).slice(:limit, :offset, :fields))
body = body.transform_values { |v| v.respond_to?(:join) ? v.join(',') : v }

http_get "/indexes/#{@uid}/documents", body
http_get "/indexes/#{@uid}/documents", Utils.parse_query(options, [:limit, :offset, :fields])
end
alias get_documents documents

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

response = http_post "/indexes/#{@uid}/search", parsed_options
response['nbHits'] ||= response['estimatedTotalHits']

response['nbHits'] ||= response['estimatedTotalHits'] unless response.key?('totalPages')

response
end
Expand Down
22 changes: 16 additions & 6 deletions lib/meilisearch/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,37 @@

module MeiliSearch
class Task < HTTPRequest
ALLOWED_PARAMS = [:limit, :from, :index_uid, :type, :status].freeze
ALLOWED_PARAMS = [
:limit, :from, :index_uids, :types, :statuses, :uids, :canceled_by,
:before_enqueued_at, :after_enqueued_at, :before_started_at, :after_started_at,
:before_finished_at, :after_finished_at
].freeze
ALLOWED_CANCELATION_PARAMS = (ALLOWED_PARAMS - [:limit, :from]).freeze

def task_list(options = {})
body = Utils.transform_attributes(options.transform_keys(&:to_sym).slice(*ALLOWED_PARAMS))
body = body.transform_values { |v| v.respond_to?(:join) ? v.join(',') : v }

http_get '/tasks/', body
http_get '/tasks/', Utils.parse_query(options, ALLOWED_PARAMS)
end

def task(task_uid)
http_get "/tasks/#{task_uid}"
end

def index_tasks(index_uid)
http_get '/tasks', { indexUid: [index_uid].flatten.join(',') }
http_get '/tasks', { indexUids: [index_uid].flatten.join(',') }
end

def index_task(task_uid)
http_get "/tasks/#{task_uid}"
end

def cancel_tasks(options)
http_post '/tasks/cancel', nil, Utils.parse_query(options, ALLOWED_CANCELATION_PARAMS)
end

def delete_tasks(options)
http_delete '/tasks', Utils.parse_query(options, ALLOWED_CANCELATION_PARAMS)
end

def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
Timeout.timeout(timeout_in_ms.to_f / 1000) do
loop do
Expand Down
10 changes: 10 additions & 0 deletions lib/meilisearch/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ def self.parse(body)
end
end

def self.parse_query(original_options, allowed_params = [])
only_allowed_params = original_options.transform_keys(&:to_sym).slice(*allowed_params)

Utils.transform_attributes(only_allowed_params).then do |body|
body.transform_values do |v|
v.respond_to?(:join) ? v.join(',') : v.to_s
end
end
end

private_class_method :parse
end
end
11 changes: 11 additions & 0 deletions spec/meilisearch/client/indexes_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,15 @@
end
end
end

describe '#swap_indexes' do
it 'swaps two indexes' do
task = client.swap_indexes(['indexA', 'indexB'], ['indexC', 'indexD'])
task = client.wait_for_task(task['taskUid'])

expect(task['type']).to eq('indexSwap')
expect(task['details']['swaps']).to eq([{ 'indexes' => ['indexA', 'indexB'] },
{ 'indexes' => ['indexC', 'indexD'] }])
end
end
end
87 changes: 84 additions & 3 deletions spec/meilisearch/client/tasks_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,35 @@
expect(tasks['next']).to be_a(Integer)
end

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

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

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

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

it 'ensures supports to all available filters' do
allow(MeiliSearch::Utils).to receive(:transform_attributes).and_call_original

client.tasks(
canceled_by: [1, 2], uids: [2], foo: 'bar',
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
)

expect(MeiliSearch::Utils).to have_received(:transform_attributes)
.with(
canceled_by: [1, 2], uids: [2],
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
)
end

describe '#index.wait_for_task' do
it 'waits for task with default values' do
task = index.add_documents(documents)
Expand Down Expand Up @@ -133,4 +152,66 @@
end.to raise_error(Timeout::Error)
end
end

describe '#client.cancel_tasks' do
it 'ensures supports to all available filters' do
allow(MeiliSearch::Utils).to receive(:transform_attributes).and_call_original

client.cancel_tasks(
canceled_by: [1, 2], uids: [2], foo: 'bar',
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
)

expect(MeiliSearch::Utils).to have_received(:transform_attributes)
.with(
canceled_by: [1, 2], uids: [2],
before_enqueued_at: '2022-01-20', after_enqueued_at: '2022-01-20',
before_started_at: '2022-01-20', after_started_at: '2022-01-20',
before_finished_at: '2022-01-20', after_finished_at: '2022-01-20'
)
end

it 'has fields in the details field' do
task = client.cancel_tasks(uids: [1, 2])
task = client.wait_for_task(task['taskUid'])

expect(task['details']['originalFilter']).to eq('?uids=1%2C2')
expect(task['details']['matchedTasks']).to be_a(Integer)
expect(task['details']['canceledTasks']).to be_a(Integer)
end
end

describe '#client.delete_tasks' do
it 'ensures supports to all available filters' do
date = DateTime.new(2022, 1, 20)

allow(MeiliSearch::Utils).to receive(:transform_attributes).and_call_original

client.delete_tasks(
canceled_by: [1, 2], uids: [2], foo: 'bar',
before_enqueued_at: date, after_enqueued_at: date,
before_started_at: date, after_started_at: date,
before_finished_at: date, after_finished_at: date
)

expect(MeiliSearch::Utils).to have_received(:transform_attributes)
.with(
canceled_by: [1, 2], uids: [2],
before_enqueued_at: date, after_enqueued_at: date,
before_started_at: date, after_started_at: date,
before_finished_at: date, after_finished_at: date
)
end

it 'has fields in the details field' do
task = client.delete_tasks(uids: [1, 2])
task = client.wait_for_task(task['taskUid'])

expect(task['details']['originalFilter']).to eq('?uids=1%2C2')
expect(task['details']['matchedTasks']).to be_a(Integer)
expect(task['details']['deletedTasks']).to be_a(Integer)
end
end
end
14 changes: 14 additions & 0 deletions spec/meilisearch/index/search/q_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,18 @@
expect(response['hits'].first['objectId']).to eq(4)
expect(response['hits'].first).not_to have_key('_formatted')
end

context 'with finite pagination params' do
it 'responds with specialized fields' do
response = index.search('coco', { page: 2, hits_per_page: 2 })

expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS)
end

it 'responds with specialized fields' do
response = index.search('coco', { page: 2, hitsPerPage: 2 })

expect(response.keys).to contain_exactly(*FINITE_PAGINATED_SEARCH_RESPONSE_KEYS)
end
end
end
23 changes: 23 additions & 0 deletions spec/meilisearch/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

RSpec.describe MeiliSearch::Utils do
describe '.parse_query' do
it 'transforms arrays into strings' do
data = described_class.parse_query({ array: [1, 2, 3], other: 'string' }, [:array, :other])

expect(data).to eq({ 'array' => '1,2,3', 'other' => 'string' })
end

it 'cleans list based on another list' do
data = described_class.parse_query({ array: [1, 2, 3], ignore: 'string' }, [:array])

expect(data).to eq({ 'array' => '1,2,3' })
end

it 'transforms dates into strings' do
data = described_class.parse_query({ date: DateTime.new(2012, 12, 21, 19, 5) }, [:date])

expect(data).to eq({ 'date' => '2012-12-21T19:05:00+00:00' })
end
end
end
10 changes: 10 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@
'nbHits'
].freeze

FINITE_PAGINATED_SEARCH_RESPONSE_KEYS = [
'hits',
'query',
'processingTimeMs',
'hitsPerPage',
'page',
'totalPages',
'totalHits'
].freeze

Dir["#{Dir.pwd}/spec/support/**/*.rb"].sort.each { |file| require file }

RSpec.configure do |config|
Expand Down