meilisearch-rails 0.2.1 → 0.4.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/README.md +25 -32
- data/Rakefile +4 -3
- data/lib/meilisearch/configuration.rb +5 -3
- data/lib/meilisearch/errors.rb +12 -0
- data/lib/meilisearch/pagination/kaminari.rb +13 -7
- data/lib/meilisearch/pagination/will_paginate.rb +3 -2
- data/lib/meilisearch/pagination.rb +5 -6
- data/lib/meilisearch/railtie.rb +2 -1
- data/lib/meilisearch/tasks/meilisearch.rake +8 -10
- data/lib/meilisearch/utilities.rb +10 -14
- data/lib/meilisearch/version.rb +3 -1
- data/lib/meilisearch-rails.rb +208 -190
- data/meilisearch-rails.gemspec +25 -35
- metadata +4 -5
- data/spec/spec_helper.rb +0 -52
- data/spec/utilities_spec.rb +0 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e61a10b5b9eaf11606209ff295728cdd962acdb4f21d02004d416ed7346a3212
|
4
|
+
data.tar.gz: bd8b788affabaa81eafbcd027e223179c356e3d21af2083f335d2def1def71af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5e3af82f062f7578f8008c15050c67c1241b9922613e65dfeb8fb9c7ef6816e2900eb76b0836e726e3af0c408401761f6e9582ccd3ebd10173db49fc978b7015
|
7
|
+
data.tar.gz: '02424401493f9e983fa5b0b556e388191ef9612c7ab799cc781ed05bf8a42cb8a54cf98d3062bcd0c7525e81e0a986326fd37b89c853d83981bbf1332e0c9329'
|
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/README.md
CHANGED
@@ -29,33 +29,26 @@
|
|
29
29
|
|
30
30
|
- [📖 Documentation](#-documentation)
|
31
31
|
- [🤖 Compatibility with MeiliSearch](#-compatibility-with-meilisearch)
|
32
|
-
- [
|
33
|
-
- [
|
32
|
+
- [🚀 Getting Started](#-getting-started)
|
33
|
+
- [⚙️ Settings](#️-settings)
|
34
34
|
- [🔍 Custom search](#-custom-search)
|
35
35
|
- [🪛 Options](#-options)
|
36
|
-
- [MeiliSearch configuration & environment](#meilisearch-configuration
|
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
|
|
@@ -65,9 +58,9 @@ To learn more about MeiliSearch, check out our [Documentation](https://docs.meil
|
|
65
58
|
|
66
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
|
|
@@ -104,7 +97,7 @@ Create a new file `config/initializers/meilisearch.rb` to setup your `MEILISEARC
|
|
104
97
|
|
105
98
|
```ruby
|
106
99
|
MeiliSearch.configuration = {
|
107
|
-
meilisearch_host: 'YourMeiliSearchHost',
|
100
|
+
meilisearch_host: 'YourMeiliSearchHost', # example: http://localhost:7700
|
108
101
|
meilisearch_api_key: 'YourMeiliSearchAPIKey',
|
109
102
|
}
|
110
103
|
```
|
@@ -207,15 +200,15 @@ class Book < ApplicationRecord
|
|
207
200
|
|
208
201
|
meilisearch do
|
209
202
|
searchable_attributes [:title, :author, :publisher, :description]
|
210
|
-
|
203
|
+
filterable_attributes [:genre]
|
211
204
|
ranking_rules [
|
212
205
|
'proximity',
|
213
206
|
'typo',
|
214
207
|
'words',
|
215
208
|
'attribute',
|
216
|
-
'
|
209
|
+
'sort',
|
217
210
|
'exactness',
|
218
|
-
'desc
|
211
|
+
'publication_year:desc'
|
219
212
|
]
|
220
213
|
synonyms nyc: ['new york']
|
221
214
|
|
@@ -234,7 +227,7 @@ Check the dedicated section of the documentation, for more information on the [s
|
|
234
227
|
All the supported options are described in the [search parameters](https://docs.meilisearch.com/reference/features/search_parameters.html) section of the documentation.
|
235
228
|
|
236
229
|
```ruby
|
237
|
-
Book.search('Harry',
|
230
|
+
Book.search('Harry', attributesToHighlight: ['*'])
|
238
231
|
```
|
239
232
|
👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
|
240
233
|
`crop_length` can be set up in the `meilisearch` block of your model.
|
@@ -243,7 +236,7 @@ Book.search('Harry', filters: 'author = J. K. Rowling')
|
|
243
236
|
|
244
237
|
### MeiliSearch configuration & environment
|
245
238
|
|
246
|
-
#### Custom index_uid
|
239
|
+
#### Custom index_uid <!-- omit in toc -->
|
247
240
|
|
248
241
|
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.
|
249
242
|
|
@@ -256,7 +249,7 @@ class Book < ActiveRecord::Base
|
|
256
249
|
end
|
257
250
|
```
|
258
251
|
|
259
|
-
#### Index UID according to the environment
|
252
|
+
#### Index UID according to the environment <!-- omit in toc -->
|
260
253
|
|
261
254
|
You can suffix the index UID with the current Rails environment using the following option:
|
262
255
|
|
@@ -284,13 +277,13 @@ class Author < ApplicationRecord
|
|
284
277
|
meilisearch do
|
285
278
|
attribute :first_name, :last_name
|
286
279
|
attribute :full_name do
|
287
|
-
|
280
|
+
"#{first_name} #{last_name}"
|
288
281
|
end
|
289
282
|
add_attribute :full_name_reversed
|
290
283
|
end
|
291
284
|
|
292
285
|
def full_name_reversed
|
293
|
-
|
286
|
+
"#{last_name} #{first_name}"
|
294
287
|
end
|
295
288
|
|
296
289
|
def will_save_change_to_full_name?
|
@@ -342,7 +335,7 @@ class Book < ActiveRecord::Base
|
|
342
335
|
end
|
343
336
|
end
|
344
337
|
```
|
345
|
-
##### Target multiple indexes
|
338
|
+
##### Target multiple indexes <!-- omit in toc -->
|
346
339
|
|
347
340
|
You can index a record in several indexes using the `add_index` option:
|
348
341
|
|
@@ -638,7 +631,7 @@ index = Book.index
|
|
638
631
|
|
639
632
|
### Development & testing
|
640
633
|
|
641
|
-
#### Exceptions
|
634
|
+
#### Exceptions <!-- omit in toc -->
|
642
635
|
|
643
636
|
You can disable exceptions that could be raised while trying to reach MeiliSearch's API by using the `raise_on_failure` option:
|
644
637
|
|
@@ -652,9 +645,9 @@ class Book < ActiveRecord::Base
|
|
652
645
|
end
|
653
646
|
```
|
654
647
|
|
655
|
-
#### Testing
|
648
|
+
#### Testing <!-- omit in toc -->
|
656
649
|
|
657
|
-
##### Synchronous testing
|
650
|
+
##### Synchronous testing <!-- omit in toc -->
|
658
651
|
|
659
652
|
You can force indexing and removing to be synchronous by setting the following option:
|
660
653
|
|
@@ -666,9 +659,9 @@ class Book < ActiveRecord::Base
|
|
666
659
|
end
|
667
660
|
end
|
668
661
|
```
|
669
|
-
🚨 This is only recommended for testing purposes, the gem will call the `
|
662
|
+
🚨 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.
|
670
663
|
|
671
|
-
##### Disable auto-indexing & auto-removal
|
664
|
+
##### Disable auto-indexing & auto-removal <!-- omit in toc -->
|
672
665
|
|
673
666
|
You can disable auto-indexing and auto-removing setting the following options:
|
674
667
|
|
data/Rakefile
CHANGED
@@ -2,8 +2,9 @@ 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
10
|
rdoc.title = "MeiliSearch Rails #{version}"
|
@@ -11,7 +12,7 @@ Rake::RDocTask.new do |rdoc|
|
|
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
|
@@ -1,16 +1,18 @@
|
|
1
1
|
module MeiliSearch
|
2
2
|
module Configuration
|
3
3
|
def configuration
|
4
|
-
|
4
|
+
raise NotConfigured if @_config.blank?
|
5
|
+
|
6
|
+
@_config
|
5
7
|
end
|
6
8
|
|
7
9
|
def configuration=(configuration)
|
8
|
-
|
10
|
+
@_config = configuration
|
9
11
|
end
|
10
12
|
|
11
13
|
def client
|
12
14
|
::MeiliSearch::Client.new(
|
13
|
-
configuration[:meilisearch_host],
|
15
|
+
configuration[:meilisearch_host] || 'http://localhost:7700',
|
14
16
|
configuration[:meilisearch_api_key],
|
15
17
|
configuration.slice(:timeout, :max_retries)
|
16
18
|
)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module MeiliSearch
|
2
|
+
class NoBlockGiven < StandardError; end
|
3
|
+
|
4
|
+
class BadConfiguration < StandardError; end
|
5
|
+
|
6
|
+
class NotConfigured < StandardError
|
7
|
+
def message
|
8
|
+
'Please configure MeiliSearch. Set MeiliSearch.configuration = ' \
|
9
|
+
"{meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -1,23 +1,27 @@
|
|
1
1
|
unless defined? Kaminari
|
2
|
-
raise(MeiliSearch::BadConfiguration,
|
2
|
+
raise(MeiliSearch::BadConfiguration,
|
3
|
+
"MeiliSearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
|
3
4
|
end
|
4
5
|
|
5
|
-
require
|
6
|
+
require 'kaminari/models/array_extension'
|
6
7
|
|
7
8
|
module MeiliSearch
|
8
9
|
module Pagination
|
9
10
|
class Kaminari < ::Kaminari::PaginatableArray
|
10
|
-
|
11
11
|
def initialize(array, options)
|
12
|
-
|
12
|
+
if RUBY_VERSION >= '3'
|
13
|
+
super(array, **options)
|
14
|
+
else
|
15
|
+
super(array, options)
|
16
|
+
end
|
13
17
|
end
|
14
18
|
|
15
|
-
def limit(
|
19
|
+
def limit(_num)
|
16
20
|
# noop
|
17
21
|
self
|
18
22
|
end
|
19
23
|
|
20
|
-
def offset(
|
24
|
+
def offset(_num)
|
21
25
|
# noop
|
22
26
|
self
|
23
27
|
end
|
@@ -26,12 +30,14 @@ module MeiliSearch
|
|
26
30
|
def create(results, total_hits, options = {})
|
27
31
|
offset = ((options[:page] - 1) * options[:per_page])
|
28
32
|
array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
|
29
|
-
|
33
|
+
|
34
|
+
if array.empty? && !results.empty?
|
30
35
|
# since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
|
31
36
|
# otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
|
32
37
|
results = ([nil] * offset) + results
|
33
38
|
array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
|
34
39
|
end
|
40
|
+
|
35
41
|
array
|
36
42
|
end
|
37
43
|
end
|
@@ -1,14 +1,15 @@
|
|
1
1
|
begin
|
2
2
|
require 'will_paginate/collection'
|
3
3
|
rescue LoadError
|
4
|
-
raise(MeiliSearch::BadConfiguration,
|
4
|
+
raise(MeiliSearch::BadConfiguration,
|
5
|
+
"MeiliSearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
|
5
6
|
end
|
6
7
|
|
7
8
|
module MeiliSearch
|
8
9
|
module Pagination
|
9
10
|
class WillPaginate
|
10
11
|
def self.create(results, total_hits, options = {})
|
11
|
-
::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do
|
12
|
+
::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
|
12
13
|
start = (options[:page] - 1) * options[:per_page]
|
13
14
|
paginated_results = results[start, options[:per_page]]
|
14
15
|
pager.replace paginated_results
|
@@ -1,19 +1,18 @@
|
|
1
1
|
module MeiliSearch
|
2
2
|
module Pagination
|
3
|
-
|
4
3
|
autoload :WillPaginate, 'meilisearch/pagination/will_paginate'
|
5
4
|
autoload :Kaminari, 'meilisearch/pagination/kaminari'
|
6
5
|
|
7
6
|
def self.create(results, total_hits, options = {})
|
8
7
|
return results if MeiliSearch.configuration[:pagination_backend].nil?
|
8
|
+
|
9
9
|
begin
|
10
|
-
backend = MeiliSearch.configuration[:pagination_backend].to_s.
|
11
|
-
|
12
|
-
|
10
|
+
backend = MeiliSearch.configuration[:pagination_backend].to_s.classify
|
11
|
+
|
12
|
+
::MeiliSearch::Pagination.const_get(backend).create(results, total_hits, options)
|
13
13
|
rescue NameError
|
14
|
-
raise(BadConfiguration,
|
14
|
+
raise(BadConfiguration, 'Unknown pagination backend')
|
15
15
|
end
|
16
16
|
end
|
17
|
-
|
18
17
|
end
|
19
18
|
end
|
data/lib/meilisearch/railtie.rb
CHANGED
@@ -1,19 +1,17 @@
|
|
1
1
|
namespace :meilisearch do
|
2
|
-
|
3
|
-
|
4
|
-
task :reindex => :environment do
|
2
|
+
desc 'Reindex all models'
|
3
|
+
task reindex: :environment do
|
5
4
|
MeiliSearch::Utilities.reindex_all_models
|
6
5
|
end
|
7
6
|
|
8
|
-
desc
|
9
|
-
task :
|
7
|
+
desc 'Set settings to all indexes'
|
8
|
+
task set_all_settings: :environment do
|
10
9
|
MeiliSearch::Utilities.set_settings_all_models
|
11
10
|
end
|
12
|
-
|
13
|
-
desc
|
14
|
-
task :
|
15
|
-
puts
|
11
|
+
|
12
|
+
desc 'Clear all indexes'
|
13
|
+
task clear_indexes: :environment do
|
14
|
+
puts 'clearing all indexes'
|
16
15
|
MeiliSearch::Utilities.clear_all_indexes
|
17
16
|
end
|
18
|
-
|
19
17
|
end
|
@@ -7,25 +7,23 @@ module MeiliSearch
|
|
7
7
|
elsif Rails.application
|
8
8
|
Rails.application.eager_load!
|
9
9
|
end
|
10
|
-
MeiliSearch.instance_variable_get
|
10
|
+
klasses = MeiliSearch.instance_variable_get(:@included_in)
|
11
|
+
(klasses + klasses.map(&:descendants).flatten).uniq
|
11
12
|
end
|
12
13
|
|
13
14
|
def clear_all_indexes
|
14
|
-
get_model_classes.each
|
15
|
-
klass.clear_index!
|
16
|
-
end
|
15
|
+
get_model_classes.each(&:clear_index!)
|
17
16
|
end
|
18
17
|
|
19
18
|
def reindex_all_models
|
20
19
|
klasses = get_model_classes
|
21
20
|
|
22
|
-
|
23
|
-
puts "Reindexing #{klasses.count} models: #{klasses.to_sentence}."
|
24
|
-
puts ''
|
21
|
+
Rails.logger.info "\n\nReindexing #{klasses.count} models: #{klasses.to_sentence}.\n"
|
25
22
|
|
26
23
|
klasses.each do |klass|
|
27
|
-
|
28
|
-
|
24
|
+
Rails.logger.info klass
|
25
|
+
Rails.logger.info "Reindexing #{klass.count} records..."
|
26
|
+
|
29
27
|
klass.ms_reindex!
|
30
28
|
end
|
31
29
|
end
|
@@ -33,16 +31,14 @@ module MeiliSearch
|
|
33
31
|
def set_settings_all_models
|
34
32
|
klasses = get_model_classes
|
35
33
|
|
36
|
-
|
37
|
-
puts "Pushing settings for #{klasses.count} models: #{klasses.to_sentence}."
|
38
|
-
puts ''
|
34
|
+
Rails.logger.info "\n\nPushing settings for #{klasses.count} models: #{klasses.to_sentence}.\n"
|
39
35
|
|
40
36
|
klasses.each do |klass|
|
41
|
-
|
37
|
+
Rails.logger.info "Pushing #{klass} settings..."
|
38
|
+
|
42
39
|
klass.ms_set_settings
|
43
40
|
end
|
44
41
|
end
|
45
42
|
end
|
46
43
|
end
|
47
44
|
end
|
48
|
-
|
data/lib/meilisearch/version.rb
CHANGED