Skip to content

Commit 3198ac8

Browse files
Merge #344
344: Document multi search usage r=brunoocasali a=ellnix Fixes #337 Co-authored-by: ellnix <[email protected]>
2 parents b5983bd + 37c894c commit 3198ac8

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

README.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
- [Compatibility](#-compatibility)
3939
- [⚙️ Settings](#️-settings)
4040
- [🔍 Custom search](#-custom-search)
41+
- [🔍🔍 Multi search](#-multi-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-)
@@ -240,6 +241,58 @@ Book.search('*', sort: ['title:asc'])
240241

241242
👉 Don't forget to set up the `sortable_attributes` option in the `meilisearch` block of your model.
242243

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+
243296
## 🪛 Options
244297

245298
### Meilisearch configuration & environment

0 commit comments

Comments
 (0)