|
38 | 38 | - [⚙️ Settings](#️-settings) |
39 | 39 | - [🔍 Custom search](#-custom-search) |
40 | 40 | - [🔍🔍 Multi search](#-multi-search) |
| 41 | +- [🔍🔍 Federated search](#-federated-search) |
41 | 42 | - [🪛 Options](#-options) |
42 | 43 | - [Meilisearch configuration & environment](#meilisearch-configuration--environment) |
43 | 44 | - [Pagination with `kaminari` or `will_paginate`](#backend-pagination-with-kaminari-or-will_paginate-) |
@@ -282,6 +283,158 @@ You can iterate through the results with `.each` or `.each_result`: |
282 | 283 |
|
283 | 284 | See the [official multi search documentation](https://www.meilisearch.com/docs/reference/api/multi_search). |
284 | 285 |
|
| 286 | +## 🔍🔍 Federated search |
| 287 | + |
| 288 | +Federated search is similar to multi search, except that results are not grouped but sorted by ranking rules. |
| 289 | + |
| 290 | +```ruby |
| 291 | +results = MeiliSearch::Rails.federated_search( |
| 292 | + queries: [ |
| 293 | + { q: 'Harry', class_name: 'Book' }, |
| 294 | + { q: 'Attack on Titan', class_name: 'Manga' } |
| 295 | + ] |
| 296 | +) |
| 297 | +``` |
| 298 | + |
| 299 | +An enumerable `FederatedSearchResult` is returned, which can be iterated through with `#each`: |
| 300 | + |
| 301 | +```erb |
| 302 | +<ul> |
| 303 | + <% results.each do |record| %> |
| 304 | + <li><%= record.title %></li> |
| 305 | + <% end %> |
| 306 | +</ul> |
| 307 | +
|
| 308 | +
|
| 309 | +<ul> |
| 310 | + <!-- Attack on Titan appears first even though it was specified second, |
| 311 | + it's ranked higher because it's a closer match --> |
| 312 | + <li>Attack on Titan</li> |
| 313 | + <li>Harry Potter and the Philosopher's Stone</li> |
| 314 | + <li>Harry Potter and the Chamber of Secrets</li> |
| 315 | +</ul> |
| 316 | +``` |
| 317 | + |
| 318 | +The `queries` parameter may be a multi-search style hash with keys that are either classes, index names, or neither: |
| 319 | + |
| 320 | +```ruby |
| 321 | +results = MeiliSearch::Rails.federated_search( |
| 322 | + queries: { |
| 323 | + Book => { q: 'Harry' }, |
| 324 | + Manga => { q: 'Attack on Titan' } |
| 325 | + } |
| 326 | +) |
| 327 | +``` |
| 328 | + |
| 329 | +```ruby |
| 330 | +results = MeiliSearch::Rails.federated_search( |
| 331 | + queries: { |
| 332 | + 'books_production' => { q: 'Harry', class_name: 'Book' }, |
| 333 | + 'mangas_production' => { q: 'Attack on Titan', class_name: 'Manga' } |
| 334 | + } |
| 335 | +) |
| 336 | +``` |
| 337 | + |
| 338 | +```ruby |
| 339 | +results = MeiliSearch::Rails.federated_search( |
| 340 | + queries: { |
| 341 | + 'potter' => { q: 'Harry', class_name: 'Book', index_uid: 'books_production' }, |
| 342 | + 'titan' => { q: 'Attack on Titan', class_name: 'Manga', index_uid: 'mangas_production' } |
| 343 | + } |
| 344 | +) |
| 345 | +``` |
| 346 | + |
| 347 | +### Loading records <!-- omit in toc --> |
| 348 | + |
| 349 | +Records are loaded when the `:class_name` option is passed, or when a hash query is used with models as keys: |
| 350 | + |
| 351 | +```ruby |
| 352 | +results = MeiliSearch::Rails.federated_search( |
| 353 | + queries: [ |
| 354 | + { q: 'Harry', class_name: 'Book' }, |
| 355 | + { q: 'Attack on Titan', class_name: 'Manga' }, |
| 356 | + ] |
| 357 | +) |
| 358 | +``` |
| 359 | + |
| 360 | +```ruby |
| 361 | +results = MeiliSearch::Rails.federated_search( |
| 362 | + queries: { |
| 363 | + Book => { q: 'Harry' }, |
| 364 | + Manga => { q: 'Attack on Titan' } |
| 365 | + } |
| 366 | +) |
| 367 | +``` |
| 368 | + |
| 369 | +If the model is not provided, hashes are returned! |
| 370 | + |
| 371 | +### Specifying the search index <!-- omit in toc --> |
| 372 | + |
| 373 | +In order of precedence, to figure out which index to search, Meilisearch Rails will check: |
| 374 | + |
| 375 | +1. `index_uid` options |
| 376 | + ```ruby |
| 377 | + results = MeiliSearch::Rails.federated_search( |
| 378 | + queries: [ |
| 379 | + # Searching the 'fantasy_books' index |
| 380 | + { q: 'Harry', class_name: 'Book', index_uid: 'fantasy_books' }, |
| 381 | + ] |
| 382 | + ) |
| 383 | + ``` |
| 384 | +2. The index associated with the model |
| 385 | + ```ruby |
| 386 | + results = MeiliSearch::Rails.federated_search( |
| 387 | + queries: [ |
| 388 | + # Searching the index associated with the Book model |
| 389 | + # i. e. Book.index.uid |
| 390 | + { q: 'Harry', class_name: 'Book' }, |
| 391 | + ] |
| 392 | + ) |
| 393 | + ``` |
| 394 | +3. The key when using hash queries |
| 395 | + ```ruby |
| 396 | + results = MeiliSearch::Rails.federated_search( |
| 397 | + queries: { |
| 398 | + # Searching index 'books_production' |
| 399 | + books_production: { q: 'Harry', class_name: 'Book' }, |
| 400 | + } |
| 401 | + ) |
| 402 | + ``` |
| 403 | + |
| 404 | +### Pagination and other options <!-- omit in toc --> |
| 405 | + |
| 406 | +In addition to queries, federated search also accepts `:federation` parameters which allow for finer control of the search: |
| 407 | + |
| 408 | +```ruby |
| 409 | +results = MeiliSearch::Rails.federated_search( |
| 410 | + queries: [ |
| 411 | + { q: 'Harry', class_name: 'Book' }, |
| 412 | + { q: 'Attack on Titan', class_name: 'Manga' }, |
| 413 | + ], |
| 414 | + federation: { offset: 10, limit: 5 } |
| 415 | +) |
| 416 | +``` |
| 417 | +See a full list of accepted options in [the meilisearch documentation](https://www.meilisearch.com/docs/reference/api/multi_search#federation). |
| 418 | + |
| 419 | +#### Metadata <!-- omit in toc --> |
| 420 | + |
| 421 | +The returned result from a federated search includes a `.metadata` attribute you can use to access everything other than the search hits: |
| 422 | + |
| 423 | +```ruby |
| 424 | +result.metadata |
| 425 | +# { |
| 426 | +# "processingTimeMs" => 0, |
| 427 | +# "limit" => 20, |
| 428 | +# "offset" => 0, |
| 429 | +# "estimatedTotalHits" => 2, |
| 430 | +# "semanticHitCount": 0 |
| 431 | +# } |
| 432 | +``` |
| 433 | + |
| 434 | +The metadata contains facet stats and pagination stats, among others. See the full response in [the documentation](https://www.meilisearch.com/docs/reference/api/multi_search#federated-multi-search-requests). |
| 435 | + |
| 436 | +More details on federated search (such as available `federation:` options) can be found on [the official multi search documentation](https://www.meilisearch.com/docs/reference/api/multi_search). |
| 437 | + |
285 | 438 | ## 🪛 Options |
286 | 439 |
|
287 | 440 | ### Meilisearch configuration & environment |
|
0 commit comments