Skip to content

Commit

Permalink
Add federated search
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre authored and andre-m-dev committed Nov 6, 2024
1 parent d1c0c19 commit 83a79c8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/meilisearch/multi_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
33 changes: 33 additions & 0 deletions spec/meilisearch/client/multi_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 83a79c8

Please sign in to comment.