|
38 | 38 | - [Compatibility](#-compatibility) |
39 | 39 | - [⚙️ Settings](#️-settings) |
40 | 40 | - [🔍 Custom search](#-custom-search) |
| 41 | +- [🔍🔍 Multi search](#-multi-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-) |
@@ -240,6 +241,58 @@ Book.search('*', sort: ['title:asc']) |
240 | 241 |
|
241 | 242 | 👉 Don't forget to set up the `sortable_attributes` option in the `meilisearch` block of your model. |
242 | 243 |
|
| 244 | +## 🔍🔍 Multi search |
| 245 | + |
| 246 | +Meilisearch supports searching multiple models at the same time (see [🔍 Custom search](#-custom-search) for search options): |
| 247 | + |
| 248 | +```ruby |
| 249 | +multi_search_results = MeiliSearch::Rails.multi_search( |
| 250 | + Book => { q: 'Harry' }, |
| 251 | + Manga => { q: 'Attack' } |
| 252 | +) |
| 253 | +``` |
| 254 | + |
| 255 | +You can iterate through the results with `.each` or `.each_result`: |
| 256 | + |
| 257 | +```erb |
| 258 | +<% multi_search_results.each do |record| %> |
| 259 | + <p><%= record.title %></p> |
| 260 | + <p><%= record.author %></p> |
| 261 | +<% end %> |
| 262 | +
|
| 263 | +<p>Harry Potter and the Philosopher's Stone</p> |
| 264 | +<p>J. K. Rowling</p> |
| 265 | +<p>Harry Potter and the Chamber of Secrets</p> |
| 266 | +<p>J. K. Rowling</p> |
| 267 | +<p>Attack on Titan</p> |
| 268 | +<p>Iseyama</p> |
| 269 | +``` |
| 270 | + |
| 271 | +```erb |
| 272 | +<% multi_search_results.each_result do |klass, results| %> |
| 273 | + <p><%= klass.name.pluralize %></p> |
| 274 | +
|
| 275 | + <ul> |
| 276 | + <% results.each do |record| %> |
| 277 | + <li><%= record.title %></li> |
| 278 | + <% end %> |
| 279 | + </ul> |
| 280 | +<% end %> |
| 281 | +
|
| 282 | +
|
| 283 | +<p>Books</p> |
| 284 | +<ul> |
| 285 | + <li>Harry Potter and the Philosopher's Stone</li> |
| 286 | + <li>Harry Potter and the Chamber of Secrets</li> |
| 287 | +</ul> |
| 288 | +<p>Mangas</p> |
| 289 | +<ul> |
| 290 | + <li>Attack on Titan</li> |
| 291 | +</ul> |
| 292 | +``` |
| 293 | + |
| 294 | +See the [official multi search documentation](https://www.meilisearch.com/docs/reference/api/multi_search). |
| 295 | + |
243 | 296 | ## 🪛 Options |
244 | 297 |
|
245 | 298 | ### Meilisearch configuration & environment |
|
0 commit comments