Sha256: 1fa4ed0567929e9bcf956a4c62f8accdfcc91f0d6eefc5614a156a18dfaa7b3d

Contents?: true

Size: 1.49 KB

Versions: 20

Compression:

Stored size: 1.49 KB

Contents

module Elasticity
  class MultiSearch
    def initialize(msearch_args = {})
      @results  = {}
      @searches = {}
      @mappers  = {}
      @msearch_args = msearch_args
      yield self if block_given?
    end

    def add(name, search, documents: nil, active_records: nil)
      if !documents.nil? && !active_records.nil?
        raise ArgumentError, "you can only pass either :documents or :active_records as an option"
      elsif documents.nil? && active_records.nil?
        raise ArgumentError, "you need to provide either :documents or :active_records as an option"
      end

      @searches[name] = {
        search_definition: search.search_definition,
        documents: documents,
        active_records: active_records
      }

      name
    end

    def [](name)
      @results[name] ||= result_for(name)
    end

    private

    def result_for(name)
      search = @searches[name]
      return if search.nil?

      query_response = response_for(@searches.keys.index(name))
      MultiSearchResponseParser.parse(query_response, search)
    end

    def response_for(index)
      @response ||= ActiveSupport::Notifications.instrument("multi_search.elasticity", args: { body: bodies }) do
        args = { body: bodies.map(&:dup) }.reverse_merge(@msearch_args)
        Elasticity.config.client.msearch(args)
      end
      @response["responses"][index]
    end

    def bodies
      @bodies ||= @searches.values.map do |hsh|
        hsh[:search_definition].to_msearch_args
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
es-elasticity-1.2.1 lib/elasticity/multi_search.rb
es-elasticity-1.1.0 lib/elasticity/multi_search.rb
es-elasticity-1.0.1 lib/elasticity/multi_search.rb
es-elasticity-0.14.1 lib/elasticity/multi_search.rb
es-elasticity-0.14.0 lib/elasticity/multi_search.rb
es-elasticity-0.14.0.jhumphreys lib/elasticity/multi_search.rb
es-elasticity-0.13.5 lib/elasticity/multi_search.rb
es-elasticity-0.13.5.dblackmon.1 lib/elasticity/multi_search.rb
es-elasticity-0.13.4 lib/elasticity/multi_search.rb
es-elasticity-0.13.4.zpesic lib/elasticity/multi_search.rb
es-elasticity-0.13.4.dblackmon.1 lib/elasticity/multi_search.rb
es-elasticity-0.13.4.dblackmon.0 lib/elasticity/multi_search.rb
es-elasticity-0.13.4.dblackmon lib/elasticity/multi_search.rb
es-elasticity-0.13.3 lib/elasticity/multi_search.rb
es-elasticity-0.13.3.pre1 lib/elasticity/multi_search.rb
es-elasticity-0.12.0 lib/elasticity/multi_search.rb
es-elasticity-0.11.5 lib/elasticity/multi_search.rb
es-elasticity-0.11.1 lib/elasticity/multi_search.rb
es-elasticity-0.9.0 lib/elasticity/multi_search.rb
es-elasticity-0.8.4 lib/elasticity/multi_search.rb