Sha256: 1c0af4fdb048caa4a0848cc91fca490958e2f1935196ac95ed24f09a4cbe9f2f

Contents?: true

Size: 1.2 KB

Versions: 8

Compression:

Stored size: 1.2 KB

Contents

module ChefAPI
  class Resource::PartialSearch < Resource::Base
    collection_path "/search/:index"

    schema do
      attribute :total, type: Integer
      attribute :start, type: Integer
      attribute :rows,  type: Array
    end

    class << self
      #
      # About search : https://docs.chef.io/chef_search.html
      #
      # @param [String] index
      #   the name of the index to search
      # @param [Hash] keys
      #   key paths for the attributes to be returned
      # @param [String] query
      #   the query string
      # @param [Hash] options
      #   the query string
      #
      # @return [self]
      #   the current resource
      #
      def query(index, keys, query = "*:*", options = {})
        return nil if index.nil?

        params = {}.tap do |o|
          o[:q]     = query
          o[:rows]  = options[:rows]  || 1000
          o[:sort]  = options[:sort]  || "X_CHEF_id_CHEF_X"
          o[:start] = options[:start] || 0
        end

        path = expanded_collection_path(index: index.to_s)
        response = connection.post(path, keys.to_json, params)
        response["rows"].map! { |row| row["data"] }
        from_json(response, index: index.to_s)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
chef-infra-api-0.10.10 lib/chef-api/resources/partial_search.rb
chef-api-0.10.10 lib/chef-api/resources/partial_search.rb
chef-infra-api-0.10.7 lib/chef-api/resources/partial_search.rb
chef-api-0.10.7 lib/chef-api/resources/partial_search.rb
chef-infra-api-0.10.5 lib/chef-api/resources/partial_search.rb
chef-api-0.10.5 lib/chef-api/resources/partial_search.rb
chef-infra-api-0.10.2 lib/chef-api/resources/partial_search.rb
chef-api-0.10.2 lib/chef-api/resources/partial_search.rb