Skip to content

Commit 92d0bdf

Browse files
committed
Document federated search in the README
1 parent 674b127 commit 92d0bdf

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed

README.md

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [⚙️ Settings](#️-settings)
3939
- [🔍 Custom search](#-custom-search)
4040
- [🔍🔍 Multi search](#-multi-search)
41+
- [🔍🔍 Federated search](#-federated-search)
4142
- [🪛 Options](#-options)
4243
- [Meilisearch configuration & environment](#meilisearch-configuration--environment)
4344
- [Pagination with `kaminari` or `will_paginate`](#backend-pagination-with-kaminari-or-will_paginate-)
@@ -323,6 +324,158 @@ But this has been deprecated in favor of **federated search**.
323324

324325
See the [official multi search documentation](https://www.meilisearch.com/docs/reference/api/multi_search).
325326

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', class_name: 'Book' },
335+
{ q: 'Attack on Titan', class_name: 'Manga' }
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', class_name: 'Book' },
374+
'mangas_production' => { q: 'Attack on Titan', class_name: 'Manga' }
375+
}
376+
)
377+
```
378+
379+
```ruby
380+
results = Meilisearch::Rails.federated_search(
381+
queries: {
382+
'potter' => { q: 'Harry', class_name: 'Book', index_uid: 'books_production' },
383+
'titan' => { q: 'Attack on Titan', class_name: 'Manga', index_uid: 'mangas_production' }
384+
}
385+
)
386+
```
387+
388+
### Loading records <!-- omit in toc -->
389+
390+
Records are loaded when the `:class_name` option is passed, or when a hash query is used with models as keys:
391+
392+
```ruby
393+
results = Meilisearch::Rails.federated_search(
394+
queries: [
395+
{ q: 'Harry', class_name: 'Book' },
396+
{ q: 'Attack on Titan', class_name: 'Manga' },
397+
]
398+
)
399+
```
400+
401+
```ruby
402+
results = Meilisearch::Rails.federated_search(
403+
queries: {
404+
Book => { q: 'Harry' },
405+
Manga => { q: 'Attack on Titan' }
406+
}
407+
)
408+
```
409+
410+
If the model is not provided, hashes are returned!
411+
412+
### Specifying the search index <!-- omit in toc -->
413+
414+
In order of precedence, to figure out which index to search, Meilisearch Rails will check:
415+
416+
1. `index_uid` options
417+
```ruby
418+
results = Meilisearch::Rails.federated_search(
419+
queries: [
420+
# Searching the 'fantasy_books' index
421+
{ q: 'Harry', class_name: 'Book', index_uid: 'fantasy_books' },
422+
]
423+
)
424+
```
425+
2. The index associated with the model
426+
```ruby
427+
results = Meilisearch::Rails.federated_search(
428+
queries: [
429+
# Searching the index associated with the Book model
430+
# i. e. Book.index.uid
431+
{ q: 'Harry', class_name: 'Book' },
432+
]
433+
)
434+
```
435+
3. The key when using hash queries
436+
```ruby
437+
results = Meilisearch::Rails.federated_search(
438+
queries: {
439+
# Searching index 'books_production'
440+
books_production: { q: 'Harry', class_name: 'Book' },
441+
}
442+
)
443+
```
444+
445+
### Pagination and other options <!-- omit in toc -->
446+
447+
In addition to queries, federated search also accepts `:federation` parameters which allow for finer control of the search:
448+
449+
```ruby
450+
results = Meilisearch::Rails.federated_search(
451+
queries: [
452+
{ q: 'Harry', class_name: 'Book' },
453+
{ q: 'Attack on Titan', class_name: 'Manga' },
454+
],
455+
federation: { offset: 10, limit: 5 }
456+
)
457+
```
458+
See a full list of accepted options in [the meilisearch documentation](https://www.meilisearch.com/docs/reference/api/multi_search#federation).
459+
460+
#### Metadata <!-- omit in toc -->
461+
462+
The returned result from a federated search includes a `.metadata` attribute you can use to access everything other than the search hits:
463+
464+
```ruby
465+
result.metadata
466+
# {
467+
# "processingTimeMs" => 0,
468+
# "limit" => 20,
469+
# "offset" => 0,
470+
# "estimatedTotalHits" => 2,
471+
# "semanticHitCount": 0
472+
# }
473+
```
474+
475+
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).
476+
477+
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).
478+
326479
## 🪛 Options
327480

328481
### Meilisearch configuration & environment

0 commit comments

Comments
 (0)