meilisearch-rails 0.10.2 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rspec +1 -0
- data/Gemfile +9 -5
- data/LICENSE +1 -1
- data/README.md +300 -50
- data/lib/meilisearch/rails/configuration.rb +3 -3
- data/lib/meilisearch/rails/errors.rb +2 -2
- data/lib/meilisearch/rails/ms_clean_up_job.rb +19 -0
- data/lib/meilisearch/rails/ms_job.rb +1 -1
- data/lib/meilisearch/rails/multi_search/federated_search_result.rb +78 -0
- data/lib/meilisearch/rails/multi_search/multi_search_result.rb +115 -0
- data/lib/meilisearch/rails/multi_search.rb +104 -0
- data/lib/meilisearch/rails/null_object.rb +1 -1
- data/lib/meilisearch/rails/pagination/kaminari.rb +10 -4
- data/lib/meilisearch/rails/pagination/will_paginate.rb +9 -3
- data/lib/meilisearch/rails/pagination.rb +5 -5
- data/lib/meilisearch/rails/railtie.rb +1 -1
- data/lib/meilisearch/rails/tasks/meilisearch.rake +4 -4
- data/lib/meilisearch/rails/templates/initializer.rb +1 -1
- data/lib/meilisearch/rails/utilities.rb +10 -2
- data/lib/meilisearch/rails/version.rb +2 -2
- data/lib/meilisearch-rails.rb +119 -69
- data/meilisearch-rails.gemspec +4 -3
- metadata +27 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9f9160abd3574add59fc40a7d118ae19acf61c6d1378e732922681e3efba38e7
|
|
4
|
+
data.tar.gz: 3e1f25e0175a89699880f1225a51f1fca99ebb6fb1f9800f49e81f4c8a4c1357
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3ab450b2fe203297e17b96bf4dc2b6e1289e8fbcb6bd7e000b14092e6136e2c0f089f66af2a0bd88d7df4f0a6a0ab02accee49c40419e1902d86b9583cca8695
|
|
7
|
+
data.tar.gz: 41eb28679a52abac258559686bf15ea8af79b8dcc814511390a7db7f3cd75fc0062036ba4ef23d687f507c1c82b520c9af6a69ec03b707e80ccea25aab5988ef
|
data/.rspec
CHANGED
data/Gemfile
CHANGED
|
@@ -13,17 +13,20 @@ group :development do
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
group :test do
|
|
16
|
-
rails_version = ENV['RAILS_VERSION'] || '
|
|
16
|
+
rails_version = ENV['RAILS_VERSION'] || '7.1'
|
|
17
17
|
sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
|
|
18
|
+
mongoid_version = ENV['MONGOID_VERSION'] || '9.0'
|
|
18
19
|
|
|
19
20
|
gem 'active_model_serializers'
|
|
20
21
|
gem 'rails', "~> #{rails_version}"
|
|
21
22
|
gem 'sequel', sequel_version
|
|
23
|
+
gem 'mongoid', "~> #{mongoid_version}"
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
+
# remove when deprecate rails 6
|
|
26
|
+
if Gem::Version.new(rails_version) >= Gem::Version.new('7.0')
|
|
27
|
+
gem 'sqlite3', '~> 2', platform: %i[rbx ruby]
|
|
25
28
|
else
|
|
26
|
-
gem 'sqlite3', '<
|
|
29
|
+
gem 'sqlite3', '< 2', platform: %i[rbx ruby]
|
|
27
30
|
end
|
|
28
31
|
|
|
29
32
|
gem 'activerecord-jdbc-adapter', platform: :jruby
|
|
@@ -31,8 +34,9 @@ group :test do
|
|
|
31
34
|
gem 'jdbc-sqlite3', platform: :jruby
|
|
32
35
|
gem 'rspec', '~> 3.0'
|
|
33
36
|
gem 'simplecov', require: 'false'
|
|
34
|
-
gem '
|
|
37
|
+
gem 'simplecov-cobertura', require: 'false'
|
|
35
38
|
gem 'threads'
|
|
39
|
+
gem 'logger'
|
|
36
40
|
|
|
37
41
|
gem 'byebug'
|
|
38
42
|
gem 'dotenv', '~> 2.7', '>= 2.7.6'
|
data/LICENSE
CHANGED
data/README.md
CHANGED
|
@@ -32,12 +32,13 @@
|
|
|
32
32
|
## Table of Contents <!-- omit in TOC -->
|
|
33
33
|
|
|
34
34
|
- [📖 Documentation](#-documentation)
|
|
35
|
-
- [⚡ Supercharge your Meilisearch experience](#-supercharge-your-meilisearch-experience)
|
|
36
35
|
- [🤖 Compatibility with Meilisearch](#-compatibility-with-meilisearch)
|
|
37
36
|
- [🚀 Getting started](#-getting-started)
|
|
38
37
|
- [Compatibility](#-compatibility)
|
|
39
38
|
- [⚙️ Settings](#️-settings)
|
|
40
39
|
- [🔍 Custom search](#-custom-search)
|
|
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-)
|
|
@@ -65,17 +66,13 @@ The whole usage of this gem is detailed in this README.
|
|
|
65
66
|
|
|
66
67
|
To learn more about Meilisearch, check out our [Documentation](https://www.meilisearch.com/docs/learn/tutorials/getting_started.html) or our [API References](https://www.meilisearch.com/docs/reference/api/).
|
|
67
68
|
|
|
68
|
-
## ⚡ Supercharge your Meilisearch experience
|
|
69
|
-
|
|
70
|
-
Say goodbye to server deployment and manual updates with [Meilisearch Cloud](https://www.meilisearch.com/pricing?utm_campaign=oss&utm_source=integration&utm_medium=meilisearch-rails). No credit card required.
|
|
71
|
-
|
|
72
69
|
## 🤖 Compatibility with Meilisearch
|
|
73
70
|
|
|
74
71
|
This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-rails/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.
|
|
75
72
|
|
|
76
73
|
## 🔧 Installation <!-- omit in toc -->
|
|
77
74
|
|
|
78
|
-
This package requires Ruby version
|
|
75
|
+
This package requires Ruby version 3.0 or later and Rails 6.1 or later. It may work in older versions but it is not officially supported.
|
|
79
76
|
|
|
80
77
|
With `gem` in command line:
|
|
81
78
|
```bash
|
|
@@ -91,16 +88,9 @@ gem 'meilisearch-rails'
|
|
|
91
88
|
|
|
92
89
|
### Run Meilisearch <!-- omit in toc -->
|
|
93
90
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
For example, if you use Docker:
|
|
97
|
-
|
|
98
|
-
```bash
|
|
99
|
-
docker pull getmeili/meilisearch:latest # Fetch the latest version of Meilisearch image from Docker Hub
|
|
100
|
-
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest meilisearch --master-key=masterKey
|
|
101
|
-
```
|
|
91
|
+
⚡️ **Launch, scale, and streamline in minutes with Meilisearch Cloud**—no maintenance, no commitment, cancel anytime. [Try it free now](https://cloud.meilisearch.com/login?utm_campaign=oss&utm_source=github&utm_medium=meilisearch-rails).
|
|
102
92
|
|
|
103
|
-
|
|
93
|
+
🪨 Prefer to self-host? [Download and deploy](https://www.meilisearch.com/docs/learn/self_hosted/getting_started_with_self_hosted_meilisearch?utm_campaign=oss&utm_source=github&utm_medium=meilisearch-rails) our fast, open-source search engine on your own infrastructure.
|
|
104
94
|
|
|
105
95
|
## 🚀 Getting started
|
|
106
96
|
|
|
@@ -109,7 +99,7 @@ NB: you can also download Meilisearch from **Homebrew** or **APT**.
|
|
|
109
99
|
Create a new file `config/initializers/meilisearch.rb` to setup your `MEILISEARCH_HOST` and `MEILISEARCH_API_KEY`
|
|
110
100
|
|
|
111
101
|
```ruby
|
|
112
|
-
|
|
102
|
+
Meilisearch::Rails.configuration = {
|
|
113
103
|
meilisearch_url: ENV.fetch('MEILISEARCH_HOST', 'http://localhost:7700'),
|
|
114
104
|
meilisearch_api_key: ENV.fetch('MEILISEARCH_API_KEY', 'YourMeilisearchAPIKey')
|
|
115
105
|
}
|
|
@@ -131,7 +121,7 @@ The following code will create a `Book` index and add search capabilities to you
|
|
|
131
121
|
|
|
132
122
|
```ruby
|
|
133
123
|
class Book < ActiveRecord::Base
|
|
134
|
-
include
|
|
124
|
+
include Meilisearch::Rails
|
|
135
125
|
|
|
136
126
|
meilisearch do
|
|
137
127
|
attribute :title, :author # only the attributes 'title', and 'author' will be sent to Meilisearch
|
|
@@ -165,7 +155,7 @@ Requests made to Meilisearch may timeout and retry. To adapt the behavior to
|
|
|
165
155
|
your needs, you can change the parameters during configuration:
|
|
166
156
|
|
|
167
157
|
```ruby
|
|
168
|
-
|
|
158
|
+
Meilisearch::Rails.configuration = {
|
|
169
159
|
meilisearch_url: 'YourMeilisearchUrl',
|
|
170
160
|
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
|
171
161
|
timeout: 2,
|
|
@@ -183,7 +173,7 @@ You can configure the index settings by adding them inside the `meilisearch` blo
|
|
|
183
173
|
|
|
184
174
|
```ruby
|
|
185
175
|
class Book < ApplicationRecord
|
|
186
|
-
include
|
|
176
|
+
include Meilisearch::Rails
|
|
187
177
|
|
|
188
178
|
meilisearch do
|
|
189
179
|
searchable_attributes [:title, :author, :publisher, :description]
|
|
@@ -206,6 +196,7 @@ class Book < ApplicationRecord
|
|
|
206
196
|
crop_length 10
|
|
207
197
|
faceting max_values_per_facet: 2000
|
|
208
198
|
pagination max_total_hits: 1000
|
|
199
|
+
proximity_precision 'byWord'
|
|
209
200
|
end
|
|
210
201
|
end
|
|
211
202
|
```
|
|
@@ -229,7 +220,7 @@ harry_book.formatted # => {"id"=>"1", "name"=>"<em>Harry</em> Potter", "descript
|
|
|
229
220
|
👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
|
|
230
221
|
`crop_length` can be set up in the `meilisearch` block of your model.
|
|
231
222
|
|
|
232
|
-
|
|
223
|
+
### 🔍 Sorted search
|
|
233
224
|
|
|
234
225
|
As an example of how to use the sort option, here is how you could achieve
|
|
235
226
|
returning all books sorted by title in ascending order:
|
|
@@ -240,6 +231,265 @@ Book.search('*', sort: ['title:asc'])
|
|
|
240
231
|
|
|
241
232
|
👉 Don't forget to set up the `sortable_attributes` option in the `meilisearch` block of your model.
|
|
242
233
|
|
|
234
|
+
## 🔍🔍 Multi search
|
|
235
|
+
|
|
236
|
+
Meilisearch supports searching multiple models at the same time (see [🔍 Custom search](#-custom-search) for search options):
|
|
237
|
+
|
|
238
|
+
```ruby
|
|
239
|
+
multi_search_results = Meilisearch::Rails.multi_search(
|
|
240
|
+
Book => { q: 'Harry' },
|
|
241
|
+
Manga => { q: 'Attack' }
|
|
242
|
+
)
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
Use `#each_result` to loop through pairs of your provided keys and the results:
|
|
246
|
+
```erb
|
|
247
|
+
<% multi_search_results.each_result do |klass, results| %>
|
|
248
|
+
<p><%= klass.name.pluralize %></p>
|
|
249
|
+
|
|
250
|
+
<ul>
|
|
251
|
+
<% results.each do |record| %>
|
|
252
|
+
<li><%= record.title %></li>
|
|
253
|
+
<% end %>
|
|
254
|
+
</ul>
|
|
255
|
+
<% end %>
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
<p>Books</p>
|
|
259
|
+
<ul>
|
|
260
|
+
<li>Harry Potter and the Philosopher's Stone</li>
|
|
261
|
+
<li>Harry Potter and the Chamber of Secrets</li>
|
|
262
|
+
</ul>
|
|
263
|
+
<p>Mangas</p>
|
|
264
|
+
<ul>
|
|
265
|
+
<li>Attack on Titan</li>
|
|
266
|
+
</ul>
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Records are loaded when the keys are models, or when `:scope` option is passed:
|
|
270
|
+
|
|
271
|
+
```ruby
|
|
272
|
+
multi_search_results = Meilisearch::Rails.multi_search(
|
|
273
|
+
# scope may be a relation
|
|
274
|
+
'books' => { q: 'Harry', scope: Book.all },
|
|
275
|
+
# or a model
|
|
276
|
+
'mangas' => { q: 'Attack', scope: Manga }
|
|
277
|
+
)
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Otherwise, hashes are returned.
|
|
281
|
+
|
|
282
|
+
The index to search is inferred from the model if the key is a model, if the key is a string the key is assumed to be the index unless the `:index_uid` option is passed:
|
|
283
|
+
|
|
284
|
+
```ruby
|
|
285
|
+
multi_search_results = Meilisearch::Rails.multi_search(
|
|
286
|
+
'western' => { q: 'Harry', scope: Book, index_uid: 'books_production' },
|
|
287
|
+
'japanese' => { q: 'Attack', scope: Manga, index_uid: 'mangas_production' }
|
|
288
|
+
)
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Multi search the same index <!-- omit in toc -->
|
|
292
|
+
|
|
293
|
+
You can search the same index multiple times by specifying `:index_uid`:
|
|
294
|
+
|
|
295
|
+
```ruby
|
|
296
|
+
query = 'hero'
|
|
297
|
+
|
|
298
|
+
multi_search_results = Meilisearch::Rails.multi_search(
|
|
299
|
+
'Isekai Manga' => { q: query, scope: Manga, filters: 'genre:isekai', index_uid: 'mangas_production' }
|
|
300
|
+
'Shounen Manga' => { q: query, scope: Manga, filters: 'genre:shounen', index_uid: 'mangas_production' }
|
|
301
|
+
'Steampunk Manga' => { q: query, scope: Manga, filters: 'genre:steampunk', index_uid: 'mangas_production' }
|
|
302
|
+
)
|
|
303
|
+
```
|
|
304
|
+
|
|
305
|
+
### Deprecated #each <!-- omit in toc -->
|
|
306
|
+
|
|
307
|
+
**DEPRECATED:** You used to be able to iterate through a flattened collection with `.each`:
|
|
308
|
+
|
|
309
|
+
```erb
|
|
310
|
+
<% multi_search_results.each do |record| %>
|
|
311
|
+
<p><%= record.title %></p>
|
|
312
|
+
<p><%= record.author %></p>
|
|
313
|
+
<% end %>
|
|
314
|
+
|
|
315
|
+
<p>Harry Potter and the Philosopher's Stone</p>
|
|
316
|
+
<p>J. K. Rowling</p>
|
|
317
|
+
<p>Harry Potter and the Chamber of Secrets</p>
|
|
318
|
+
<p>J. K. Rowling</p>
|
|
319
|
+
<p>Attack on Titan</p>
|
|
320
|
+
<p>Iseyama</p>
|
|
321
|
+
```
|
|
322
|
+
|
|
323
|
+
But this has been deprecated in favor of **federated search**.
|
|
324
|
+
|
|
325
|
+
See the [official multi search documentation](https://www.meilisearch.com/docs/reference/api/multi_search).
|
|
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
|
+
|
|
243
493
|
## 🪛 Options
|
|
244
494
|
|
|
245
495
|
### Meilisearch configuration & environment
|
|
@@ -253,7 +503,7 @@ This gem supports:
|
|
|
253
503
|
Specify the `:pagination_backend` in the configuration file:
|
|
254
504
|
|
|
255
505
|
```ruby
|
|
256
|
-
|
|
506
|
+
Meilisearch::Rails.configuration = {
|
|
257
507
|
meilisearch_url: 'YourMeilisearchUrl',
|
|
258
508
|
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
|
259
509
|
pagination_backend: :kaminari # :will_paginate
|
|
@@ -302,7 +552,7 @@ Then in your model you must extend `Pagy::Meilisearch`:
|
|
|
302
552
|
|
|
303
553
|
```rb
|
|
304
554
|
class Book < ApplicationRecord
|
|
305
|
-
include
|
|
555
|
+
include Meilisearch::Rails
|
|
306
556
|
extend Pagy::Meilisearch
|
|
307
557
|
|
|
308
558
|
meilisearch # ...
|
|
@@ -323,7 +573,7 @@ end
|
|
|
323
573
|
<%== pagy_nav(@pagy) %>
|
|
324
574
|
```
|
|
325
575
|
|
|
326
|
-
:warning: There is no need to set `pagination_backend` in the configuration block `
|
|
576
|
+
:warning: There is no need to set `pagination_backend` in the configuration block `Meilisearch::Rails.configuration` for `pagy`.
|
|
327
577
|
|
|
328
578
|
Check [`ddnexus/pagy`](https://ddnexus.github.io/pagy/extras/meilisearch) for more information.
|
|
329
579
|
|
|
@@ -335,7 +585,7 @@ you have multiple ways to achieve this.
|
|
|
335
585
|
By adding `active: false` in the configuration initializer:
|
|
336
586
|
|
|
337
587
|
```ruby
|
|
338
|
-
|
|
588
|
+
Meilisearch::Rails.configuration = {
|
|
339
589
|
meilisearch_url: 'YourMeilisearchUrl',
|
|
340
590
|
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
|
341
591
|
active: false
|
|
@@ -345,11 +595,11 @@ MeiliSearch::Rails.configuration = {
|
|
|
345
595
|
Or you can disable programmatically:
|
|
346
596
|
|
|
347
597
|
```ruby
|
|
348
|
-
|
|
598
|
+
Meilisearch::Rails.deactivate! # all the following HTTP calls will be dismissed.
|
|
349
599
|
|
|
350
600
|
# or you can pass a block to it:
|
|
351
601
|
|
|
352
|
-
|
|
602
|
+
Meilisearch::Rails.deactivate! do
|
|
353
603
|
# every Meilisearch call here will be dismissed, no error will be raised.
|
|
354
604
|
# after the block, Meilisearch state will be active.
|
|
355
605
|
end
|
|
@@ -358,7 +608,7 @@ end
|
|
|
358
608
|
You can also activate if you deactivated earlier:
|
|
359
609
|
|
|
360
610
|
```ruby
|
|
361
|
-
|
|
611
|
+
Meilisearch::Rails.activate!
|
|
362
612
|
```
|
|
363
613
|
|
|
364
614
|
:warning: These calls are persistent, so prefer to use the method with the block. This way, you will not forget to activate it afterward.
|
|
@@ -369,7 +619,7 @@ By default, the **index_uid** will be the class name, e.g. `Book`. You can custo
|
|
|
369
619
|
|
|
370
620
|
```ruby
|
|
371
621
|
class Book < ActiveRecord::Base
|
|
372
|
-
include
|
|
622
|
+
include Meilisearch::Rails
|
|
373
623
|
|
|
374
624
|
meilisearch index_uid: 'MyCustomUID'
|
|
375
625
|
end
|
|
@@ -380,7 +630,7 @@ end
|
|
|
380
630
|
You can suffix the index UID with the current Rails environment by setting it globally:
|
|
381
631
|
|
|
382
632
|
```ruby
|
|
383
|
-
|
|
633
|
+
Meilisearch::Rails.configuration = {
|
|
384
634
|
meilisearch_url: 'YourMeilisearchUrl',
|
|
385
635
|
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
|
386
636
|
per_environment: true
|
|
@@ -399,7 +649,7 @@ You can add a custom attribute by using the `add_attribute` option or by using a
|
|
|
399
649
|
|
|
400
650
|
```ruby
|
|
401
651
|
class Author < ApplicationRecord
|
|
402
|
-
include
|
|
652
|
+
include Meilisearch::Rails
|
|
403
653
|
|
|
404
654
|
meilisearch do
|
|
405
655
|
attribute :first_name, :last_name
|
|
@@ -431,7 +681,7 @@ Note that the primary key must return a **unique value** otherwise your data cou
|
|
|
431
681
|
|
|
432
682
|
```ruby
|
|
433
683
|
class Book < ActiveRecord::Base
|
|
434
|
-
include
|
|
684
|
+
include Meilisearch::Rails
|
|
435
685
|
|
|
436
686
|
meilisearch primary_key: :isbn # isbn is a column in your table definition.
|
|
437
687
|
end
|
|
@@ -442,7 +692,7 @@ will be used as the reference to the document when Meilisearch needs it.
|
|
|
442
692
|
|
|
443
693
|
```rb
|
|
444
694
|
class Book < ActiveRecord::Base
|
|
445
|
-
include
|
|
695
|
+
include Meilisearch::Rails
|
|
446
696
|
|
|
447
697
|
meilisearch primary_key: :my_custom_ms_id
|
|
448
698
|
|
|
@@ -461,7 +711,7 @@ As soon as you use those constraints, `add_documents` and `delete_documents` cal
|
|
|
461
711
|
|
|
462
712
|
```ruby
|
|
463
713
|
class Book < ActiveRecord::Base
|
|
464
|
-
include
|
|
714
|
+
include Meilisearch::Rails
|
|
465
715
|
|
|
466
716
|
meilisearch if: :published?, unless: :premium?
|
|
467
717
|
|
|
@@ -484,7 +734,7 @@ You can index a record in several indexes using the `add_index` option:
|
|
|
484
734
|
|
|
485
735
|
```ruby
|
|
486
736
|
class Book < ActiveRecord::Base
|
|
487
|
-
include
|
|
737
|
+
include Meilisearch::Rails
|
|
488
738
|
|
|
489
739
|
PUBLIC_INDEX_UID = 'Books'
|
|
490
740
|
SECURED_INDEX_UID = 'PrivateBooks'
|
|
@@ -513,7 +763,7 @@ You may want to share an index between several models. You'll need to ensure you
|
|
|
513
763
|
|
|
514
764
|
```ruby
|
|
515
765
|
class Cat < ActiveRecord::Base
|
|
516
|
-
include
|
|
766
|
+
include Meilisearch::Rails
|
|
517
767
|
|
|
518
768
|
meilisearch index_uid: 'Animals', primary_key: :ms_id
|
|
519
769
|
|
|
@@ -525,7 +775,7 @@ class Cat < ActiveRecord::Base
|
|
|
525
775
|
end
|
|
526
776
|
|
|
527
777
|
class Dog < ActiveRecord::Base
|
|
528
|
-
include
|
|
778
|
+
include Meilisearch::Rails
|
|
529
779
|
|
|
530
780
|
meilisearch index_uid: 'Animals', primary_key: :ms_id
|
|
531
781
|
|
|
@@ -543,7 +793,7 @@ You can configure the auto-indexing & auto-removal process to use a queue to per
|
|
|
543
793
|
|
|
544
794
|
```ruby
|
|
545
795
|
class Book < ActiveRecord::Base
|
|
546
|
-
include
|
|
796
|
+
include Meilisearch::Rails
|
|
547
797
|
|
|
548
798
|
meilisearch enqueue: true # ActiveJob will be triggered using a `meilisearch` queue
|
|
549
799
|
end
|
|
@@ -557,7 +807,7 @@ With **ActiveJob**:
|
|
|
557
807
|
|
|
558
808
|
```ruby
|
|
559
809
|
class Book < ActiveRecord::Base
|
|
560
|
-
include
|
|
810
|
+
include Meilisearch::Rails
|
|
561
811
|
|
|
562
812
|
meilisearch enqueue: :trigger_job do
|
|
563
813
|
attribute :title, :author, :description
|
|
@@ -587,7 +837,7 @@ With [**Sidekiq**](https://github.com/mperham/sidekiq):
|
|
|
587
837
|
|
|
588
838
|
```ruby
|
|
589
839
|
class Book < ActiveRecord::Base
|
|
590
|
-
include
|
|
840
|
+
include Meilisearch::Rails
|
|
591
841
|
|
|
592
842
|
meilisearch enqueue: :trigger_sidekiq_job do
|
|
593
843
|
attribute :title, :author, :description
|
|
@@ -617,7 +867,7 @@ With [**DelayedJob**](https://github.com/collectiveidea/delayed_job):
|
|
|
617
867
|
|
|
618
868
|
```ruby
|
|
619
869
|
class Book < ActiveRecord::Base
|
|
620
|
-
include
|
|
870
|
+
include Meilisearch::Rails
|
|
621
871
|
|
|
622
872
|
meilisearch enqueue: :trigger_delayed_job do
|
|
623
873
|
attribute :title, :author, :description
|
|
@@ -641,7 +891,7 @@ Extend a change to a related record.
|
|
|
641
891
|
|
|
642
892
|
```ruby
|
|
643
893
|
class Author < ActiveRecord::Base
|
|
644
|
-
include
|
|
894
|
+
include Meilisearch::Rails
|
|
645
895
|
|
|
646
896
|
has_many :books
|
|
647
897
|
# If your association uses belongs_to
|
|
@@ -651,7 +901,7 @@ class Author < ActiveRecord::Base
|
|
|
651
901
|
end
|
|
652
902
|
|
|
653
903
|
class Book < ActiveRecord::Base
|
|
654
|
-
include
|
|
904
|
+
include Meilisearch::Rails
|
|
655
905
|
|
|
656
906
|
belongs_to :author
|
|
657
907
|
after_touch :index!
|
|
@@ -670,7 +920,7 @@ With **Sequel**, you can use the `touch` plugin to propagate changes.
|
|
|
670
920
|
```ruby
|
|
671
921
|
# app/models/author.rb
|
|
672
922
|
class Author < Sequel::Model
|
|
673
|
-
include
|
|
923
|
+
include Meilisearch::Rails
|
|
674
924
|
|
|
675
925
|
one_to_many :books
|
|
676
926
|
|
|
@@ -692,7 +942,7 @@ end
|
|
|
692
942
|
|
|
693
943
|
# app/models/book.rb
|
|
694
944
|
class Book < Sequel::Model
|
|
695
|
-
include
|
|
945
|
+
include Meilisearch::Rails
|
|
696
946
|
|
|
697
947
|
many_to_one :author
|
|
698
948
|
after_touch :index!
|
|
@@ -715,7 +965,7 @@ You can strip all HTML tags from your attributes with the `sanitize` option.
|
|
|
715
965
|
|
|
716
966
|
```ruby
|
|
717
967
|
class Book < ActiveRecord::Base
|
|
718
|
-
include
|
|
968
|
+
include Meilisearch::Rails
|
|
719
969
|
|
|
720
970
|
meilisearch sanitize: true
|
|
721
971
|
end
|
|
@@ -727,7 +977,7 @@ You can force the UTF-8 encoding of all your attributes using the `force_utf8_en
|
|
|
727
977
|
|
|
728
978
|
```ruby
|
|
729
979
|
class Book < ActiveRecord::Base
|
|
730
|
-
include
|
|
980
|
+
include Meilisearch::Rails
|
|
731
981
|
|
|
732
982
|
meilisearch force_utf8_encoding: true
|
|
733
983
|
end
|
|
@@ -739,7 +989,7 @@ You can eager load associations using `meilisearch_import` scope.
|
|
|
739
989
|
|
|
740
990
|
```ruby
|
|
741
991
|
class Author < ActiveRecord::Base
|
|
742
|
-
include
|
|
992
|
+
include Meilisearch::Rails
|
|
743
993
|
|
|
744
994
|
has_many :books
|
|
745
995
|
|
|
@@ -792,7 +1042,7 @@ You can disable exceptions that could be raised while trying to reach Meilisearc
|
|
|
792
1042
|
|
|
793
1043
|
```ruby
|
|
794
1044
|
class Book < ActiveRecord::Base
|
|
795
|
-
include
|
|
1045
|
+
include Meilisearch::Rails
|
|
796
1046
|
|
|
797
1047
|
# Only raise exceptions in development environment.
|
|
798
1048
|
meilisearch raise_on_failure: Rails.env.development?
|
|
@@ -807,7 +1057,7 @@ You can force indexing and removing to be synchronous by setting the following o
|
|
|
807
1057
|
|
|
808
1058
|
```ruby
|
|
809
1059
|
class Book < ActiveRecord::Base
|
|
810
|
-
include
|
|
1060
|
+
include Meilisearch::Rails
|
|
811
1061
|
|
|
812
1062
|
meilisearch synchronous: true
|
|
813
1063
|
end
|
|
@@ -820,7 +1070,7 @@ You can disable auto-indexing and auto-removing setting the following options:
|
|
|
820
1070
|
|
|
821
1071
|
```ruby
|
|
822
1072
|
class Book < ActiveRecord::Base
|
|
823
|
-
include
|
|
1073
|
+
include Meilisearch::Rails
|
|
824
1074
|
|
|
825
1075
|
meilisearch auto_index: false, auto_remove: false
|
|
826
1076
|
end
|