|
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-) |
@@ -323,6 +324,172 @@ But this has been deprecated in favor of **federated search**. |
323 | 324 |
|
324 | 325 | See the [official multi search documentation](https://www.meilisearch.com/docs/reference/api/multi_search). |
325 | 326 |
|
| 327 | +## 🔍🔍 Federated search |
| 328 | + |
| 329 | +Federated search is similar to multi search, except that results are not grouped but sorted by ranking rules. |
| 330 | + |
| 331 | +```ruby |
| 332 | +results = Meilisearch::Rails.federated_search( |
| 333 | + queries: [ |
| 334 | + { q: 'Harry', scope: Book.all }, |
| 335 | + { q: 'Attack on Titan', scope: Manga.all } |
| 336 | + ] |
| 337 | +) |
| 338 | +``` |
| 339 | + |
| 340 | +An enumerable `FederatedSearchResult` is returned, which can be iterated through with `#each`: |
| 341 | + |
| 342 | +```erb |
| 343 | +<ul> |
| 344 | + <% results.each do |record| %> |
| 345 | + <li><%= record.title %></li> |
| 346 | + <% end %> |
| 347 | +</ul> |
| 348 | +
|
| 349 | +
|
| 350 | +<ul> |
| 351 | + <!-- Attack on Titan appears first even though it was specified second, |
| 352 | + it's ranked higher because it's a closer match --> |
| 353 | + <li>Attack on Titan</li> |
| 354 | + <li>Harry Potter and the Philosopher's Stone</li> |
| 355 | + <li>Harry Potter and the Chamber of Secrets</li> |
| 356 | +</ul> |
| 357 | +``` |
| 358 | + |
| 359 | +The `queries` parameter may be a multi-search style hash with keys that are either classes, index names, or neither: |
| 360 | + |
| 361 | +```ruby |
| 362 | +results = Meilisearch::Rails.federated_search( |
| 363 | + queries: { |
| 364 | + Book => { q: 'Harry' }, |
| 365 | + Manga => { q: 'Attack on Titan' } |
| 366 | + } |
| 367 | +) |
| 368 | +``` |
| 369 | + |
| 370 | +```ruby |
| 371 | +results = Meilisearch::Rails.federated_search( |
| 372 | + queries: { |
| 373 | + 'books_production' => { q: 'Harry', scope: Book.all }, |
| 374 | + 'mangas_production' => { q: 'Attack on Titan', scope: Manga.all } |
| 375 | + } |
| 376 | +) |
| 377 | +``` |
| 378 | + |
| 379 | +```ruby |
| 380 | +results = Meilisearch::Rails.federated_search( |
| 381 | + queries: { |
| 382 | + 'potter' => { q: 'Harry', scope: Book.all, index_uid: 'books_production' }, |
| 383 | + 'titan' => { q: 'Attack on Titan', scope: Manga.all, index_uid: 'mangas_production' } |
| 384 | + } |
| 385 | +) |
| 386 | +``` |
| 387 | + |
| 388 | +### Loading records <!-- omit in toc --> |
| 389 | + |
| 390 | +Records are loaded when the `:scope` option is passed (may be a model or a relation), |
| 391 | +or when a hash query is used with models as keys: |
| 392 | + |
| 393 | +```ruby |
| 394 | +results = Meilisearch::Rails.federated_search( |
| 395 | + queries: [ |
| 396 | + { q: 'Harry', scope: Book }, |
| 397 | + { q: 'Attack on Titan', scope: Manga }, |
| 398 | + ] |
| 399 | +) |
| 400 | +``` |
| 401 | + |
| 402 | +```ruby |
| 403 | +results = Meilisearch::Rails.federated_search( |
| 404 | + queries: { |
| 405 | + Book => { q: 'Harry' }, |
| 406 | + Manga => { q: 'Attack on Titan' } |
| 407 | + } |
| 408 | +) |
| 409 | +``` |
| 410 | + |
| 411 | +If the model is not provided, hashes are returned! |
| 412 | + |
| 413 | +### Scoping records <!-- omit in toc --> |
| 414 | + |
| 415 | +Any relation passed as `:scope` is used as the starting point when loading records: |
| 416 | + |
| 417 | +```ruby |
| 418 | +results = Meilisearch::Rails.federated_search( |
| 419 | + queries: [ |
| 420 | + { q: 'Harry', scope: Book.where('year <= 2006') }, |
| 421 | + { q: 'Attack on Titan', scope: Manga.where(author: Author.find_by(name: 'Iseyama')) }, |
| 422 | + ] |
| 423 | +) |
| 424 | +``` |
| 425 | + |
| 426 | +### Specifying the search index <!-- omit in toc --> |
| 427 | + |
| 428 | +In order of precedence, to figure out which index to search, Meilisearch Rails will check: |
| 429 | + |
| 430 | +1. `index_uid` options |
| 431 | + ```ruby |
| 432 | + results = Meilisearch::Rails.federated_search( |
| 433 | + queries: [ |
| 434 | + # Searching the 'fantasy_books' index |
| 435 | + { q: 'Harry', scope: Book, index_uid: 'fantasy_books' }, |
| 436 | + ] |
| 437 | + ) |
| 438 | + ``` |
| 439 | +2. The index associated with the model |
| 440 | + ```ruby |
| 441 | + results = Meilisearch::Rails.federated_search( |
| 442 | + queries: [ |
| 443 | + # Searching the index associated with the Book model |
| 444 | + # i. e. Book.index.uid |
| 445 | + { q: 'Harry', scope: Book }, |
| 446 | + ] |
| 447 | + ) |
| 448 | + ``` |
| 449 | +3. The key when using hash queries |
| 450 | + ```ruby |
| 451 | + results = Meilisearch::Rails.federated_search( |
| 452 | + queries: { |
| 453 | + # Searching index 'books_production' |
| 454 | + books_production: { q: 'Harry', scope: Book }, |
| 455 | + } |
| 456 | + ) |
| 457 | + ``` |
| 458 | + |
| 459 | +### Pagination and other options <!-- omit in toc --> |
| 460 | + |
| 461 | +In addition to queries, federated search also accepts `:federation` parameters which allow for finer control of the search: |
| 462 | + |
| 463 | +```ruby |
| 464 | +results = Meilisearch::Rails.federated_search( |
| 465 | + queries: [ |
| 466 | + { q: 'Harry', scope: Book }, |
| 467 | + { q: 'Attack on Titan', scope: Manga }, |
| 468 | + ], |
| 469 | + federation: { offset: 10, limit: 5 } |
| 470 | +) |
| 471 | +``` |
| 472 | +See a full list of accepted options in [the meilisearch documentation](https://www.meilisearch.com/docs/reference/api/multi_search#federation). |
| 473 | + |
| 474 | +#### Metadata <!-- omit in toc --> |
| 475 | + |
| 476 | +The returned result from a federated search includes a `.metadata` attribute you can use to access everything other than the search hits: |
| 477 | + |
| 478 | +```ruby |
| 479 | +result.metadata |
| 480 | +# { |
| 481 | +# "processingTimeMs" => 0, |
| 482 | +# "limit" => 20, |
| 483 | +# "offset" => 0, |
| 484 | +# "estimatedTotalHits" => 2, |
| 485 | +# "semanticHitCount": 0 |
| 486 | +# } |
| 487 | +``` |
| 488 | + |
| 489 | +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). |
| 490 | + |
| 491 | +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). |
| 492 | + |
326 | 493 | ## 🪛 Options |
327 | 494 |
|
328 | 495 | ### Meilisearch configuration & environment |
|
0 commit comments