Sha256: c3723ffe79224f03a9e84f1bf23d873d07907d2efa7075c91cfe06ff0e93b22f

Contents?: true

Size: 1.15 KB

Versions: 13

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Esse
  class Index
    module ClassMethods
      # @param query_or_payload [String,Hash] The search request definition or query in the Lucene query string syntax
      # @param kwargs [Hash] The options to pass to the search.
      def search(*args, &block)
        kwargs = extract_search_options!(args)
        cluster.search(self, **kwargs, &block)
      end

      private

      def extract_search_options!(args)
        query_or_payload = args.shift
        kwargs = args.last.is_a?(Hash) ? args.pop : {}

        if query_or_payload.respond_to?(:to_hash) && (hash = query_or_payload.to_hash) && (hash.key?(:body) || hash.key?('body') || hash.key?(:q) || hash.key?('q'))
          kwargs.merge!(hash.transform_keys(&:to_sym))
        elsif query_or_payload.respond_to?(:to_hash)
          kwargs[:body] = query_or_payload.to_hash
        elsif query_or_payload.is_a?(String) && query_or_payload =~ /^\s*{/
          kwargs[:body] = MultiJson.load(query_or_payload)
        elsif query_or_payload.is_a?(String)
          kwargs[:q] = query_or_payload
        end
        kwargs
      end
    end

    extend ClassMethods
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
esse-0.4.0.rc4 lib/esse/index/search.rb
esse-0.4.0.rc3 lib/esse/index/search.rb
esse-0.4.0.rc2 lib/esse/index/search.rb
esse-0.4.0.rc1 lib/esse/index/search.rb
esse-0.3.5 lib/esse/index/search.rb
esse-0.3.4 lib/esse/index/search.rb
esse-0.3.3 lib/esse/index/search.rb
esse-0.3.2 lib/esse/index/search.rb
esse-0.3.1 lib/esse/index/search.rb
esse-0.3.0 lib/esse/index/search.rb
esse-0.2.6 lib/esse/index/search.rb
esse-0.2.5 lib/esse/index/search.rb
esse-0.2.4 lib/esse/index/search.rb