diff --git a/lib/meilisearch/multi_search.rb b/lib/meilisearch/multi_search.rb index 2cb3cba1..d981cd3e 100644 --- a/lib/meilisearch/multi_search.rb +++ b/lib/meilisearch/multi_search.rb @@ -2,10 +2,15 @@ module MeiliSearch module MultiSearch - def multi_search(data) + # Performs search on one or more indexes + # + # @param [Hash] federation_options + # - `limit`: number of results in the merged list + # - `offset`: number of results to skip in the merged list + def multi_search(data, federation_options = nil) body = Utils.transform_attributes(data) - http_post '/multi-search', queries: body + http_post '/multi-search', queries: body, federation: federation_options end end end diff --git a/spec/meilisearch/client/multi_search_spec.rb b/spec/meilisearch/client/multi_search_spec.rb index 4efc0aa7..bde29623 100644 --- a/spec/meilisearch/client/multi_search_spec.rb +++ b/spec/meilisearch/client/multi_search_spec.rb @@ -16,4 +16,37 @@ expect(response['results'][0]['estimatedTotalHits']).to eq(0) expect(response['results'][1]['estimatedTotalHits']).to eq(0) end + + it 'does a federated search with two different indexes' do + client.index('books').add_documents( + [ + { id: 1, title: 'Harry Potter and the Philosophers Stone' }, + { id: 2, title: 'War and Peace' } + ] + ) + + client.index('movies').add_documents( + [ + { id: 1, title: 'Harry Potter and the Philosophers Stone' }, + { id: 2, title: 'Lord of the Rings' } + ] + ).await + + response = client.multi_search([ + { index_uid: 'books', q: 'Harry Potter' }, + { index_uid: 'movies', q: 'Harry Potter' } + ], + { + limit: 20, + offset: 0 + }) + + expect(response).to have_key('hits') + expect(response['hits'].first).to have_key('_federation') + expect(response['hits'].first['_federation']).to have_key('indexUid') + expect(response).to have_key('estimatedTotalHits') + expect(response).not_to have_key('results') + expect(response['limit']).to eq(20) + expect(response['offset']).to eq(0) + end end