Configure queries for federated search
mainThe queries parameter in federated_search can be structured in several ways depending on how you want to identify the search targets:
- Array of Hashes: Each hash contains a query string
qand an optionalscopeorindex_uid. - Hash with Model Keys: Use the model class as the key. Records will be automatically loaded using that model's scope.
- Hash with Index UIDs: Use string or symbol index names as keys. You can still provide a
scopeto ensure the returned results are loaded as model instances.
Precedence for determining the search index:
- The
index_uidoption within a query hash. - The index associated with the model provided in the
scope(e.g.,Book.index.uid). - The key used in a hash-style query.
# Using an array of hashes with explicit index_uid
results = Meilisearch::Rails.federated_search(
queries: [
{ q: 'Harry', scope: Book, index_uid: 'fantasy_books' }
]
)
# Using a hash with models as keys (automatic loading)
results = Meilisearch::Rails.federated_search(
queries: {
Book => { q: 'Harry' },
Manga => { q: 'Attack on Titan' }
}
)
# Using a hash with index names as keys
results = Meilisearch::Rails.federated_search(
queries: {
'books_production' => { q: 'Harry', scope: Book.all }
}
)