Sha256: 8477c43ebf012f9e058975493cf6f3ec69da217a05e13b9b8a08ef8a42d44229

Contents?: true

Size: 1.2 KB

Versions: 5

Compression:

Stored size: 1.2 KB

Contents

require "celsius/primo/adapter"
require "celsius/primo/adapter/operation"

# Mget is implemented by ordinary searches, because the original "get" of primo
# does not return the same amount of informations as a "search" for ids.
#
# Because searching for to many ids at once might cause trouble, a mget is split
# up into n searches, to be more easy to digest by primo.
class Celsius::Primo::Adapter::Mget < Celsius::Primo::Adapter::Operation
  RECORDS_PER_SEARCH = 25

  def call(mget_request, options = {})
    options = deep_stringify(options)

    search_results = search_requests_from(mget_request).map do |search_request|
      adapter.search(search_request, return_raw_response: options["return_raw_response"])
    end

    search_results.inject({"docs" => []}) do |mget_result, search_result|
      mget_result["docs"].concat(search_result["hits"]["hits"])
      return mget_result
    end
  end

  def search_requests_from(mget_request)
    mget_request["docs"].each_slice(RECORDS_PER_SEARCH).to_a.map.with_index do |array, index|
      {
        from: index * RECORDS_PER_SEARCH,
        size: RECORDS_PER_SEARCH,
        query: {
          ids: array.map { |element| element["_id"] }
        }
      }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
celsius-primo-0.1.4 lib/celsius/primo/adapter/mget.rb
celsius-primo-0.1.3 lib/celsius/primo/adapter/mget.rb
celsius-primo-0.1.2 lib/celsius/primo/adapter/mget.rb
celsius-primo-0.1.1 lib/celsius/primo/adapter/mget.rb
celsius-primo-0.1.0 lib/celsius/primo/adapter/mget.rb