From 37c894cf4dd7c59f35e3a7b0b18ba376423feb6b Mon Sep 17 00:00:00 2001 From: ellnix <103502144+ellnix@users.noreply.github.com> Date: Thu, 29 Feb 2024 12:46:33 +0100 Subject: [PATCH] Document multi search usage --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/README.md b/README.md index a833bb31..ddd410dd 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ - [Compatibility](#-compatibility) - [⚙️ Settings](#️-settings) - [🔍 Custom search](#-custom-search) +- [🔍🔍 Multi search](#-multi-search) - [🪛 Options](#-options) - [Meilisearch configuration & environment](#meilisearch-configuration--environment) - [Pagination with `kaminari` or `will_paginate`](#backend-pagination-with-kaminari-or-will_paginate-) @@ -240,6 +241,58 @@ Book.search('*', sort: ['title:asc']) 👉 Don't forget to set up the `sortable_attributes` option in the `meilisearch` block of your model. +## 🔍🔍 Multi search + +Meilisearch supports searching multiple models at the same time (see [🔍 Custom search](#-custom-search) for search options): + +```ruby +multi_search_results = MeiliSearch::Rails.multi_search( + Book => { q: 'Harry' }, + Manga => { q: 'Attack' } +) +``` + +You can iterate through the results with `.each` or `.each_result`: + +```erb +<% multi_search_results.each do |record| %> +

<%= record.title %>

+

<%= record.author %>

+<% end %> + +

Harry Potter and the Philosopher's Stone

+

J. K. Rowling

+

Harry Potter and the Chamber of Secrets

+

J. K. Rowling

+

Attack on Titan

+

Iseyama

+``` + +```erb +<% multi_search_results.each_result do |klass, results| %> +

<%= klass.name.pluralize %>

+ + +<% end %> + + +

Books

+ +

Mangas

+ +``` + +See the [official multi search documentation](https://www.meilisearch.com/docs/reference/api/multi_search). + ## 🪛 Options ### Meilisearch configuration & environment