Skip to content

Commit

Permalink
Merge #587
Browse files Browse the repository at this point in the history
587: Support batches API r=brunoocasali a=ellnix

# Pull Request

## Related issue
Fixes #582


Co-authored-by: ellnix <[email protected]>
  • Loading branch information
meili-bors[bot] and ellnix authored Jan 8, 2025
2 parents 5c91e87 + a5bada0 commit 4040605
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,10 @@ search_parameter_reference_distinct_1: |-
client.index('INDEX_NAME').search('QUERY TERMS', {
distinct: 'ATTRIBUTE_A'
})
get_all_batches_1: |-
client.batches
get_batch_1: |-
client.batch(BATCH_UID)
distinct_attribute_guide_filterable_1: |-
client.index('products').update_filterable_attributes([
'product_id',
Expand Down
10 changes: 10 additions & 0 deletions lib/meilisearch/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,16 @@ def wait_for_task(task_uid, timeout_in_ms = 5000, interval_in_ms = 50)
task_endpoint.wait_for_task(task_uid, timeout_in_ms, interval_in_ms)
end

### BATCHES

def batches(options = {})
http_get '/batches', options
end

def batch(batch_uid)
http_get "/batches/#{batch_uid}"
end

private

def index_object(uid, primary_key = nil)
Expand Down
63 changes: 63 additions & 0 deletions spec/meilisearch/client/batches_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# frozen_string_literal: true

RSpec.describe 'Meilisearch::Client - Batches' do
let(:index) { client.index(random_uid) }

def new_task
index.add_documents({ id: 1 })
end

describe '#batches' do
it 'includes the most recent batch' do
new_task.await

expect(client.batches).to match(
'results' => array_including(
a_hash_including(
'details' => a_hash_including('receivedDocuments' => 1),
'stats' => a_hash_including(
'types' => { 'documentAdditionOrUpdate' => 1 }
)
)
),
'total' => anything,
'limit' => 20,
'next' => anything,
'from' => anything
)
end

it 'accepts options such as limit' do
new_task.await
new_task.await

batches = client.batches(limit: 1)
unlimited_batches = client.batches

expect(batches['results']).to be_one
expect(unlimited_batches['results'].count).to be > 1
end

it 'allows searching by task uids' do
new_tasks = Array.new(3) { new_task }

new_tasks.last.await # give time for meilisearch to batch new tasks
batches = client.batches(uids: new_tasks.map(&:uid).join(','))

task_count = batches['results'].sum do |batch|
batch['stats']['totalNbTasks'].to_i
end

expect(task_count).to eq(3)
end
end

context '#batch' do
it 'shows details of a single batch' do
new_task.await

first_batch = client.batches['results'].first
expect(client.batch(first_batch['uid'])).to eq(first_batch)
end
end
end

0 comments on commit 4040605

Please sign in to comment.