Sha256: d837e49f4a243510d518ac4579094e42f649c76bcc54726481053c8ef4a9e6fc

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

require_relative 'multi_search/result'

module MeiliSearch
  module Rails
    class << self
      def multi_search(searches)
        search_parameters = searches.map do |(index_target, options)|
          paginate(options) if pagination_enabled?
          normalize(options, index_target)
        end

        MultiSearchResult.new(searches, client.multi_search(search_parameters))
      end

      private

      def normalize(options, index_target)
        options
          .except(:class_name)
          .merge!(index_uid: index_uid_from_target(index_target))
      end

      def index_uid_from_target(index_target)
        case index_target
        when String, Symbol
          index_target
        else
          index_target.index.uid
        end
      end

      def paginate(options)
        %w[page hitsPerPage hits_per_page].each do |key|
          # Deletes hitsPerPage to avoid passing along a meilisearch-ruby warning/exception
          value = options.delete(key) || options.delete(key.to_sym)
          options[key.underscore.to_sym] = value.to_i if value
        end

        # 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.
        options[:page] ||= 1
      end

      def pagination_enabled?
        MeiliSearch::Rails.configuration[:pagination_backend]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
meilisearch-rails-0.14.1 lib/meilisearch/rails/multi_search.rb
meilisearch-rails-0.14.0 lib/meilisearch/rails/multi_search.rb
meilisearch-rails-0.12.0 lib/meilisearch/rails/multi_search.rb