Top Level Namespace

Defined Under Namespace

Modules: KnifePSearch Classes: Chef, Psearch

Instance Method Summary collapse

Instance Method Details

#partial_search(type, query = '*:*', *args, &block) ⇒ Object

partial_search(type, query, options, &block)

Searches for nodes, roles, etc. and returns the results. This method may perform more than one search request, if there are a large number of results.

Parameters

  • type: index type (:role, :node, :client, :environment, data bag name)

  • query: SOLR query. “:”, “role:blah”, “not role:blah”, etc. Defaults to ‘:

  • options: hash with options:

** :start: First row to return (:start => 50, :rows => 100 means “return the

50th through 150th result")

** :rows: Number of rows to return. Defaults to 1000. ** :sort: a SOLR sort specification. Defaults to ‘X_CHEF_id_CHEF_X asc’. ** :keys: partial search keys. If this is not specified, the search will

not be partial.

Returns

This method returns an array of search results. Partial search results will be JSON hashes with the structure specified in the keys option. Other results include Chef::Node, Chef::Role, Chef::Client, Chef::Environment, Chef::DataBag and Chef::DataBagItem objects, depending on the search type.

If a block is specified, the block will be called with each result instead of returning an array. This method will not block if it returns

If start or row is specified, and no block is given, the result will be a triple containing the list, the start and total:

[ [ row1, row2, ... ], start, total ]

Example

partial_search(:node, 'role:webserver',
               keys: {
                 name: [ 'name' ],
                 ip: [ 'amazon', 'ip', 'public' ]
               }
).each do |node|
  puts "#{node[:name]}: #{node[:ip]}"
end


126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/chef/search/partial_search.rb', line 126

def partial_search(type, query='*:*', *args, &block)
  # Support both the old (positional args) and new (hash args) styles of calling
  if args.length == 1 && args[0].is_a?(Hash)
    args_hash = args[0]
  else
    args_hash = {}
    args_hash[:sort] = args[0] if args.length >= 1
    args_hash[:start] = args[1] if args.length >= 2
    args_hash[:rows] = args[2] if args.length >= 3
  end
  # If you pass a block, or have the start or rows arguments, do raw result parsing
  if Kernel.block_given? || args_hash[:start] || args_hash[:rows]
    Chef::PartialSearch.new.search(type, query, args_hash, &block)
  # Otherwise, do the iteration for the end user
  else
    results = Array.new
    Chef::PartialSearch.new.search(type, query, args_hash) do |o|
      results << o
    end
    results
  end
end