meilisearch-rails 0.3.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -2
- data/LICENSE +1 -1
- data/README.md +69 -57
- data/Rakefile +1 -1
- data/lib/meilisearch/rails/configuration.rb +23 -0
- data/lib/meilisearch/rails/errors.rb +14 -0
- data/lib/meilisearch/rails/ms_job.rb +11 -0
- data/lib/meilisearch/rails/pagination/kaminari.rb +48 -0
- data/lib/meilisearch/rails/pagination/will_paginate.rb +22 -0
- data/lib/meilisearch/rails/pagination.rb +20 -0
- data/lib/meilisearch/rails/railtie.rb +14 -0
- data/lib/meilisearch/rails/tasks/meilisearch.rake +22 -0
- data/lib/meilisearch/rails/utilities.rb +46 -0
- data/lib/meilisearch/{version.rb → rails/version.rb} +3 -1
- data/lib/meilisearch-rails.rb +690 -704
- data/meilisearch-rails.gemspec +4 -4
- metadata +14 -14
- data/lib/meilisearch/configuration.rb +0 -21
- data/lib/meilisearch/errors.rb +0 -12
- data/lib/meilisearch/ms_job.rb +0 -9
- data/lib/meilisearch/pagination/kaminari.rb +0 -46
- data/lib/meilisearch/pagination/will_paginate.rb +0 -20
- data/lib/meilisearch/pagination.rb +0 -18
- data/lib/meilisearch/railtie.rb +0 -12
- data/lib/meilisearch/tasks/meilisearch.rake +0 -17
- data/lib/meilisearch/utilities.rb +0 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 914f91a350294c8909a15781828048a0501cb659f782fb22294e189d8f99c20d
|
4
|
+
data.tar.gz: c07ab9397326bed33fca52c556730b38adb4aba84df4ba02d784682280c01a12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 15ce645d1ab372b13af052fea2fdca5a5f9ede5c1f4b11f483ccf3f389bdd21734743c847f858765860fa94f9db02866a09265b710d4068c932242035f203b2c
|
7
|
+
data.tar.gz: db881fdc4cec5de57c7eb294782c35f8a3494e82f181a52d41000cf7270e3d275a75191799341c72f54e2e87921c1ae55b10a8e621aa2ee63c568405fd04acce
|
data/Gemfile
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
source 'http://rubygems.org'
|
2
2
|
|
3
3
|
gem 'json', '~> 2.5', '>= 2.5.1'
|
4
|
-
gem 'meilisearch', '~> 0.
|
4
|
+
gem 'meilisearch', '~> 0.18.0'
|
5
5
|
|
6
6
|
gem 'rubysl', '~> 2.0', platform: :rbx if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
|
7
7
|
|
@@ -28,7 +28,8 @@ group :test do
|
|
28
28
|
gem 'activerecord-jdbc-adapter', platform: :jruby
|
29
29
|
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
|
30
30
|
gem 'jdbc-sqlite3', platform: :jruby
|
31
|
-
gem 'rspec', '
|
31
|
+
gem 'rspec', '~> 3.0'
|
32
|
+
gem 'simplecov', require: 'false'
|
32
33
|
|
33
34
|
gem 'byebug'
|
34
35
|
gem 'dotenv', '~> 2.7', '>= 2.7.6'
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
<p align="center">
|
2
|
-
<img src="https://
|
2
|
+
<img src="https://raw.githubusercontent.com/meilisearch/integration-guides/main/assets/logos/meilisearch_rails.svg" alt="Meilisearch-Rails" width="200" height="200" />
|
3
3
|
</p>
|
4
4
|
|
5
|
-
<h1 align="center">
|
5
|
+
<h1 align="center">Meilisearch Rails</h1>
|
6
6
|
|
7
7
|
<h4 align="center">
|
8
|
-
<a href="https://github.com/meilisearch/
|
8
|
+
<a href="https://github.com/meilisearch/meilisearch">Meilisearch</a> |
|
9
9
|
<a href="https://docs.meilisearch.com">Documentation</a> |
|
10
10
|
<a href="https://slack.meilisearch.com">Slack</a> |
|
11
11
|
<a href="https://roadmap.meilisearch.com/tabs/1-under-consideration">Roadmap</a> |
|
@@ -19,21 +19,21 @@
|
|
19
19
|
<a href="https://app.bors.tech/repositories/33032"><img src="https://bors.tech/images/badge_small.svg" alt="Bors enabled"></a>
|
20
20
|
</p>
|
21
21
|
|
22
|
-
<p align="center">⚡ The
|
22
|
+
<p align="center">⚡ The Meilisearch integration for Ruby on Rails 💎</p>
|
23
23
|
|
24
|
-
**
|
24
|
+
**Meilisearch Rails** is the Meilisearch integration for Ruby on Rails developers.
|
25
25
|
|
26
|
-
**
|
26
|
+
**Meilisearch** is an open-source search engine. [Discover what Meilisearch is!](https://github.com/meilisearch/meilisearch)
|
27
27
|
|
28
28
|
## Table of Contents <!-- omit in toc -->
|
29
29
|
|
30
30
|
- [📖 Documentation](#-documentation)
|
31
|
-
- [🤖 Compatibility with
|
31
|
+
- [🤖 Compatibility with Meilisearch](#-compatibility-with-meilisearch)
|
32
32
|
- [🚀 Getting Started](#-getting-started)
|
33
33
|
- [⚙️ Settings](#️-settings)
|
34
34
|
- [🔍 Custom search](#-custom-search)
|
35
35
|
- [🪛 Options](#-options)
|
36
|
-
- [
|
36
|
+
- [Meilisearch configuration & environment](#meilisearch-configuration--environment)
|
37
37
|
- [Index configuration](#index-configuration)
|
38
38
|
- [Custom attribute definition](#custom-attribute-definition)
|
39
39
|
- [Custom primary key](#custom-primary-key)
|
@@ -54,11 +54,11 @@
|
|
54
54
|
|
55
55
|
The whole usage of this gem is detailed in this README.
|
56
56
|
|
57
|
-
To learn more about
|
57
|
+
To learn more about Meilisearch, check out our [Documentation](https://docs.meilisearch.com/learn/tutorials/getting_started.html) or our [API References](https://docs.meilisearch.com/reference/api/).
|
58
58
|
|
59
|
-
## 🤖 Compatibility with
|
59
|
+
## 🤖 Compatibility with Meilisearch
|
60
60
|
|
61
|
-
This package only guarantees the compatibility with the [version v0.
|
61
|
+
This package only guarantees the compatibility with the [version v0.25.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.25.0).
|
62
62
|
|
63
63
|
## 🔧 Installation <!-- omit in toc -->
|
64
64
|
|
@@ -76,18 +76,18 @@ source 'https://rubygems.org'
|
|
76
76
|
gem 'meilisearch-rails'
|
77
77
|
```
|
78
78
|
|
79
|
-
### Run
|
79
|
+
### Run Meilisearch <!-- omit in toc -->
|
80
80
|
|
81
|
-
There are many easy ways to [download and run a
|
81
|
+
There are many easy ways to [download and run a Meilisearch instance](https://docs.meilisearch.com/reference/features/installation.html#download-and-launch).
|
82
82
|
|
83
83
|
For example, if you use Docker:
|
84
84
|
|
85
85
|
```bash
|
86
|
-
docker pull getmeili/meilisearch:latest # Fetch the latest version of
|
86
|
+
docker pull getmeili/meilisearch:latest # Fetch the latest version of Meilisearch image from Docker Hub
|
87
87
|
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
|
88
88
|
```
|
89
89
|
|
90
|
-
NB: you can also download
|
90
|
+
NB: you can also download Meilisearch from **Homebrew** or **APT**.
|
91
91
|
|
92
92
|
## 🚀 Getting Started
|
93
93
|
|
@@ -96,9 +96,9 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
|
|
96
96
|
Create a new file `config/initializers/meilisearch.rb` to setup your `MEILISEARCH_HOST` and `MEILISEARCH_API_KEY`
|
97
97
|
|
98
98
|
```ruby
|
99
|
-
MeiliSearch.configuration = {
|
100
|
-
meilisearch_host: '
|
101
|
-
meilisearch_api_key: '
|
99
|
+
MeiliSearch::Rails.configuration = {
|
100
|
+
meilisearch_host: 'YourMeilisearchHost', # example: http://localhost:7700
|
101
|
+
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
102
102
|
}
|
103
103
|
```
|
104
104
|
|
@@ -110,11 +110,11 @@ The following code will create a `Book` index and add search capabilities to you
|
|
110
110
|
|
111
111
|
```ruby
|
112
112
|
class Book < ActiveRecord::Base
|
113
|
-
include MeiliSearch
|
113
|
+
include MeiliSearch::Rails
|
114
114
|
|
115
115
|
meilisearch do
|
116
|
-
attribute :title, :author # only the attributes 'title', and 'author' will be sent to
|
117
|
-
# all attributes will be sent to
|
116
|
+
attribute :title, :author # only the attributes 'title', and 'author' will be sent to Meilisearch
|
117
|
+
# all attributes will be sent to Meilisearch if block is left empty
|
118
118
|
end
|
119
119
|
end
|
120
120
|
```
|
@@ -128,7 +128,7 @@ We **strongly recommend the use of front-end search** through our [JavaScript AP
|
|
128
128
|
Search returns ORM-compliant objects reloaded from your database.
|
129
129
|
|
130
130
|
```ruby
|
131
|
-
#
|
131
|
+
# Meilisearch is typo-tolerant:
|
132
132
|
hits = Book.search('harry pottre')
|
133
133
|
hits.each do |hit|
|
134
134
|
puts hit.title
|
@@ -146,9 +146,9 @@ This gem supports:
|
|
146
146
|
Specify the `:pagination_backend` in the configuration file:
|
147
147
|
|
148
148
|
```ruby
|
149
|
-
MeiliSearch.configuration = {
|
150
|
-
meilisearch_host: '
|
151
|
-
meilisearch_api_key: '
|
149
|
+
MeiliSearch::Rails.configuration = {
|
150
|
+
meilisearch_host: 'YourMeilisearchHost',
|
151
|
+
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
152
152
|
pagination_backend: :kaminari #:will_paginate
|
153
153
|
}
|
154
154
|
```
|
@@ -178,13 +178,13 @@ Book.search('harry potter', hitsPerPage: 10)
|
|
178
178
|
|
179
179
|
#### Extra Configuration <!-- omit in toc -->
|
180
180
|
|
181
|
-
Requests made to
|
181
|
+
Requests made to Meilisearch may timeout and retry. To adapt the behavior to
|
182
182
|
your needs, you can change the parameters during configuration:
|
183
183
|
|
184
184
|
```ruby
|
185
|
-
MeiliSearch.configuration = {
|
186
|
-
meilisearch_host: '
|
187
|
-
meilisearch_api_key: '
|
185
|
+
MeiliSearch::Rails.configuration = {
|
186
|
+
meilisearch_host: 'YourMeilisearchHost',
|
187
|
+
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
188
188
|
timeout: 2,
|
189
189
|
max_retries: 1,
|
190
190
|
}
|
@@ -196,11 +196,12 @@ You can configure the index settings by adding them inside the `meilisearch` blo
|
|
196
196
|
|
197
197
|
```ruby
|
198
198
|
class Book < ApplicationRecord
|
199
|
-
include MeiliSearch
|
199
|
+
include MeiliSearch::Rails
|
200
200
|
|
201
201
|
meilisearch do
|
202
202
|
searchable_attributes [:title, :author, :publisher, :description]
|
203
203
|
filterable_attributes [:genre]
|
204
|
+
sortable_attributes [:title]
|
204
205
|
ranking_rules [
|
205
206
|
'proximity',
|
206
207
|
'typo',
|
@@ -232,9 +233,20 @@ Book.search('Harry', attributesToHighlight: ['*'])
|
|
232
233
|
👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
|
233
234
|
`crop_length` can be set up in the `meilisearch` block of your model.
|
234
235
|
|
236
|
+
## 🔍 Sorted search
|
237
|
+
|
238
|
+
As an example of how to use the sort option, here is how you could achieve
|
239
|
+
returning all books sorted by title in ascending order:
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
Book.search('*', sort: ['title:asc'])
|
243
|
+
```
|
244
|
+
|
245
|
+
👉 Don't forget to set up the `sortable_attributes` option in the `meilisearch` block of your model.
|
246
|
+
|
235
247
|
## 🪛 Options
|
236
248
|
|
237
|
-
###
|
249
|
+
### Meilisearch configuration & environment
|
238
250
|
|
239
251
|
#### Custom index_uid <!-- omit in toc -->
|
240
252
|
|
@@ -242,7 +254,7 @@ By default, the **index_uid** will be the class name, e.g. `Book`. You can custo
|
|
242
254
|
|
243
255
|
```ruby
|
244
256
|
class Book < ActiveRecord::Base
|
245
|
-
include MeiliSearch
|
257
|
+
include MeiliSearch::Rails
|
246
258
|
|
247
259
|
meilisearch index_uid: 'MyCustomUID' do
|
248
260
|
end
|
@@ -255,7 +267,7 @@ You can suffix the index UID with the current Rails environment using the follow
|
|
255
267
|
|
256
268
|
```ruby
|
257
269
|
class Book < ActiveRecord::Base
|
258
|
-
include MeiliSearch
|
270
|
+
include MeiliSearch::Rails
|
259
271
|
|
260
272
|
meilisearch per_environment: true do # The index UID will be "Book_#{Rails.env}"
|
261
273
|
end
|
@@ -272,7 +284,7 @@ You can add a custom attribute by using the `add_attribute` option or by using a
|
|
272
284
|
|
273
285
|
```ruby
|
274
286
|
class Author < ApplicationRecord
|
275
|
-
include MeiliSearch
|
287
|
+
include MeiliSearch::Rails
|
276
288
|
|
277
289
|
meilisearch do
|
278
290
|
attribute :first_name, :last_name
|
@@ -304,7 +316,7 @@ Note that the primary key must have a **unique value**.
|
|
304
316
|
|
305
317
|
```ruby
|
306
318
|
class Book < ActiveRecord::Base
|
307
|
-
include MeiliSearch
|
319
|
+
include MeiliSearch::Rails
|
308
320
|
|
309
321
|
meilisearch primary_key: 'ISBN' do
|
310
322
|
end
|
@@ -317,7 +329,7 @@ As soon as you use those constraints, `add_documents` and `delete_documents` cal
|
|
317
329
|
|
318
330
|
```ruby
|
319
331
|
class Book < ActiveRecord::Base
|
320
|
-
include MeiliSearch
|
332
|
+
include MeiliSearch::Rails
|
321
333
|
|
322
334
|
meilisearch if: :published?, unless: :premium? do
|
323
335
|
end
|
@@ -341,7 +353,7 @@ You can index a record in several indexes using the `add_index` option:
|
|
341
353
|
|
342
354
|
```ruby
|
343
355
|
class Book < ActiveRecord::Base
|
344
|
-
include MeiliSearch
|
356
|
+
include MeiliSearch::Rails
|
345
357
|
|
346
358
|
PUBLIC_INDEX_UID = 'Books'
|
347
359
|
SECURED_INDEX_UID = 'PrivateBooks'
|
@@ -369,7 +381,7 @@ You may want to share an index between several models. You'll need to ensure you
|
|
369
381
|
|
370
382
|
```ruby
|
371
383
|
class Cat < ActiveRecord::Base
|
372
|
-
include MeiliSearch
|
384
|
+
include MeiliSearch::Rails
|
373
385
|
|
374
386
|
meilisearch index_uid: 'Animals', primary_key: :ms_id do
|
375
387
|
end
|
@@ -381,7 +393,7 @@ class Cat < ActiveRecord::Base
|
|
381
393
|
end
|
382
394
|
|
383
395
|
class Dog < ActiveRecord::Base
|
384
|
-
include MeiliSearch
|
396
|
+
include MeiliSearch::Rails
|
385
397
|
|
386
398
|
meilisearch index_uid: 'Animals', primary_key: :ms_id do
|
387
399
|
end
|
@@ -399,7 +411,7 @@ You can configure the auto-indexing & auto-removal process to use a queue to per
|
|
399
411
|
|
400
412
|
```ruby
|
401
413
|
class Book < ActiveRecord::Base
|
402
|
-
include MeiliSearch
|
414
|
+
include MeiliSearch::Rails
|
403
415
|
|
404
416
|
meilisearch enqueue: true do # ActiveJob will be triggered using a `meilisearch` queue
|
405
417
|
end
|
@@ -414,7 +426,7 @@ With **ActiveJob**:
|
|
414
426
|
|
415
427
|
```ruby
|
416
428
|
class Book < ActiveRecord::Base
|
417
|
-
include MeiliSearch
|
429
|
+
include MeiliSearch::Rails
|
418
430
|
|
419
431
|
meilisearch enqueue: :trigger_job do
|
420
432
|
attribute :title, :author, :description
|
@@ -430,7 +442,7 @@ class MyActiveJob < ApplicationJob
|
|
430
442
|
if remove
|
431
443
|
# The record has likely already been removed from your database so we cannot
|
432
444
|
# use ActiveRecord#find to load it.
|
433
|
-
# We access the underlying
|
445
|
+
# We access the underlying Meilisearch index object.
|
434
446
|
Book.index.delete_document(id)
|
435
447
|
else
|
436
448
|
# The record should be present.
|
@@ -444,7 +456,7 @@ With [**Sidekiq**](https://github.com/mperham/sidekiq):
|
|
444
456
|
|
445
457
|
```ruby
|
446
458
|
class Book < ActiveRecord::Base
|
447
|
-
include MeiliSearch
|
459
|
+
include MeiliSearch::Rails
|
448
460
|
|
449
461
|
meilisearch enqueue: :trigger_sidekiq_worker do
|
450
462
|
attribute :title, :author, :description
|
@@ -460,7 +472,7 @@ class MySidekiqWorker
|
|
460
472
|
if remove
|
461
473
|
# The record has likely already been removed from your database so we cannot
|
462
474
|
# use ActiveRecord#find to load it.
|
463
|
-
# We access the underlying
|
475
|
+
# We access the underlying Meilisearch index object.
|
464
476
|
Book.index.delete_document(id)
|
465
477
|
else
|
466
478
|
# The record should be present.
|
@@ -474,7 +486,7 @@ With [**DelayedJob**](https://github.com/collectiveidea/delayed_job):
|
|
474
486
|
|
475
487
|
```ruby
|
476
488
|
class Book < ActiveRecord::Base
|
477
|
-
include MeiliSearch
|
489
|
+
include MeiliSearch::Rails
|
478
490
|
|
479
491
|
meilisearch enqueue: :trigger_delayed_job do
|
480
492
|
attribute :title, :author, :description
|
@@ -498,7 +510,7 @@ Extend a change to a related record.
|
|
498
510
|
|
499
511
|
```ruby
|
500
512
|
class Author < ActiveRecord::Base
|
501
|
-
include MeiliSearch
|
513
|
+
include MeiliSearch::Rails
|
502
514
|
|
503
515
|
has_many :books
|
504
516
|
# If your association uses belongs_to
|
@@ -508,7 +520,7 @@ class Author < ActiveRecord::Base
|
|
508
520
|
end
|
509
521
|
|
510
522
|
class Book < ActiveRecord::Base
|
511
|
-
include MeiliSearch
|
523
|
+
include MeiliSearch::Rails
|
512
524
|
|
513
525
|
belongs_to :author
|
514
526
|
after_touch :index!
|
@@ -527,7 +539,7 @@ With **Sequel**, you can use the `touch` plugin to propagate changes.
|
|
527
539
|
```ruby
|
528
540
|
# app/models/author.rb
|
529
541
|
class Author < Sequel::Model
|
530
|
-
include MeiliSearch
|
542
|
+
include MeiliSearch::Rails
|
531
543
|
|
532
544
|
one_to_many :books
|
533
545
|
|
@@ -549,7 +561,7 @@ end
|
|
549
561
|
|
550
562
|
# app/models/book.rb
|
551
563
|
class Book < Sequel::Model
|
552
|
-
include MeiliSearch
|
564
|
+
include MeiliSearch::Rails
|
553
565
|
|
554
566
|
many_to_one :author
|
555
567
|
after_touch :index!
|
@@ -572,7 +584,7 @@ You can strip all HTML tags from your attributes with the `sanitize` option.
|
|
572
584
|
|
573
585
|
```ruby
|
574
586
|
class Book < ActiveRecord::Base
|
575
|
-
include MeiliSearch
|
587
|
+
include MeiliSearch::Rails
|
576
588
|
|
577
589
|
meilisearch sanitize: true do
|
578
590
|
end
|
@@ -585,7 +597,7 @@ You can force the UTF-8 encoding of all your attributes using the `force_utf8_en
|
|
585
597
|
|
586
598
|
```ruby
|
587
599
|
class Book < ActiveRecord::Base
|
588
|
-
include MeiliSearch
|
600
|
+
include MeiliSearch::Rails
|
589
601
|
|
590
602
|
meilisearch force_utf8_encoding: true do
|
591
603
|
end
|
@@ -633,11 +645,11 @@ index = Book.index
|
|
633
645
|
|
634
646
|
#### Exceptions <!-- omit in toc -->
|
635
647
|
|
636
|
-
You can disable exceptions that could be raised while trying to reach
|
648
|
+
You can disable exceptions that could be raised while trying to reach Meilisearch's API by using the `raise_on_failure` option:
|
637
649
|
|
638
650
|
```ruby
|
639
651
|
class Book < ActiveRecord::Base
|
640
|
-
include MeiliSearch
|
652
|
+
include MeiliSearch::Rails
|
641
653
|
|
642
654
|
# Only raise exceptions in development environment.
|
643
655
|
meilisearch raise_on_failure: Rails.env.development? do
|
@@ -653,13 +665,13 @@ You can force indexing and removing to be synchronous by setting the following o
|
|
653
665
|
|
654
666
|
```ruby
|
655
667
|
class Book < ActiveRecord::Base
|
656
|
-
include MeiliSearch
|
668
|
+
include MeiliSearch::Rails
|
657
669
|
|
658
670
|
meilisearch synchronous: true do
|
659
671
|
end
|
660
672
|
end
|
661
673
|
```
|
662
|
-
🚨 This is only recommended for testing purposes, the gem will call the `
|
674
|
+
🚨 This is only recommended for testing purposes, the gem will call the `wait_for_task` method that will stop your code execution until the asynchronous task has been processed by MeilSearch.
|
663
675
|
|
664
676
|
##### Disable auto-indexing & auto-removal <!-- omit in toc -->
|
665
677
|
|
@@ -667,7 +679,7 @@ You can disable auto-indexing and auto-removing setting the following options:
|
|
667
679
|
|
668
680
|
```ruby
|
669
681
|
class Book < ActiveRecord::Base
|
670
|
-
include MeiliSearch
|
682
|
+
include MeiliSearch::Rails
|
671
683
|
|
672
684
|
meilisearch auto_index: false, auto_remove: false do
|
673
685
|
end
|
@@ -695,4 +707,4 @@ The provided features and the code base is inspired by [algoliasearch-rails](htt
|
|
695
707
|
|
696
708
|
<hr>
|
697
709
|
|
698
|
-
**
|
710
|
+
**Meilisearch** provides and maintains many **SDKs and Integration tools** like this one. We want to provide everyone with an **amazing search experience for any kind of project**. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the [integration-guides](https://github.com/meilisearch/integration-guides) repository.
|
data/Rakefile
CHANGED
@@ -7,7 +7,7 @@ Rake::RDocTask.new do |rdoc|
|
|
7
7
|
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
8
8
|
|
9
9
|
rdoc.rdoc_dir = 'rdoc'
|
10
|
-
rdoc.title = "
|
10
|
+
rdoc.title = "Meilisearch Rails #{version}"
|
11
11
|
rdoc.rdoc_files.include('README*')
|
12
12
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
13
13
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module MeiliSearch
|
2
|
+
module Rails
|
3
|
+
module Configuration
|
4
|
+
def configuration
|
5
|
+
raise NotConfigured if @_config.blank?
|
6
|
+
|
7
|
+
@_config
|
8
|
+
end
|
9
|
+
|
10
|
+
def configuration=(configuration)
|
11
|
+
@_config = configuration
|
12
|
+
end
|
13
|
+
|
14
|
+
def client
|
15
|
+
::MeiliSearch::Client.new(
|
16
|
+
configuration[:meilisearch_host] || 'http://localhost:7700',
|
17
|
+
configuration[:meilisearch_api_key],
|
18
|
+
configuration.slice(:timeout, :max_retries)
|
19
|
+
)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module MeiliSearch
|
2
|
+
module Rails
|
3
|
+
class NoBlockGiven < StandardError; end
|
4
|
+
|
5
|
+
class BadConfiguration < StandardError; end
|
6
|
+
|
7
|
+
class NotConfigured < StandardError
|
8
|
+
def message
|
9
|
+
'Please configure Meilisearch. Set MeiliSearch::Rails.configuration = ' \
|
10
|
+
"{meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
unless defined? Kaminari
|
2
|
+
raise(MeiliSearch::BadConfiguration,
|
3
|
+
"Meilisearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
|
4
|
+
end
|
5
|
+
|
6
|
+
require 'kaminari/models/array_extension'
|
7
|
+
|
8
|
+
module MeiliSearch
|
9
|
+
module Rails
|
10
|
+
module Pagination
|
11
|
+
class Kaminari < ::Kaminari::PaginatableArray
|
12
|
+
def initialize(array, options)
|
13
|
+
if RUBY_VERSION >= '3'
|
14
|
+
super(array, **options)
|
15
|
+
else
|
16
|
+
super(array, options)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def limit(_num)
|
21
|
+
# noop
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
def offset(_num)
|
26
|
+
# noop
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
class << self
|
31
|
+
def create(results, total_hits, options = {})
|
32
|
+
offset = ((options[:page] - 1) * options[:per_page])
|
33
|
+
array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
|
34
|
+
|
35
|
+
if array.empty? && !results.empty?
|
36
|
+
# since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
|
37
|
+
# otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
|
38
|
+
results = ([nil] * offset) + results
|
39
|
+
array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
|
40
|
+
end
|
41
|
+
|
42
|
+
array
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
begin
|
2
|
+
require 'will_paginate/collection'
|
3
|
+
rescue LoadError
|
4
|
+
raise(MeiliSearch::BadConfiguration,
|
5
|
+
"MeiliSearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
|
6
|
+
end
|
7
|
+
|
8
|
+
module MeiliSearch
|
9
|
+
module Rails
|
10
|
+
module Pagination
|
11
|
+
class WillPaginate
|
12
|
+
def self.create(results, total_hits, options = {})
|
13
|
+
::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
|
14
|
+
start = (options[:page] - 1) * options[:per_page]
|
15
|
+
paginated_results = results[start, options[:per_page]]
|
16
|
+
pager.replace paginated_results
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module MeiliSearch
|
2
|
+
module Rails
|
3
|
+
module Pagination
|
4
|
+
autoload :WillPaginate, 'meilisearch/rails/pagination/will_paginate'
|
5
|
+
autoload :Kaminari, 'meilisearch/rails/pagination/kaminari'
|
6
|
+
|
7
|
+
def self.create(results, total_hits, options = {})
|
8
|
+
return results if MeiliSearch::Rails.configuration[:pagination_backend].nil?
|
9
|
+
|
10
|
+
begin
|
11
|
+
backend = MeiliSearch::Rails.configuration[:pagination_backend].to_s.classify
|
12
|
+
|
13
|
+
::MeiliSearch::Rails::Pagination.const_get(backend).create(results, total_hits, options)
|
14
|
+
rescue NameError
|
15
|
+
raise(BadConfiguration, 'Unknown pagination backend')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
namespace :meilisearch do
|
2
|
+
desc 'Reindex all models'
|
3
|
+
task reindex: :environment do
|
4
|
+
puts 'Reindexing all Meilisearch models'
|
5
|
+
|
6
|
+
MeiliSearch::Rails::Utilities.reindex_all_models
|
7
|
+
end
|
8
|
+
|
9
|
+
desc 'Set settings to all indexes'
|
10
|
+
task set_all_settings: :environment do
|
11
|
+
puts 'Set settings in all Meilisearch models'
|
12
|
+
|
13
|
+
MeiliSearch::Rails::Utilities.set_settings_all_models
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Clear all indexes'
|
17
|
+
task clear_indexes: :environment do
|
18
|
+
puts 'Clearing indexes from all Meilisearch models'
|
19
|
+
|
20
|
+
MeiliSearch::Rails::Utilities.clear_all_indexes
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module MeiliSearch
|
2
|
+
module Rails
|
3
|
+
module Utilities
|
4
|
+
class << self
|
5
|
+
def get_model_classes
|
6
|
+
if ::Rails.application && defined?(::Rails.autoloaders) && ::Rails.autoloaders.zeitwerk_enabled?
|
7
|
+
Zeitwerk::Loader.eager_load_all
|
8
|
+
elsif ::Rails.application
|
9
|
+
::Rails.application.eager_load!
|
10
|
+
end
|
11
|
+
klasses = MeiliSearch::Rails.instance_variable_get(:@included_in)
|
12
|
+
(klasses + klasses.map(&:descendants).flatten).uniq
|
13
|
+
end
|
14
|
+
|
15
|
+
def clear_all_indexes
|
16
|
+
get_model_classes.each(&:clear_index!)
|
17
|
+
end
|
18
|
+
|
19
|
+
def reindex_all_models
|
20
|
+
klasses = get_model_classes
|
21
|
+
|
22
|
+
::Rails.logger.info "\n\nReindexing #{klasses.count} models: #{klasses.to_sentence}.\n"
|
23
|
+
|
24
|
+
klasses.each do |klass|
|
25
|
+
::Rails.logger.info klass
|
26
|
+
::Rails.logger.info "Reindexing #{klass.count} records..."
|
27
|
+
|
28
|
+
klass.ms_reindex!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def set_settings_all_models
|
33
|
+
klasses = get_model_classes
|
34
|
+
|
35
|
+
::Rails.logger.info "\n\nPushing settings for #{klasses.count} models: #{klasses.to_sentence}.\n"
|
36
|
+
|
37
|
+
klasses.each do |klass|
|
38
|
+
::Rails.logger.info "Pushing #{klass} settings..."
|
39
|
+
|
40
|
+
klass.ms_set_settings
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|