meilisearch-rails 0.14.2 → 0.15.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 +4 -4
- data/README.md +258 -50
- data/lib/meilisearch/rails/configuration.rb +3 -3
- data/lib/meilisearch/rails/errors.rb +2 -2
- data/lib/meilisearch/rails/ms_clean_up_job.rb +2 -2
- data/lib/meilisearch/rails/ms_job.rb +1 -1
- data/lib/meilisearch/rails/multi_search/federated_search_result.rb +78 -0
- data/lib/meilisearch/rails/multi_search/multi_search_result.rb +115 -0
- data/lib/meilisearch/rails/multi_search.rb +63 -8
- data/lib/meilisearch/rails/null_object.rb +1 -1
- data/lib/meilisearch/rails/pagination/kaminari.rb +10 -7
- data/lib/meilisearch/rails/pagination/will_paginate.rb +9 -3
- data/lib/meilisearch/rails/pagination.rb +4 -4
- data/lib/meilisearch/rails/railtie.rb +1 -1
- data/lib/meilisearch/rails/tasks/meilisearch.rake +3 -3
- data/lib/meilisearch/rails/templates/initializer.rb +1 -1
- data/lib/meilisearch/rails/utilities.rb +2 -2
- data/lib/meilisearch/rails/version.rb +2 -2
- data/lib/meilisearch-rails.rb +28 -22
- data/meilisearch-rails.gemspec +2 -2
- metadata +6 -5
- data/lib/meilisearch/rails/multi_search/result.rb +0 -84
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'active_support/core_ext/module/delegation'
|
2
|
+
|
3
|
+
module Meilisearch
|
4
|
+
module Rails
|
5
|
+
class FederatedSearchResult
|
6
|
+
attr_reader :metadata, :hits
|
7
|
+
|
8
|
+
def initialize(searches, raw_results)
|
9
|
+
hits = raw_results.delete('hits')
|
10
|
+
@hits = load_hits(hits, searches.to_a)
|
11
|
+
@metadata = raw_results
|
12
|
+
end
|
13
|
+
|
14
|
+
include Enumerable
|
15
|
+
|
16
|
+
delegate :each, :to_a, :to_ary, :empty?, :[], :first, :last, to: :@hits
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def load_hits(hits, searches)
|
21
|
+
hits_by_pos = hits.group_by { |hit| hit['_federation']['queriesPosition'] }
|
22
|
+
|
23
|
+
keys_and_records_by_pos = hits_by_pos.to_h do |pos, group_hits|
|
24
|
+
search_target, search_opts = searches[pos]
|
25
|
+
|
26
|
+
scope = if search_opts[:scope]
|
27
|
+
search_opts[:scope]
|
28
|
+
elsif search_target.instance_of?(Class)
|
29
|
+
search_target
|
30
|
+
end
|
31
|
+
|
32
|
+
if scope.present?
|
33
|
+
[pos, load_results(scope, group_hits)]
|
34
|
+
else
|
35
|
+
[pos, [nil, group_hits]]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
hits.filter_map do |hit|
|
40
|
+
hit_cond_key, recs_by_id = keys_and_records_by_pos[hit['_federation']['queriesPosition']]
|
41
|
+
|
42
|
+
if hit_cond_key.present?
|
43
|
+
record = recs_by_id[hit[hit_cond_key.to_s].to_s]
|
44
|
+
record&.formatted = hit['_formatted']
|
45
|
+
record
|
46
|
+
else
|
47
|
+
hit
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def load_results(scope, hits)
|
53
|
+
klass = scope.respond_to?(:model) ? scope.model : scope
|
54
|
+
|
55
|
+
pk_method = klass.ms_primary_key_method
|
56
|
+
pk_method = pk_method.in if Utilities.mongo_model?(klass)
|
57
|
+
|
58
|
+
condition_key = pk_is_virtual?(klass, pk_method) ? klass.primary_key : pk_method
|
59
|
+
|
60
|
+
hits_by_id = hits.index_by { |hit| hit[condition_key.to_s] }
|
61
|
+
|
62
|
+
records = scope.where(condition_key => hits_by_id.keys)
|
63
|
+
|
64
|
+
results_by_id = records.index_by do |record|
|
65
|
+
record.send(condition_key).to_s
|
66
|
+
end
|
67
|
+
|
68
|
+
[condition_key, results_by_id]
|
69
|
+
end
|
70
|
+
|
71
|
+
def pk_is_virtual?(model_class, pk_method)
|
72
|
+
model_class.columns
|
73
|
+
.map(&(Utilities.sequel_model?(model_class) ? :to_s : :name))
|
74
|
+
.exclude?(pk_method.to_s)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
module Meilisearch
|
2
|
+
module Rails
|
3
|
+
class MultiSearchResult
|
4
|
+
attr_reader :metadata
|
5
|
+
|
6
|
+
def initialize(searches, raw_results)
|
7
|
+
@results = {}
|
8
|
+
@metadata = {}
|
9
|
+
|
10
|
+
searches.zip(raw_results['results']).each do |(target, search_options), result|
|
11
|
+
results_class = if search_options[:class_name]
|
12
|
+
Meilisearch::Rails.logger.warn(
|
13
|
+
'[meilisearch-rails] The :class_name option in multi search is deprecated, please use :scope instead.'
|
14
|
+
)
|
15
|
+
|
16
|
+
search_options[:class_name].constantize
|
17
|
+
elsif target.instance_of?(Class)
|
18
|
+
target
|
19
|
+
elsif search_options[:scope]
|
20
|
+
search_options[:scope]
|
21
|
+
end
|
22
|
+
|
23
|
+
@results[target] = results_class ? load_results(results_class, result, scope: search_options[:scope]) : result['hits']
|
24
|
+
|
25
|
+
@metadata[target] = result.except('hits')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
include Enumerable
|
30
|
+
|
31
|
+
def each_hit(&block)
|
32
|
+
Meilisearch::Rails.logger.warn(
|
33
|
+
<<~DEPRECATION
|
34
|
+
[meilisearch-rails] Flattening multi search results is deprecated.
|
35
|
+
If you do not want the results to be grouped, please use federated search instead.
|
36
|
+
DEPRECATION
|
37
|
+
)
|
38
|
+
|
39
|
+
@results.each_value do |results|
|
40
|
+
results.each(&block)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def each(&block)
|
45
|
+
Meilisearch::Rails.logger.info(
|
46
|
+
<<~INFO
|
47
|
+
[meilisearch-rails] #each on a multi search now iterates through grouped results.
|
48
|
+
If you do not want the results to be grouped, please use federated search instead.
|
49
|
+
To quickly go back to the old deprecated behavior, use `#each_hit`.
|
50
|
+
INFO
|
51
|
+
)
|
52
|
+
|
53
|
+
@results.each(&block)
|
54
|
+
end
|
55
|
+
|
56
|
+
def each_result(&block)
|
57
|
+
@results.each(&block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def to_a
|
61
|
+
Meilisearch::Rails.logger.warn(
|
62
|
+
<<~DEPRECATION
|
63
|
+
[meilisearch-rails] Flattening multi search results is deprecated.
|
64
|
+
If you do not want the results to be grouped, please use federated search instead.
|
65
|
+
DEPRECATION
|
66
|
+
)
|
67
|
+
@results.values.flatten(1)
|
68
|
+
end
|
69
|
+
alias to_ary to_a
|
70
|
+
|
71
|
+
def to_h
|
72
|
+
@results
|
73
|
+
end
|
74
|
+
alias to_hash to_h
|
75
|
+
|
76
|
+
private
|
77
|
+
|
78
|
+
def load_results(klass, result, scope:)
|
79
|
+
scope ||= klass
|
80
|
+
|
81
|
+
pk_method = klass.ms_primary_key_method
|
82
|
+
pk_method = pk_method.in if Utilities.mongo_model?(klass)
|
83
|
+
|
84
|
+
condition_key = pk_is_virtual?(klass, pk_method) ? klass.primary_key : pk_method
|
85
|
+
|
86
|
+
hits_by_id =
|
87
|
+
result['hits'].index_by { |hit| hit[condition_key.to_s] }
|
88
|
+
|
89
|
+
records = scope.where(condition_key => hits_by_id.keys)
|
90
|
+
|
91
|
+
if records.respond_to? :in_order_of
|
92
|
+
records.in_order_of(condition_key, hits_by_id.keys).each do |record|
|
93
|
+
record.formatted = hits_by_id[record.send(condition_key).to_s]['_formatted']
|
94
|
+
end
|
95
|
+
else
|
96
|
+
results_by_id = records.index_by do |hit|
|
97
|
+
hit.send(condition_key).to_s
|
98
|
+
end
|
99
|
+
|
100
|
+
result['hits'].filter_map do |hit|
|
101
|
+
record = results_by_id[hit[condition_key.to_s].to_s]
|
102
|
+
record&.formatted = hit['_formatted']
|
103
|
+
record
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
def pk_is_virtual?(model_class, pk_method)
|
109
|
+
model_class.columns
|
110
|
+
.map(&(Utilities.sequel_model?(model_class) ? :to_s : :name))
|
111
|
+
.exclude?(pk_method.to_s)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -1,31 +1,72 @@
|
|
1
|
-
require_relative 'multi_search/
|
1
|
+
require_relative 'multi_search/multi_search_result'
|
2
|
+
require_relative 'multi_search/federated_search_result'
|
2
3
|
|
3
|
-
module
|
4
|
+
module Meilisearch
|
4
5
|
module Rails
|
5
6
|
class << self
|
6
7
|
def multi_search(searches)
|
7
8
|
search_parameters = searches.map do |(index_target, options)|
|
9
|
+
model_class = options[:scope].respond_to?(:model) ? options[:scope].model : options[:scope]
|
10
|
+
index_target = options.delete(:index_uid) || model_class || index_target
|
11
|
+
|
8
12
|
paginate(options) if pagination_enabled?
|
9
13
|
normalize(options, index_target)
|
10
14
|
end
|
11
15
|
|
12
|
-
MultiSearchResult.new(searches, client.multi_search(search_parameters))
|
16
|
+
MultiSearchResult.new(searches, client.multi_search(queries: search_parameters))
|
17
|
+
end
|
18
|
+
|
19
|
+
def federated_search(queries:, federation: {})
|
20
|
+
if federation.nil?
|
21
|
+
Meilisearch::Rails.logger.warn(
|
22
|
+
'[meilisearch-rails] In federated_search, `nil` is an invalid `:federation` option. To explicitly use defaults, pass `{}`.'
|
23
|
+
)
|
24
|
+
|
25
|
+
federation = {}
|
26
|
+
end
|
27
|
+
|
28
|
+
queries.map! { |item| [nil, item] } if queries.is_a?(Array)
|
29
|
+
|
30
|
+
cleaned_queries = queries.filter_map do |(index_target, options)|
|
31
|
+
model_class = options[:scope].respond_to?(:model) ? options[:scope].model : options[:scope]
|
32
|
+
index_target = options.delete(:index_uid) || index_target || model_class
|
33
|
+
|
34
|
+
strip_pagination_options(options)
|
35
|
+
normalize(options, index_target)
|
36
|
+
end
|
37
|
+
|
38
|
+
raw_results = client.multi_search(queries: cleaned_queries, federation: federation)
|
39
|
+
|
40
|
+
FederatedSearchResult.new(queries, raw_results)
|
13
41
|
end
|
14
42
|
|
15
43
|
private
|
16
44
|
|
17
45
|
def normalize(options, index_target)
|
46
|
+
index_target = index_uid_from_target(index_target)
|
47
|
+
|
48
|
+
return nil if index_target.nil?
|
49
|
+
|
18
50
|
options
|
19
|
-
.except(:class_name)
|
20
|
-
.merge!(index_uid:
|
51
|
+
.except(:class_name, :scope)
|
52
|
+
.merge!(index_uid: index_target)
|
21
53
|
end
|
22
54
|
|
23
55
|
def index_uid_from_target(index_target)
|
24
56
|
case index_target
|
25
57
|
when String, Symbol
|
26
58
|
index_target
|
27
|
-
|
28
|
-
index_target.index
|
59
|
+
when Class
|
60
|
+
if index_target.respond_to?(:index)
|
61
|
+
index_target.index.uid
|
62
|
+
else
|
63
|
+
Meilisearch::Rails.logger.warn <<~MODEL_NOT_INDEXED
|
64
|
+
[meilisearch-rails] This class was passed to a multi/federated search but it does not have an #index: #{index_target}
|
65
|
+
[meilisearch-rails] Are you sure it has a `meilisearch` block?
|
66
|
+
MODEL_NOT_INDEXED
|
67
|
+
|
68
|
+
nil
|
69
|
+
end
|
29
70
|
end
|
30
71
|
end
|
31
72
|
|
@@ -41,8 +82,22 @@ module MeiliSearch
|
|
41
82
|
options[:page] ||= 1
|
42
83
|
end
|
43
84
|
|
85
|
+
def strip_pagination_options(options)
|
86
|
+
pagination_options = %w[page hitsPerPage hits_per_page limit offset].select do |key|
|
87
|
+
options.delete(key) || options.delete(key.to_sym)
|
88
|
+
end
|
89
|
+
|
90
|
+
return if pagination_options.empty?
|
91
|
+
|
92
|
+
Meilisearch::Rails.logger.warn <<~WRONG_PAGINATION
|
93
|
+
[meilisearch-rails] Pagination options in federated search must apply to whole federation.
|
94
|
+
[meilisearch-rails] These options have been removed: #{pagination_options.join(', ')}.
|
95
|
+
[meilisearch-rails] Please pass them after queries, in the `federation:` option.
|
96
|
+
WRONG_PAGINATION
|
97
|
+
end
|
98
|
+
|
44
99
|
def pagination_enabled?
|
45
|
-
|
100
|
+
Meilisearch::Rails.configuration[:pagination_backend]
|
46
101
|
end
|
47
102
|
end
|
48
103
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
unless defined? Kaminari
|
2
|
-
raise(
|
2
|
+
raise(Meilisearch::BadConfiguration,
|
3
3
|
"Meilisearch: Please add 'kaminari' to your Gemfile to use kaminari pagination backend")
|
4
4
|
end
|
5
5
|
|
6
6
|
require 'kaminari/models/array_extension'
|
7
7
|
|
8
|
-
module
|
8
|
+
module Meilisearch
|
9
9
|
module Rails
|
10
10
|
module Pagination
|
11
11
|
class Kaminari < ::Kaminari::PaginatableArray
|
@@ -18,11 +18,14 @@ module MeiliSearch
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def self.create(results, total_hits, options = {})
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
21
|
+
unless Meilisearch::Rails.active?
|
22
|
+
total_hits = 0
|
23
|
+
options[:page] = 1
|
24
|
+
options[:per_page] = 1
|
25
|
+
end
|
26
|
+
|
27
|
+
offset = (options[:page] - 1) * options[:per_page]
|
28
|
+
array = new results, limit: options[:per_page], offset: offset, total_count: total_hits
|
26
29
|
|
27
30
|
if array.empty? && !results.empty?
|
28
31
|
# since Kaminari 0.16.0, you need to pad the results with nil values so it matches the offset param
|
@@ -1,15 +1,21 @@
|
|
1
1
|
begin
|
2
2
|
require 'will_paginate/collection'
|
3
3
|
rescue LoadError
|
4
|
-
raise(
|
5
|
-
"
|
4
|
+
raise(Meilisearch::BadConfiguration,
|
5
|
+
"Meilisearch: Please add 'will_paginate' to your Gemfile to use will_paginate pagination backend")
|
6
6
|
end
|
7
7
|
|
8
|
-
module
|
8
|
+
module Meilisearch
|
9
9
|
module Rails
|
10
10
|
module Pagination
|
11
11
|
class WillPaginate
|
12
12
|
def self.create(results, total_hits, options = {})
|
13
|
+
unless Meilisearch::Rails.active?
|
14
|
+
total_hits = 0
|
15
|
+
options[:page] = 1
|
16
|
+
options[:per_page] = 1
|
17
|
+
end
|
18
|
+
|
13
19
|
::WillPaginate::Collection.create(options[:page], options[:per_page], total_hits) do |pager|
|
14
20
|
pager.replace results
|
15
21
|
end
|
@@ -1,11 +1,11 @@
|
|
1
|
-
module
|
1
|
+
module Meilisearch
|
2
2
|
module Rails
|
3
3
|
module Pagination
|
4
4
|
autoload :WillPaginate, 'meilisearch/rails/pagination/will_paginate'
|
5
5
|
autoload :Kaminari, 'meilisearch/rails/pagination/kaminari'
|
6
6
|
|
7
7
|
def self.create(results, total_hits, options = {})
|
8
|
-
pagination_backend =
|
8
|
+
pagination_backend = Meilisearch::Rails.configuration[:pagination_backend]
|
9
9
|
|
10
10
|
if pagination_backend.nil? || (is_pagy = pagination_backend.to_s == 'pagy')
|
11
11
|
log_pagy_error if is_pagy
|
@@ -17,12 +17,12 @@ module MeiliSearch
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def self.log_pagy_error
|
20
|
-
|
20
|
+
Meilisearch::Rails.logger
|
21
21
|
.warn('[meilisearch-rails] Remove `pagination_backend: :pagy` from your initializer, `pagy` it is not required for `pagy`')
|
22
22
|
end
|
23
23
|
|
24
24
|
def self.load_pagination!(pagination_backend, results, total_hits, options)
|
25
|
-
::
|
25
|
+
::Meilisearch::Rails::Pagination
|
26
26
|
.const_get(pagination_backend.to_s.classify)
|
27
27
|
.create(results, total_hits, options)
|
28
28
|
rescue NameError
|
@@ -3,21 +3,21 @@ namespace :meilisearch do
|
|
3
3
|
task reindex: :environment do
|
4
4
|
puts 'Reindexing all Meilisearch models'
|
5
5
|
|
6
|
-
|
6
|
+
Meilisearch::Rails::Utilities.reindex_all_models
|
7
7
|
end
|
8
8
|
|
9
9
|
desc 'Set settings to all indexes'
|
10
10
|
task set_all_settings: :environment do
|
11
11
|
puts 'Set settings in all Meilisearch models'
|
12
12
|
|
13
|
-
|
13
|
+
Meilisearch::Rails::Utilities.set_settings_all_models
|
14
14
|
end
|
15
15
|
|
16
16
|
desc 'Clear all indexes'
|
17
17
|
task clear_indexes: :environment do
|
18
18
|
puts 'Clearing indexes from all Meilisearch models'
|
19
19
|
|
20
|
-
|
20
|
+
Meilisearch::Rails::Utilities.clear_all_indexes
|
21
21
|
end
|
22
22
|
|
23
23
|
desc 'Create initializer file'
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module Meilisearch
|
2
2
|
module Rails
|
3
3
|
module Utilities
|
4
4
|
class << self
|
@@ -8,7 +8,7 @@ module MeiliSearch
|
|
8
8
|
elsif ::Rails.application
|
9
9
|
::Rails.application.eager_load!
|
10
10
|
end
|
11
|
-
klasses =
|
11
|
+
klasses = Meilisearch::Rails.instance_variable_get(:@included_in)
|
12
12
|
(klasses + klasses.map(&:descendants).flatten).uniq
|
13
13
|
end
|
14
14
|
|
data/lib/meilisearch-rails.rb
CHANGED
@@ -20,7 +20,13 @@ end
|
|
20
20
|
|
21
21
|
require 'logger'
|
22
22
|
|
23
|
-
|
23
|
+
# Workaround for the soft deprecation of MeiliSearch (old spelling)
|
24
|
+
# The regular `const_get` method does not seem to work
|
25
|
+
# too well with autoload and thus does not pull in methods
|
26
|
+
# like `client` which are obviously vital.
|
27
|
+
MeiliSearch::Rails = Meilisearch::Rails
|
28
|
+
|
29
|
+
module Meilisearch
|
24
30
|
module Rails
|
25
31
|
autoload :Configuration, 'meilisearch/rails/configuration'
|
26
32
|
extend Configuration
|
@@ -97,7 +103,7 @@ module MeiliSearch
|
|
97
103
|
[meilisearch-rails] #{missing_searchable} declared in searchable_attributes but not in attributes. \
|
98
104
|
Please add it to attributes if it should be searchable.
|
99
105
|
WARNING
|
100
|
-
|
106
|
+
Meilisearch::Rails.logger.warn(warning)
|
101
107
|
end
|
102
108
|
end
|
103
109
|
end
|
@@ -270,12 +276,12 @@ module MeiliSearch
|
|
270
276
|
autoload :MSCleanUpJob, 'meilisearch/rails/ms_clean_up_job'
|
271
277
|
end
|
272
278
|
|
273
|
-
# this class wraps an
|
279
|
+
# this class wraps an Meilisearch::Index document ensuring all raised exceptions
|
274
280
|
# are correctly logged or thrown depending on the `raise_on_failure` option
|
275
281
|
class SafeIndex
|
276
282
|
def initialize(index_uid, raise_on_failure, options)
|
277
|
-
client =
|
278
|
-
primary_key = options[:primary_key] ||
|
283
|
+
client = Meilisearch::Rails.client
|
284
|
+
primary_key = options[:primary_key] || Meilisearch::Rails::IndexSettings::DEFAULT_PRIMARY_KEY
|
279
285
|
@raise_on_failure = raise_on_failure.nil? || raise_on_failure
|
280
286
|
|
281
287
|
SafeIndex.log_or_throw(nil, @raise_on_failure) do
|
@@ -285,7 +291,7 @@ module MeiliSearch
|
|
285
291
|
@index = client.index(index_uid)
|
286
292
|
end
|
287
293
|
|
288
|
-
::
|
294
|
+
::Meilisearch::Index.instance_methods(false).each do |m|
|
289
295
|
define_method(m) do |*args, &block|
|
290
296
|
if m == :update_settings
|
291
297
|
args[0].delete(:attributes_to_highlight) if args[0][:attributes_to_highlight]
|
@@ -294,7 +300,7 @@ module MeiliSearch
|
|
294
300
|
end
|
295
301
|
|
296
302
|
SafeIndex.log_or_throw(m, @raise_on_failure) do
|
297
|
-
return
|
303
|
+
return Meilisearch::Rails.black_hole unless Meilisearch::Rails.active?
|
298
304
|
|
299
305
|
@index.send(m, *args, &block)
|
300
306
|
end
|
@@ -304,7 +310,7 @@ module MeiliSearch
|
|
304
310
|
# Maually define facet_search due to complications with **opts in ruby 2.*
|
305
311
|
def facet_search(*args, **opts)
|
306
312
|
SafeIndex.log_or_throw(:facet_search, @raise_on_failure) do
|
307
|
-
return
|
313
|
+
return Meilisearch::Rails.black_hole unless Meilisearch::Rails.active?
|
308
314
|
|
309
315
|
@index.facet_search(*args, **opts)
|
310
316
|
end
|
@@ -323,7 +329,7 @@ module MeiliSearch
|
|
323
329
|
def settings(*args)
|
324
330
|
SafeIndex.log_or_throw(:settings, @raise_on_failure) do
|
325
331
|
@index.settings(*args)
|
326
|
-
rescue ::
|
332
|
+
rescue ::Meilisearch::ApiError => e
|
327
333
|
return {} if e.code == 'index_not_found' # not fatal
|
328
334
|
|
329
335
|
raise e
|
@@ -332,11 +338,11 @@ module MeiliSearch
|
|
332
338
|
|
333
339
|
def self.log_or_throw(method, raise_on_failure, &block)
|
334
340
|
yield
|
335
|
-
rescue ::
|
341
|
+
rescue ::Meilisearch::TimeoutError, ::Meilisearch::ApiError => e
|
336
342
|
raise e if raise_on_failure
|
337
343
|
|
338
344
|
# log the error
|
339
|
-
|
345
|
+
Meilisearch::Rails.logger.info("[meilisearch-rails] #{e.message}")
|
340
346
|
# return something
|
341
347
|
case method.to_s
|
342
348
|
when 'search'
|
@@ -349,7 +355,7 @@ module MeiliSearch
|
|
349
355
|
end
|
350
356
|
end
|
351
357
|
|
352
|
-
# these are the class methods added when
|
358
|
+
# these are the class methods added when Meilisearch is included
|
353
359
|
module ClassMethods
|
354
360
|
def self.extended(base)
|
355
361
|
class << base
|
@@ -379,7 +385,7 @@ module MeiliSearch
|
|
379
385
|
attr_accessor :formatted
|
380
386
|
|
381
387
|
if options.key?(:per_environment)
|
382
|
-
raise BadConfiguration, ':per_environment option should be defined globally on
|
388
|
+
raise BadConfiguration, ':per_environment option should be defined globally on Meilisearch::Rails.configuration block.'
|
383
389
|
end
|
384
390
|
|
385
391
|
if options[:synchronous] == true
|
@@ -415,7 +421,7 @@ module MeiliSearch
|
|
415
421
|
raise ArgumentError, "Invalid `enqueue` option: #{options[:enqueue]}"
|
416
422
|
end
|
417
423
|
meilisearch_options[:enqueue] = proc do |record, remove|
|
418
|
-
proc.call(record, remove)
|
424
|
+
proc.call(record, remove) if ::Meilisearch::Rails.active? && !ms_without_auto_index_scope
|
419
425
|
end
|
420
426
|
end
|
421
427
|
unless options[:auto_index] == false
|
@@ -499,7 +505,7 @@ module MeiliSearch
|
|
499
505
|
Thread.current["ms_without_auto_index_scope_for_#{model_name}"]
|
500
506
|
end
|
501
507
|
|
502
|
-
def ms_reindex!(batch_size =
|
508
|
+
def ms_reindex!(batch_size = Meilisearch::Rails::IndexSettings::DEFAULT_BATCH_SIZE, synchronous = false)
|
503
509
|
return if ms_without_auto_index_scope
|
504
510
|
|
505
511
|
ms_configurations.each do |options, settings|
|
@@ -622,7 +628,7 @@ module MeiliSearch
|
|
622
628
|
|
623
629
|
index = ms_ensure_init(options, settings)
|
624
630
|
synchronous || options[:synchronous] ? index.delete_all_documents.await : index.delete_all_documents
|
625
|
-
@ms_indexes[
|
631
|
+
@ms_indexes[Meilisearch::Rails.active?][settings] = nil
|
626
632
|
end
|
627
633
|
nil
|
628
634
|
end
|
@@ -666,7 +672,7 @@ module MeiliSearch
|
|
666
672
|
end
|
667
673
|
|
668
674
|
def ms_search(query, params = {})
|
669
|
-
if
|
675
|
+
if Meilisearch::Rails.configuration[:pagination_backend]
|
670
676
|
%i[page hitsPerPage hits_per_page].each do |key|
|
671
677
|
params[key.to_s.underscore.to_sym] = params[key].to_i if params.key?(key)
|
672
678
|
end
|
@@ -736,7 +742,7 @@ module MeiliSearch
|
|
736
742
|
|
737
743
|
def ms_index_uid(options = nil)
|
738
744
|
options ||= meilisearch_options
|
739
|
-
global_options ||=
|
745
|
+
global_options ||= Meilisearch::Rails.configuration
|
740
746
|
|
741
747
|
name = options[:index_uid] || model_name.to_s.gsub('::', '_')
|
742
748
|
name = "#{name}_#{::Rails.env}" if global_options[:per_environment]
|
@@ -788,11 +794,11 @@ module MeiliSearch
|
|
788
794
|
|
789
795
|
@ms_indexes ||= { true => {}, false => {} }
|
790
796
|
|
791
|
-
@ms_indexes[
|
797
|
+
@ms_indexes[Meilisearch::Rails.active?][settings] ||= SafeIndex.new(ms_index_uid(options), meilisearch_options[:raise_on_failure], meilisearch_options)
|
792
798
|
|
793
|
-
update_settings_if_changed(@ms_indexes[
|
799
|
+
update_settings_if_changed(@ms_indexes[Meilisearch::Rails.active?][settings], options, user_configuration)
|
794
800
|
|
795
|
-
@ms_indexes[
|
801
|
+
@ms_indexes[Meilisearch::Rails.active?][settings]
|
796
802
|
end
|
797
803
|
|
798
804
|
private
|
@@ -845,7 +851,7 @@ module MeiliSearch
|
|
845
851
|
end
|
846
852
|
|
847
853
|
def ms_pk(options = nil)
|
848
|
-
options[:primary_key] ||
|
854
|
+
options[:primary_key] || Meilisearch::Rails::IndexSettings::DEFAULT_PRIMARY_KEY
|
849
855
|
end
|
850
856
|
|
851
857
|
def meilisearch_settings_changed?(server_state, user_configuration)
|
data/meilisearch-rails.gemspec
CHANGED
@@ -5,7 +5,7 @@ require 'meilisearch/rails/version'
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = 'meilisearch-rails'
|
8
|
-
s.version =
|
8
|
+
s.version = Meilisearch::Rails::VERSION
|
9
9
|
|
10
10
|
s.authors = ['Meili']
|
11
11
|
s.email = 'bonjour@meilisearch.com'
|
@@ -34,6 +34,6 @@ Gem::Specification.new do |s|
|
|
34
34
|
|
35
35
|
s.required_ruby_version = '>= 3.0.0'
|
36
36
|
|
37
|
-
s.add_dependency 'meilisearch', '~> 0.
|
37
|
+
s.add_dependency 'meilisearch', '~> 0.31.0'
|
38
38
|
s.add_dependency 'mutex_m', '~> 0.2'
|
39
39
|
end
|