meilisearch-rails 0.2.3 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +25 -17
- data/LICENSE +1 -1
- data/README.md +86 -80
- data/Rakefile +5 -4
- 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/rails/version.rb +7 -0
- data/lib/meilisearch-rails.rb +674 -671
- data/meilisearch-rails.gemspec +27 -37
- metadata +15 -16
- data/lib/meilisearch/configuration.rb +0 -19
- data/lib/meilisearch/ms_job.rb +0 -9
- data/lib/meilisearch/pagination/kaminari.rb +0 -44
- data/lib/meilisearch/pagination/will_paginate.rb +0 -19
- data/lib/meilisearch/pagination.rb +0 -19
- data/lib/meilisearch/railtie.rb +0 -11
- data/lib/meilisearch/tasks/meilisearch.rake +0 -19
- data/lib/meilisearch/utilities.rb +0 -49
- data/lib/meilisearch/version.rb +0 -3
- data/spec/spec_helper.rb +0 -52
- data/spec/utilities_spec.rb +0 -36
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ad6c51de59755b283d9371028f127537f8eae862603d65469f3867e7e62b6ec
|
4
|
+
data.tar.gz: a2ef9d38103f8e2750e687f1fae309818b210576b63c6951b913b7856c713598
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5720f087c6c85aa34b553ba01614968fe4862a416c6e72c1829b4107470862a30db4e4107455d2a45ab94497ff69b084460196f6173c4f584aa1364769a0564a
|
7
|
+
data.tar.gz: 5cebff7daa2d2caf3ed62e9a49fe4f3f17d4ccf2b6dcfed90f92645fdac4c7d8adddea521aff563aca9f454942529b1444c9fea092fd329e2cfda187ef885eba
|
data/Gemfile
CHANGED
@@ -1,31 +1,39 @@
|
|
1
|
-
source
|
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
|
-
if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
|
7
|
-
|
6
|
+
gem 'rubysl', '~> 2.0', platform: :rbx if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
|
7
|
+
|
8
|
+
group :development do
|
9
|
+
gem 'rubocop', '~> 1.22'
|
10
|
+
gem 'rubocop-rails'
|
11
|
+
gem 'rubocop-rspec'
|
8
12
|
end
|
9
13
|
|
10
14
|
group :test do
|
11
|
-
rails_version = ENV[
|
12
|
-
|
15
|
+
rails_version = ENV['RAILS_VERSION'] || '5.2'
|
16
|
+
sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
|
17
|
+
|
13
18
|
gem 'active_model_serializers'
|
19
|
+
gem 'rails', "~> #{rails_version}"
|
20
|
+
gem 'sequel', sequel_version
|
21
|
+
|
14
22
|
if Gem::Version.new(rails_version) >= Gem::Version.new('6.0')
|
15
|
-
gem 'sqlite3', '~> 1.4.0', :
|
23
|
+
gem 'sqlite3', '~> 1.4.0', platform: %i[rbx ruby]
|
16
24
|
else
|
17
|
-
gem 'sqlite3', '< 1.4.0', :
|
25
|
+
gem 'sqlite3', '< 1.4.0', platform: %i[rbx ruby]
|
18
26
|
end
|
19
|
-
gem 'rspec', '>= 2.5.0', '< 3.0'
|
20
|
-
gem 'jdbc-sqlite3', :platform => :jruby
|
21
|
-
gem 'activerecord-jdbc-adapter', :platform => :jruby
|
22
|
-
gem 'activerecord-jdbcsqlite3-adapter', :platform => :jruby
|
23
27
|
|
24
|
-
|
25
|
-
gem '
|
28
|
+
gem 'activerecord-jdbc-adapter', platform: :jruby
|
29
|
+
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
|
30
|
+
gem 'jdbc-sqlite3', platform: :jruby
|
31
|
+
gem 'rspec', '~> 3.0'
|
32
|
+
gem 'simplecov', require: 'false'
|
33
|
+
|
34
|
+
gem 'byebug'
|
35
|
+
gem 'dotenv', '~> 2.7', '>= 2.7.6'
|
26
36
|
gem 'faker', '~> 2.17'
|
27
|
-
gem 'will_paginate', '>= 2.3.15'
|
28
37
|
gem 'kaminari'
|
29
|
-
gem '
|
30
|
-
gem 'byebug'
|
38
|
+
gem 'will_paginate', '>= 2.3.15'
|
31
39
|
end
|
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,55 +19,48 @@
|
|
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
|
32
|
-
- [
|
33
|
-
- [
|
31
|
+
- [🤖 Compatibility with Meilisearch](#-compatibility-with-meilisearch)
|
32
|
+
- [🚀 Getting Started](#-getting-started)
|
33
|
+
- [⚙️ Settings](#️-settings)
|
34
34
|
- [🔍 Custom search](#-custom-search)
|
35
35
|
- [🪛 Options](#-options)
|
36
|
-
- [
|
37
|
-
- [Custom index_uid](#custom-index_uid)
|
38
|
-
- [Per-environment index_uid](#per-environment-index_uid)
|
36
|
+
- [Meilisearch configuration & environment](#meilisearch-configuration--environment)
|
39
37
|
- [Index configuration](#index-configuration)
|
40
38
|
- [Custom attribute definition](#custom-attribute-definition)
|
41
39
|
- [Custom primary key](#custom-primary-key)
|
42
40
|
- [Conditional indexing](#conditional-indexing)
|
43
|
-
- [Target multiple indexes](#target-multiple-indexes)
|
44
41
|
- [Share a single index](#share-a-single-index)
|
45
|
-
- [Queues & background jobs](#queues
|
42
|
+
- [Queues & background jobs](#queues--background-jobs)
|
46
43
|
- [Relations](#relations)
|
47
44
|
- [Sanitize attributes](#sanitize-attributes)
|
48
45
|
- [UTF-8 encoding](#utf-8-encoding)
|
49
46
|
- [Manual operations](#manual-operations)
|
50
|
-
- [Indexing & deletion](#indexing
|
47
|
+
- [Indexing & deletion](#indexing--deletion)
|
51
48
|
- [Access the underlying index object](#access-the-underlying-index-object)
|
52
|
-
- [Development & testing](#development
|
53
|
-
|
54
|
-
|
55
|
-
- [Synchronous testing](#synchronous-testing)
|
56
|
-
- [Disable auto-indexing & auto-removal](#disable-auto-indexing-&-auto-removal)
|
57
|
-
- [⚙️ Development Workflow and Contributing](#️-development-workflow-and-contributing)
|
58
|
-
- [👏 Credits](#-credits)
|
49
|
+
- [Development & testing](#development--testing)
|
50
|
+
- [⚙️ Development workflow & contributing](#️-development-workflow--contributing)
|
51
|
+
- [👏 Credits](#--credits)
|
59
52
|
|
60
53
|
## 📖 Documentation
|
61
54
|
|
62
55
|
The whole usage of this gem is detailed in this README.
|
63
56
|
|
64
|
-
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/).
|
65
58
|
|
66
|
-
## 🤖 Compatibility with
|
59
|
+
## 🤖 Compatibility with Meilisearch
|
67
60
|
|
68
|
-
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).
|
69
62
|
|
70
|
-
## 🔧 Installation
|
63
|
+
## 🔧 Installation <!-- omit in toc -->
|
71
64
|
|
72
65
|
This package requires Ruby version 2.6.0 or later and Rails 5.2 or later.
|
73
66
|
|
@@ -83,18 +76,18 @@ source 'https://rubygems.org'
|
|
83
76
|
gem 'meilisearch-rails'
|
84
77
|
```
|
85
78
|
|
86
|
-
### Run
|
79
|
+
### Run Meilisearch <!-- omit in toc -->
|
87
80
|
|
88
|
-
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).
|
89
82
|
|
90
83
|
For example, if you use Docker:
|
91
84
|
|
92
85
|
```bash
|
93
|
-
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
|
94
87
|
docker run -it --rm -p 7700:7700 getmeili/meilisearch:latest ./meilisearch --master-key=masterKey
|
95
88
|
```
|
96
89
|
|
97
|
-
NB: you can also download
|
90
|
+
NB: you can also download Meilisearch from **Homebrew** or **APT**.
|
98
91
|
|
99
92
|
## 🚀 Getting Started
|
100
93
|
|
@@ -103,9 +96,9 @@ NB: you can also download MeiliSearch from **Homebrew** or **APT**.
|
|
103
96
|
Create a new file `config/initializers/meilisearch.rb` to setup your `MEILISEARCH_HOST` and `MEILISEARCH_API_KEY`
|
104
97
|
|
105
98
|
```ruby
|
106
|
-
MeiliSearch.configuration = {
|
107
|
-
meilisearch_host: '
|
108
|
-
meilisearch_api_key: '
|
99
|
+
MeiliSearch::Rails.configuration = {
|
100
|
+
meilisearch_host: 'YourMeilisearchHost', # example: http://localhost:7700
|
101
|
+
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
109
102
|
}
|
110
103
|
```
|
111
104
|
|
@@ -117,11 +110,11 @@ The following code will create a `Book` index and add search capabilities to you
|
|
117
110
|
|
118
111
|
```ruby
|
119
112
|
class Book < ActiveRecord::Base
|
120
|
-
include MeiliSearch
|
113
|
+
include MeiliSearch::Rails
|
121
114
|
|
122
115
|
meilisearch do
|
123
|
-
attribute :title, :author # only the attributes 'title', and 'author' will be sent to
|
124
|
-
# 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
|
125
118
|
end
|
126
119
|
end
|
127
120
|
```
|
@@ -135,7 +128,7 @@ We **strongly recommend the use of front-end search** through our [JavaScript AP
|
|
135
128
|
Search returns ORM-compliant objects reloaded from your database.
|
136
129
|
|
137
130
|
```ruby
|
138
|
-
#
|
131
|
+
# Meilisearch is typo-tolerant:
|
139
132
|
hits = Book.search('harry pottre')
|
140
133
|
hits.each do |hit|
|
141
134
|
puts hit.title
|
@@ -153,9 +146,9 @@ This gem supports:
|
|
153
146
|
Specify the `:pagination_backend` in the configuration file:
|
154
147
|
|
155
148
|
```ruby
|
156
|
-
MeiliSearch.configuration = {
|
157
|
-
meilisearch_host: '
|
158
|
-
meilisearch_api_key: '
|
149
|
+
MeiliSearch::Rails.configuration = {
|
150
|
+
meilisearch_host: 'YourMeilisearchHost',
|
151
|
+
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
159
152
|
pagination_backend: :kaminari #:will_paginate
|
160
153
|
}
|
161
154
|
```
|
@@ -185,13 +178,13 @@ Book.search('harry potter', hitsPerPage: 10)
|
|
185
178
|
|
186
179
|
#### Extra Configuration <!-- omit in toc -->
|
187
180
|
|
188
|
-
Requests made to
|
181
|
+
Requests made to Meilisearch may timeout and retry. To adapt the behavior to
|
189
182
|
your needs, you can change the parameters during configuration:
|
190
183
|
|
191
184
|
```ruby
|
192
|
-
MeiliSearch.configuration = {
|
193
|
-
meilisearch_host: '
|
194
|
-
meilisearch_api_key: '
|
185
|
+
MeiliSearch::Rails.configuration = {
|
186
|
+
meilisearch_host: 'YourMeilisearchHost',
|
187
|
+
meilisearch_api_key: 'YourMeilisearchAPIKey',
|
195
188
|
timeout: 2,
|
196
189
|
max_retries: 1,
|
197
190
|
}
|
@@ -203,18 +196,20 @@ You can configure the index settings by adding them inside the `meilisearch` blo
|
|
203
196
|
|
204
197
|
```ruby
|
205
198
|
class Book < ApplicationRecord
|
206
|
-
include MeiliSearch
|
199
|
+
include MeiliSearch::Rails
|
207
200
|
|
208
201
|
meilisearch do
|
209
202
|
searchable_attributes [:title, :author, :publisher, :description]
|
210
203
|
filterable_attributes [:genre]
|
204
|
+
sortable_attributes [:title]
|
211
205
|
ranking_rules [
|
212
206
|
'proximity',
|
213
207
|
'typo',
|
214
208
|
'words',
|
215
209
|
'attribute',
|
210
|
+
'sort',
|
216
211
|
'exactness',
|
217
|
-
'desc
|
212
|
+
'publication_year:desc'
|
218
213
|
]
|
219
214
|
synonyms nyc: ['new york']
|
220
215
|
|
@@ -238,30 +233,41 @@ Book.search('Harry', attributesToHighlight: ['*'])
|
|
238
233
|
👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
|
239
234
|
`crop_length` can be set up in the `meilisearch` block of your model.
|
240
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
|
+
|
241
247
|
## 🪛 Options
|
242
248
|
|
243
|
-
###
|
249
|
+
### Meilisearch configuration & environment
|
244
250
|
|
245
|
-
#### Custom index_uid
|
251
|
+
#### Custom index_uid <!-- omit in toc -->
|
246
252
|
|
247
253
|
By default, the **index_uid** will be the class name, e.g. `Book`. You can customize the index_uid by using the `index_uid:` option.
|
248
254
|
|
249
255
|
```ruby
|
250
256
|
class Book < ActiveRecord::Base
|
251
|
-
include MeiliSearch
|
257
|
+
include MeiliSearch::Rails
|
252
258
|
|
253
259
|
meilisearch index_uid: 'MyCustomUID' do
|
254
260
|
end
|
255
261
|
end
|
256
262
|
```
|
257
263
|
|
258
|
-
#### Index UID according to the environment
|
264
|
+
#### Index UID according to the environment <!-- omit in toc -->
|
259
265
|
|
260
266
|
You can suffix the index UID with the current Rails environment using the following option:
|
261
267
|
|
262
268
|
```ruby
|
263
269
|
class Book < ActiveRecord::Base
|
264
|
-
include MeiliSearch
|
270
|
+
include MeiliSearch::Rails
|
265
271
|
|
266
272
|
meilisearch per_environment: true do # The index UID will be "Book_#{Rails.env}"
|
267
273
|
end
|
@@ -278,7 +284,7 @@ You can add a custom attribute by using the `add_attribute` option or by using a
|
|
278
284
|
|
279
285
|
```ruby
|
280
286
|
class Author < ApplicationRecord
|
281
|
-
include MeiliSearch
|
287
|
+
include MeiliSearch::Rails
|
282
288
|
|
283
289
|
meilisearch do
|
284
290
|
attribute :first_name, :last_name
|
@@ -310,7 +316,7 @@ Note that the primary key must have a **unique value**.
|
|
310
316
|
|
311
317
|
```ruby
|
312
318
|
class Book < ActiveRecord::Base
|
313
|
-
include MeiliSearch
|
319
|
+
include MeiliSearch::Rails
|
314
320
|
|
315
321
|
meilisearch primary_key: 'ISBN' do
|
316
322
|
end
|
@@ -323,7 +329,7 @@ As soon as you use those constraints, `add_documents` and `delete_documents` cal
|
|
323
329
|
|
324
330
|
```ruby
|
325
331
|
class Book < ActiveRecord::Base
|
326
|
-
include MeiliSearch
|
332
|
+
include MeiliSearch::Rails
|
327
333
|
|
328
334
|
meilisearch if: :published?, unless: :premium? do
|
329
335
|
end
|
@@ -341,13 +347,13 @@ class Book < ActiveRecord::Base
|
|
341
347
|
end
|
342
348
|
end
|
343
349
|
```
|
344
|
-
##### Target multiple indexes
|
350
|
+
##### Target multiple indexes <!-- omit in toc -->
|
345
351
|
|
346
352
|
You can index a record in several indexes using the `add_index` option:
|
347
353
|
|
348
354
|
```ruby
|
349
355
|
class Book < ActiveRecord::Base
|
350
|
-
include MeiliSearch
|
356
|
+
include MeiliSearch::Rails
|
351
357
|
|
352
358
|
PUBLIC_INDEX_UID = 'Books'
|
353
359
|
SECURED_INDEX_UID = 'PrivateBooks'
|
@@ -375,7 +381,7 @@ You may want to share an index between several models. You'll need to ensure you
|
|
375
381
|
|
376
382
|
```ruby
|
377
383
|
class Cat < ActiveRecord::Base
|
378
|
-
include MeiliSearch
|
384
|
+
include MeiliSearch::Rails
|
379
385
|
|
380
386
|
meilisearch index_uid: 'Animals', primary_key: :ms_id do
|
381
387
|
end
|
@@ -387,7 +393,7 @@ class Cat < ActiveRecord::Base
|
|
387
393
|
end
|
388
394
|
|
389
395
|
class Dog < ActiveRecord::Base
|
390
|
-
include MeiliSearch
|
396
|
+
include MeiliSearch::Rails
|
391
397
|
|
392
398
|
meilisearch index_uid: 'Animals', primary_key: :ms_id do
|
393
399
|
end
|
@@ -405,7 +411,7 @@ You can configure the auto-indexing & auto-removal process to use a queue to per
|
|
405
411
|
|
406
412
|
```ruby
|
407
413
|
class Book < ActiveRecord::Base
|
408
|
-
include MeiliSearch
|
414
|
+
include MeiliSearch::Rails
|
409
415
|
|
410
416
|
meilisearch enqueue: true do # ActiveJob will be triggered using a `meilisearch` queue
|
411
417
|
end
|
@@ -420,7 +426,7 @@ With **ActiveJob**:
|
|
420
426
|
|
421
427
|
```ruby
|
422
428
|
class Book < ActiveRecord::Base
|
423
|
-
include MeiliSearch
|
429
|
+
include MeiliSearch::Rails
|
424
430
|
|
425
431
|
meilisearch enqueue: :trigger_job do
|
426
432
|
attribute :title, :author, :description
|
@@ -436,7 +442,7 @@ class MyActiveJob < ApplicationJob
|
|
436
442
|
if remove
|
437
443
|
# The record has likely already been removed from your database so we cannot
|
438
444
|
# use ActiveRecord#find to load it.
|
439
|
-
# We access the underlying
|
445
|
+
# We access the underlying Meilisearch index object.
|
440
446
|
Book.index.delete_document(id)
|
441
447
|
else
|
442
448
|
# The record should be present.
|
@@ -450,7 +456,7 @@ With [**Sidekiq**](https://github.com/mperham/sidekiq):
|
|
450
456
|
|
451
457
|
```ruby
|
452
458
|
class Book < ActiveRecord::Base
|
453
|
-
include MeiliSearch
|
459
|
+
include MeiliSearch::Rails
|
454
460
|
|
455
461
|
meilisearch enqueue: :trigger_sidekiq_worker do
|
456
462
|
attribute :title, :author, :description
|
@@ -466,7 +472,7 @@ class MySidekiqWorker
|
|
466
472
|
if remove
|
467
473
|
# The record has likely already been removed from your database so we cannot
|
468
474
|
# use ActiveRecord#find to load it.
|
469
|
-
# We access the underlying
|
475
|
+
# We access the underlying Meilisearch index object.
|
470
476
|
Book.index.delete_document(id)
|
471
477
|
else
|
472
478
|
# The record should be present.
|
@@ -480,7 +486,7 @@ With [**DelayedJob**](https://github.com/collectiveidea/delayed_job):
|
|
480
486
|
|
481
487
|
```ruby
|
482
488
|
class Book < ActiveRecord::Base
|
483
|
-
include MeiliSearch
|
489
|
+
include MeiliSearch::Rails
|
484
490
|
|
485
491
|
meilisearch enqueue: :trigger_delayed_job do
|
486
492
|
attribute :title, :author, :description
|
@@ -504,7 +510,7 @@ Extend a change to a related record.
|
|
504
510
|
|
505
511
|
```ruby
|
506
512
|
class Author < ActiveRecord::Base
|
507
|
-
include MeiliSearch
|
513
|
+
include MeiliSearch::Rails
|
508
514
|
|
509
515
|
has_many :books
|
510
516
|
# If your association uses belongs_to
|
@@ -514,7 +520,7 @@ class Author < ActiveRecord::Base
|
|
514
520
|
end
|
515
521
|
|
516
522
|
class Book < ActiveRecord::Base
|
517
|
-
include MeiliSearch
|
523
|
+
include MeiliSearch::Rails
|
518
524
|
|
519
525
|
belongs_to :author
|
520
526
|
after_touch :index!
|
@@ -533,7 +539,7 @@ With **Sequel**, you can use the `touch` plugin to propagate changes.
|
|
533
539
|
```ruby
|
534
540
|
# app/models/author.rb
|
535
541
|
class Author < Sequel::Model
|
536
|
-
include MeiliSearch
|
542
|
+
include MeiliSearch::Rails
|
537
543
|
|
538
544
|
one_to_many :books
|
539
545
|
|
@@ -555,7 +561,7 @@ end
|
|
555
561
|
|
556
562
|
# app/models/book.rb
|
557
563
|
class Book < Sequel::Model
|
558
|
-
include MeiliSearch
|
564
|
+
include MeiliSearch::Rails
|
559
565
|
|
560
566
|
many_to_one :author
|
561
567
|
after_touch :index!
|
@@ -578,7 +584,7 @@ You can strip all HTML tags from your attributes with the `sanitize` option.
|
|
578
584
|
|
579
585
|
```ruby
|
580
586
|
class Book < ActiveRecord::Base
|
581
|
-
include MeiliSearch
|
587
|
+
include MeiliSearch::Rails
|
582
588
|
|
583
589
|
meilisearch sanitize: true do
|
584
590
|
end
|
@@ -591,7 +597,7 @@ You can force the UTF-8 encoding of all your attributes using the `force_utf8_en
|
|
591
597
|
|
592
598
|
```ruby
|
593
599
|
class Book < ActiveRecord::Base
|
594
|
-
include MeiliSearch
|
600
|
+
include MeiliSearch::Rails
|
595
601
|
|
596
602
|
meilisearch force_utf8_encoding: true do
|
597
603
|
end
|
@@ -637,13 +643,13 @@ index = Book.index
|
|
637
643
|
|
638
644
|
### Development & testing
|
639
645
|
|
640
|
-
#### Exceptions
|
646
|
+
#### Exceptions <!-- omit in toc -->
|
641
647
|
|
642
|
-
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:
|
643
649
|
|
644
650
|
```ruby
|
645
651
|
class Book < ActiveRecord::Base
|
646
|
-
include MeiliSearch
|
652
|
+
include MeiliSearch::Rails
|
647
653
|
|
648
654
|
# Only raise exceptions in development environment.
|
649
655
|
meilisearch raise_on_failure: Rails.env.development? do
|
@@ -651,29 +657,29 @@ class Book < ActiveRecord::Base
|
|
651
657
|
end
|
652
658
|
```
|
653
659
|
|
654
|
-
#### Testing
|
660
|
+
#### Testing <!-- omit in toc -->
|
655
661
|
|
656
|
-
##### Synchronous testing
|
662
|
+
##### Synchronous testing <!-- omit in toc -->
|
657
663
|
|
658
664
|
You can force indexing and removing to be synchronous by setting the following option:
|
659
665
|
|
660
666
|
```ruby
|
661
667
|
class Book < ActiveRecord::Base
|
662
|
-
include MeiliSearch
|
668
|
+
include MeiliSearch::Rails
|
663
669
|
|
664
670
|
meilisearch synchronous: true do
|
665
671
|
end
|
666
672
|
end
|
667
673
|
```
|
668
|
-
🚨 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.
|
669
675
|
|
670
|
-
##### Disable auto-indexing & auto-removal
|
676
|
+
##### Disable auto-indexing & auto-removal <!-- omit in toc -->
|
671
677
|
|
672
678
|
You can disable auto-indexing and auto-removing setting the following options:
|
673
679
|
|
674
680
|
```ruby
|
675
681
|
class Book < ActiveRecord::Base
|
676
|
-
include MeiliSearch
|
682
|
+
include MeiliSearch::Rails
|
677
683
|
|
678
684
|
meilisearch auto_index: false, auto_remove: false do
|
679
685
|
end
|
@@ -701,4 +707,4 @@ The provided features and the code base is inspired by [algoliasearch-rails](htt
|
|
701
707
|
|
702
708
|
<hr>
|
703
709
|
|
704
|
-
**
|
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
@@ -2,16 +2,17 @@ require 'rubygems'
|
|
2
2
|
require 'rake'
|
3
3
|
|
4
4
|
require 'rdoc/task'
|
5
|
+
|
5
6
|
Rake::RDocTask.new do |rdoc|
|
6
|
-
version = File.exist?('VERSION') ? File.read('VERSION') :
|
7
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ''
|
7
8
|
|
8
9
|
rdoc.rdoc_dir = 'rdoc'
|
9
|
-
rdoc.title = "
|
10
|
+
rdoc.title = "Meilisearch Rails #{version}"
|
10
11
|
rdoc.rdoc_files.include('README*')
|
11
12
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
12
13
|
end
|
13
14
|
|
14
|
-
require
|
15
|
+
require 'rspec/core/rake_task'
|
15
16
|
RSpec::Core::RakeTask.new(:spec)
|
16
17
|
|
17
|
-
task :
|
18
|
+
task default: :spec
|
@@ -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
|