lib/meilisearch-rails.rb in meilisearch-rails-0.7.3 vs lib/meilisearch-rails.rb in meilisearch-rails-0.8.0
- old
+ new
@@ -57,12 +57,15 @@
stopWords
rankingRules
attributesToHighlight
attributesToCrop
cropLength
+ pagination
].freeze
+ CAMELIZE_OPTIONS = %i[pagination].freeze
+
OPTIONS.each do |option|
define_method option do |value|
instance_variable_set("@#{option}", value)
end
@@ -197,11 +200,17 @@
def to_settings
settings = {}
OPTIONS.each do |k|
v = get_setting(k)
- settings[k] = v unless v.nil?
+ next if v.nil?
+
+ settings[k] = if CAMELIZE_OPTIONS.include?(k) && v.is_a?(Hash)
+ v.transform_keys { |key| key.to_s.camelize(:lower) }
+ else
+ v
+ end
end
settings
end
def add_index(index_uid, options = {}, &block)
@@ -282,11 +291,11 @@
yield
rescue ::MeiliSearch::TimeoutError, ::MeiliSearch::ApiError => e
raise e if raise_on_failure
# log the error
- (::Rails.logger || Logger.new($stdout)).error("[meilisearch-rails] #{e.message}")
+ (::Rails.logger || Logger.new($stdout)).info("[meilisearch-rails] #{e.message}")
# return something
case method.to_s
when 'search'
# some attributes are required
{ 'hits' => [], 'hitsPerPage' => 0, 'page' => 0, 'facetDistribution' => {}, 'error' => e }
@@ -600,22 +609,19 @@
end
end
def ms_search(query, params = {})
if MeiliSearch::Rails.configuration[:pagination_backend]
- page = params[:page].nil? ? params[:page] : params[:page].to_i
- hits_per_page = params[:hitsPerPage].nil? ? params[:hitsPerPage] : params[:hitsPerPage].to_i
- hits_per_page ||= params[:hits_per_page].nil? ? params[:hits_per_page] : params[:hits_per_page].to_i
+ %i[page hitsPerPage hits_per_page].each do |key|
+ params[key.to_s.underscore.to_sym] = params[key].to_i if params.key?(key)
+ end
- %i[page hitsPerPage hits_per_page].each { |param| params.delete(param) }
-
- params[:limit] = 200
+ # It is required to activate the finite pagination in Meilisearch v0.30 (or newer),
+ # to have at least `hits_per_page` defined or `page` in the search request.
+ params[:page] ||= 1
end
- # Returns raw json hits as follows:
- # {"hits"=>[{"id"=>"13", "href"=>"apple", "name"=>"iphone"}], "offset"=>0, "limit"=>|| 20, "estimatedTotalHits"=>1,
- # "processingTimeMs"=>0, "query"=>"iphone"}
json = ms_raw_search(query, params)
# condition_key gets the primary key of the document; looks for "id" on the options
condition_key = if defined?(::Mongoid::Document) && include?(::Mongoid::Document)
ms_primary_key_method.in
@@ -654,14 +660,10 @@
o.formatted = hit['_formatted']
o
end
end.compact
- total_hits = json['hits'].length
- hits_per_page ||= 20
- page ||= 1
-
- res = MeiliSearch::Rails::Pagination.create(results, total_hits, meilisearch_options.merge(page: page, per_page: hits_per_page))
+ res = Pagination.create(results, json['totalHits'], meilisearch_options.merge(page: json['page'], per_page: json['hitsPerPage']))
res.extend(AdditionalMethods)
res.send(:ms_init_raw_answer, json)
res
end