meilisearch-rails 0.2.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1562941b9f5cf4cb5a13085147f5b9b9ba1b3ea04587f6d1c520239ae5362236
4
- data.tar.gz: 57106ef19a839131d89e45bbb3d01f6aeef6fca623f3d803ade4f63282c2221d
3
+ metadata.gz: 9e05e5ed1e5210587bd2797412b0f5509b77151a620b79e4e8047c6c1722c3fb
4
+ data.tar.gz: cfb5a6a39cf21caa0532ae40d005a8c7fd3fff30dbbfa02c90dd1f31f63259df
5
5
  SHA512:
6
- metadata.gz: 2d33ff75ad9c5dc9eeab0c602bdfc5fa8f80b1d14d3a025105cf04c7c2e644a1309e68cbff56452eeea5c1801e13fc58d11f8dd529b02bd6ac14e3baf4c30d20
7
- data.tar.gz: 02720ef33a1a0da9cc309f04c24129c87b9f1d885e9d8ff4386ba0873f1aec14e88373bbabe8eae279627a76e573046d4ae445ac1837efffda39b7a458e33fa3
6
+ metadata.gz: 7a3e738a240fcbdc903d3a58acc9d5d68992a5e1adb56401fcd30a03e81c1fddbcc2024bed1338934de84d366ef58cc9e069243ad6ab5ca78c1330414e4a90b9
7
+ data.tar.gz: 88228f931aafcbdd69fa483594c3842aaf7603dac277333107dc8629656e96f2fb0b0b57d84039b81b922f95cc9ef6322fb5745a4626058f135357779e227e0c
data/Gemfile CHANGED
@@ -1,31 +1,38 @@
1
- source "http://rubygems.org"
1
+ source 'http://rubygems.org'
2
2
 
3
3
  gem 'json', '~> 2.5', '>= 2.5.1'
4
- gem 'meilisearch', '~> 0.15.3'
4
+ gem 'meilisearch', '~> 0.17.0'
5
5
 
6
- if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
7
- gem 'rubysl', '~> 2.0', :platform => :rbx
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["RAILS_VERSION"] || '5.2'
12
- gem 'rails', "~> #{rails_version}"
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', :platform => [:rbx, :ruby]
23
+ gem 'sqlite3', '~> 1.4.0', platform: %i[rbx ruby]
16
24
  else
17
- gem 'sqlite3', '< 1.4.0', :platform => [:rbx, :ruby]
25
+ gem 'sqlite3', '< 1.4.0', platform: %i[rbx ruby]
18
26
  end
27
+
28
+ gem 'activerecord-jdbc-adapter', platform: :jruby
29
+ gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
30
+ gem 'jdbc-sqlite3', platform: :jruby
19
31
  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
32
 
24
- sequel_version = ENV['SEQUEL_VERSION'] ? "~> #{ENV['SEQUEL_VERSION']}" : '>= 4.0'
25
- gem 'sequel', sequel_version
33
+ gem 'byebug'
34
+ gem 'dotenv', '~> 2.7', '>= 2.7.6'
26
35
  gem 'faker', '~> 2.17'
27
- gem 'will_paginate', '>= 2.3.15'
28
36
  gem 'kaminari'
29
- gem 'dotenv', '~> 2.7', '>= 2.7.6'
30
- gem 'byebug'
37
+ gem 'will_paginate', '>= 2.3.15'
31
38
  end
data/README.md CHANGED
@@ -29,33 +29,26 @@
29
29
 
