meilisearch-rails 0.2.2 → 0.4.1
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 +21 -27
- data/Rakefile +4 -3
- data/lib/meilisearch/configuration.rb +5 -3
- data/lib/meilisearch/errors.rb +12 -0
- data/lib/meilisearch/pagination/kaminari.rb +8 -6
- 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 -202
- 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: ea22a400fc0f909f793e231f12a61b47f1d751d0c5932f71f2503001bf57c39e
|
4
|
+
data.tar.gz: b05b67044feec7c8199252026794cd33247045adb62bde0f368a82f2597dd27f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e1d8b8475cf7f5777ed9c897b342bb4283ad3b00ab12b12060e664acecb973fcb87566aaf5b6fb4a98324bfadeec389bc765cb83563b37d9bce8da088a573e53
|
7
|
+
data.tar.gz: ba4c38cc90384fbcb0623891e57629b7ba3472d7695f108d6623846a534af81c669b1f8b63af7003998c50449c20385b4f0f7a85d9f6eb5ef25daaf13948e3f0
|
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
|
```
|
@@ -213,8 +206,9 @@ class Book < ApplicationRecord
|
|
213
206
|
'typo',
|
214
207
|
'words',
|
215
208
|
'attribute',
|
209
|
+
'sort',
|
216
210
|
'exactness',
|
217
|
-
'desc
|
211
|
+
'publication_year:desc'
|
218
212
|
]
|
219
213
|
synonyms nyc: ['new york']
|
220
214
|
|
@@ -242,7 +236,7 @@ Book.search('Harry', attributesToHighlight: ['*'])
|
|
242
236
|
|
243
237
|
### MeiliSearch configuration & environment
|
244
238
|
|
245
|
-
#### Custom index_uid
|
239
|
+
#### Custom index_uid <!-- omit in toc -->
|
246
240
|
|
247
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.
|
248
242
|
|
@@ -255,7 +249,7 @@ class Book < ActiveRecord::Base
|
|
255
249
|
end
|
256
250
|
```
|
257
251
|
|
258
|
-
#### Index UID according to the environment
|
252
|
+
#### Index UID according to the environment <!-- omit in toc -->
|
259
253
|
|
260
254
|
You can suffix the index UID with the current Rails environment using the following option:
|
261
255
|
|
@@ -341,7 +335,7 @@ class Book < ActiveRecord::Base
|
|
341
335
|
end
|
342
336
|
end
|
343
337
|
```
|
344
|
-
##### Target multiple indexes
|
338
|
+
##### Target multiple indexes <!-- omit in toc -->
|
345
339
|
|
346
340
|
You can index a record in several indexes using the `add_index` option:
|
347
341
|
|
@@ -637,7 +631,7 @@ index = Book.index
|
|
637
631
|
|
638
632
|
### Development & testing
|
639
633
|
|
640
|
-
#### Exceptions
|
634
|
+
#### Exceptions <!-- omit in toc -->
|
641
635
|
|
642
636
|
You can disable exceptions that could be raised while trying to reach MeiliSearch's API by using the `raise_on_failure` option:
|
643
637
|
|
@@ -651,9 +645,9 @@ class Book < ActiveRecord::Base
|
|
651
645
|
end
|
652
646
|
```
|
653
647
|
|
654
|
-
#### Testing
|
648
|
+
#### Testing <!-- omit in toc -->
|
655
649
|
|
656
|
-
##### Synchronous testing
|
650
|
+
##### Synchronous testing <!-- omit in toc -->
|
657
651
|
|
658
652
|
You can force indexing and removing to be synchronous by setting the following option:
|
659
653
|
|
@@ -665,9 +659,9 @@ class Book < ActiveRecord::Base
|
|
665
659
|
end
|
666
660
|
end
|
667
661
|
```
|
668
|
-
🚨 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.
|
669
663
|
|
670
|
-
##### Disable auto-indexing & auto-removal
|
664
|
+
##### Disable auto-indexing & auto-removal <!-- omit in toc -->
|
671
665
|
|
672
666
|
You can disable auto-indexing and auto-removing setting the following options:
|
673
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,13 +1,13 @@
|
|
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
13
|
super(array, **options)
|
@@ -16,12 +16,12 @@ module MeiliSearch
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def limit(
|
19
|
+
def limit(_num)
|
20
20
|
# noop
|
21
21
|
self
|
22
22
|
end
|
23
23
|
|
24
|
-
def offset(
|
24
|
+
def offset(_num)
|
25
25
|
# noop
|
26
26
|
self
|
27
27
|
end
|
@@ -30,12 +30,14 @@ module MeiliSearch
|
|
30
30
|
def create(results, total_hits, options = {})
|
31
31
|
offset = ((options[:page] - 1) * options[:per_page])
|
32
32
|
array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
|
33
|
-
|
33
|
+
|
34
|
+
if array.empty? && !results.empty?
|
34
35
|
# since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
|
35
36
|
# otherwise you'll get an empty array: https://github.com/amatsuda/kaminari/commit/29fdcfa8865f2021f710adaedb41b7a7b081e34d
|
36
37
|
results = ([nil] * offset) + results
|
37
38
|
array = new results, offset: offset, limit: options[:per_page], total_count: total_hits
|
38
39
|
end
|
40
|
+
|
39
41
|
array
|
40
42
|
end
|
41
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