30
30
  - [📖 Documentation](#-documentation)
31
31
  - [🤖 Compatibility with MeiliSearch](#-compatibility-with-meilisearch)
32
- - [🔧 Installation](#-installation)
33
- - [🔩 Settings](#-settings)
32
+ - [🚀 Getting Started](#-getting-started)
33
+ - [⚙️ Settings](#️-settings)
34
34
  - [🔍 Custom search](#-custom-search)
35
35
  - [🪛 Options](#-options)
36
- - [MeiliSearch configuration & environment](#meilisearch-configuration-&-environment)
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-&-background-jobs)
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-&-deletion)
47
+ - [Indexing & deletion](#indexing--deletion)
51
48
  - [Access the underlying index object](#access-the-underlying-index-object)
52
- - [Development & testing](#development-&-testing)
53
- - [Exceptions](#exceptions)
54
- - [Testing](#testing)
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.20.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.20.0).
61
+ This package only guarantees the compatibility with the [version v0.24.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.24.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,8 +97,8 @@ 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',
108
- meilisearch_api_key: 'YourMeiliSearchAPIKey',
100
+ meilisearch_host: 'YourMeiliSearchHost', # example: http://localhost:7700
101
+ meilisearch_api_key: 'YourMeiliSearchAPIKey',
109
102
  }
110
103
  ```
111
104
 
@@ -183,7 +176,21 @@ The **number of hits per page defaults to 20**, you can customize it by adding t
183
176
  Book.search('harry potter', hitsPerPage: 10)
184
177
  ```
185
178
 
186
- ## ⚙️ Settings
179
+ #### Extra Configuration <!-- omit in toc -->
180
+
181
+ Requests made to MeiliSearch may timeout and retry. To adapt the behavior to
182
+ your needs, you can change the parameters during configuration:
183
+
184
+ ```ruby
185
+ MeiliSearch.configuration = {
186
+ meilisearch_host: 'YourMeiliSearchHost',
187
+ meilisearch_api_key: 'YourMeiliSearchAPIKey',
188
+ timeout: 2,
189
+ max_retries: 1,
190
+ }
191
+ ```
192
+
193
+ ## ⚙️ Settings
187
194
 
188
195
  You can configure the index settings by adding them inside the `meilisearch` block as shown below:
189
196
 
@@ -193,15 +200,15 @@ class Book < ApplicationRecord
193
200
 
194
201
  meilisearch do
195
202
  searchable_attributes [:title, :author, :publisher, :description]
196
- attributes_for_faceting [:genre]
203
+ filterable_attributes [:genre]
197
204
  ranking_rules [
198
205
  'proximity',
199
206
  'typo',
200
207
  'words',
201
208
  'attribute',
202
- 'wordsPosition',
209
+ 'sort',
203
210
  'exactness',
204
- 'desc(publication_year)'
211
+ 'publication_year:desc'
205
212
  ]
206
213
  synonyms nyc: ['new york']
207
214
 
@@ -220,7 +227,7 @@ Check the dedicated section of the documentation, for more information on the [s
220
227
  All the supported options are described in the [search parameters](https://docs.meilisearch.com/reference/features/search_parameters.html) section of the documentation.
221
228
 
222
229
  ```ruby
223
- Book.search('Harry', filters: 'author = J. K. Rowling')
230
+ Book.search('Harry', attributesToHighlight: ['*'])
224
231
  ```
225
232
  👉 Don't forget that `attributes_to_highlight`, `attributes_to_crop`, and
226
233
  `crop_length` can be set up in the `meilisearch` block of your model.
@@ -229,7 +236,7 @@ Book.search('Harry', filters: 'author = J. K. Rowling')
229
236
 
230
237
  ### MeiliSearch configuration & environment
231
238
 
232
- #### Custom index_uid
239
+ #### Custom index_uid <!-- omit in toc -->
233
240
 
234
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.
235
242
 
@@ -242,7 +249,7 @@ class Book < ActiveRecord::Base
242
249
  end
243
250
  ```
244
251
 
245
- #### Index UID according to the environment
252
+ #### Index UID according to the environment <!-- omit in toc -->
246
253
 
247
254
  You can suffix the index UID with the current Rails environment using the following option:
248
255
 
@@ -270,13 +277,13 @@ class Author < ApplicationRecord
270
277
  meilisearch do
271
278
  attribute :first_name, :last_name
272
279
  attribute :full_name do
273
- '#{first_name} #{last_name}'
280
+ "#{first_name} #{last_name}"
274
281
  end
275
282
  add_attribute :full_name_reversed
276
283
  end
277
284
 
278
285
  def full_name_reversed
279
- '#{last_name} #{first_name}'
286
+ "#{last_name} #{first_name}"
280
287
  end
281
288
 
282
289
  def will_save_change_to_full_name?
@@ -328,7 +335,7 @@ class Book < ActiveRecord::Base
328
335
  end
329
336
  end
330
337
  ```
331
- ##### Target multiple indexes
338
+ ##### Target multiple indexes <!-- omit in toc -->
332
339
 
333
340
  You can index a record in several indexes using the `add_index` option:
334
341
 
@@ -624,7 +631,7 @@ index = Book.index
624
631
 
625
632
  ### Development & testing
626
633
 
627
- #### Exceptions
634
+ #### Exceptions <!-- omit in toc -->
628
635
 
629
636
  You can disable exceptions that could be raised while trying to reach MeiliSearch's API by using the `raise_on_failure` option:
630
637
 
@@ -638,9 +645,9 @@ class Book < ActiveRecord::Base
638
645
  end
639
646
  ```
640
647
 
641
- #### Testing
648
+ #### Testing <!-- omit in toc -->
642
649
 
643
- ##### Synchronous testing
650
+ ##### Synchronous testing <!-- omit in toc -->
644
651
 
645
652
  You can force indexing and removing to be synchronous by setting the following option:
646
653
 
@@ -654,7 +661,7 @@ end
654
661
  ```
655
662
  🚨 This is only recommended for testing purposes, the gem will call the `wait_for_pending_update` method that will stop your code execution until the asynchronous task has been processed by MeilSearch.
656
663
 
657
- ##### Disable auto-indexing & auto-removal
664
+ ##### Disable auto-indexing & auto-removal <!-- omit in toc -->
658
665
 
659
666
  You can disable auto-indexing and auto-removing setting the following options:
660
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 "rspec/core/rake_task"
15
+ require 'rspec/core/rake_task'
15
16
  RSpec::Core::RakeTask.new(:spec)
16
17
 
17
- task :default => :spec
18
+ task default: :spec
@@ -1,15 +1,21 @@
1
1
  module MeiliSearch
2
2
  module Configuration
3
3
  def configuration
4
- @@configuration || raise(NotConfigured, "Please configure MeiliSearch. Set MeiliSearch.configuration = {meilisearch_host: 'YOUR_MEILISEARCH_HOST', meilisearch_api_key: 'YOUR_API_KEY'}")
4
+ raise NotConfigured if @_config.blank?
5
+
6
+ @_config
5
7
  end
6
8
 
7
9
  def configuration=(configuration)
8
- @@configuration = configuration
10
+ @_config = configuration
9
11
  end
10
12
 
11
13
  def client
12
- ::MeiliSearch::Client.new(@@configuration[:meilisearch_host], @@configuration[:meilisearch_api_key])
14
+ ::MeiliSearch::Client.new(
15
+ configuration[:meilisearch_host] || 'http://localhost:7700',
16
+ configuration[:meilisearch_api_key],
17
+ configuration.slice(:timeout, :max_retries)
18
+ )
13
19
  end
14
20
  end
15
21
  end
@@ -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, "MeiliSearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
2
+ raise(MeiliSearch::BadConfiguration,
3
+ "MeiliSearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
3
4
  end
4
5
 
5
- require "kaminari/models/array_extension"
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
- super(array, options)
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(num)
19
+ def limit(_num)
16
20
  # noop
17
21
  self
18
22
  end
19
23
 
20
- def offset(num)
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
- if array.empty? and !results.empty?
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, "MeiliSearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
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 |pager|
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.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } # classify pagination backend name
11
- page = Object.const_get(:MeiliSearch).const_get(:Pagination).const_get(backend).create(results, total_hits, options)
12
- page
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, "Unknown pagination backend")
14
+ raise(BadConfiguration, 'Unknown pagination backend')
15
15
  end
16
16
  end
17
-
18
17
  end
19
18
  end
@@ -3,9 +3,10 @@ require 'rails'
3
3
  module MeiliSearch
4
4
  class Railtie < Rails::Railtie
5
5
  rake_tasks do
6
- load "meilisearch/tasks/meilisearch.rake"
6
+ load 'meilisearch/tasks/meilisearch.rake'
7
7
  end
8
8
  end
9
+
9
10
  class Engine < Rails::Engine
10
11
  end
11
12
  end
@@ -1,19 +1,17 @@
1
1
  namespace :meilisearch do
2
-
3
- desc "Reindex all models"
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 "Set settings to all indexes"
9
- task :set_all_settings => :environment do
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 "Clear all indexes"
14
- task :clear_indexes => :environment do
15
- puts "clearing all indexes"
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,42 +7,38 @@ module MeiliSearch
7
7
  elsif Rails.application
8
8
  Rails.application.eager_load!
9
9
  end
10
- MeiliSearch.instance_variable_get :@included_in
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 do |klass|
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
- puts ''
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
- puts klass
28
- puts "Reindexing #{klass.count} records..."
29
- klass.ms_reindex
24
+ Rails.logger.info klass
25
+ Rails.logger.info "Reindexing #{klass.count} records..."
26
+
27
+ klass.ms_reindex!
30
28
  end
31
29
  end
32
30
 
33
31
  def set_settings_all_models
34
32
  klasses = get_model_classes
35
33
 
36
- puts ''
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
- puts "Pushing #{klass} settings..."
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
-
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module MeiliSearch
2
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
3
5
  end