Module: Spice::Connection::Search

Included in:
Spice::Connection
Defined in:
lib/spice/connection/search.rb

Instance Method Summary collapse

Instance Method Details

#search(index, options = Mash.new) ⇒ Object

Parameters:

  • options (Hash) (defaults to: Mash.new)

    a customizable set of options

Options Hash (options):

  • :q (String)

    The Solr search query string

  • :sort (String)

    Order by which to sort the results

  • :start (Numeric)

    The number by which to offset the results

  • :rows (Numeric)

    The maximum number of rows to return


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/spice/connection/search.rb', line 10

def search(index, options=Mash.new)
  index = index.to_s
  options = {:q => options} if options.is_a? String
  options.symbolize_keys!

  options[:q] ||= '*:*'
  options[:sort] ||= "X_CHEF_id_CHEF_X asc"
  options[:start] ||= 0
  options[:rows] ||= 1000

  # clean up options hash
  options.delete_if{|k,v| !%w(q sort start rows).include?(k.to_s)}

  params = options.collect{ |k, v| "#{k}=#{CGI::escape(v.to_s)}"}.join("&")
  case index
  when 'node'
    get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |node|
      Spice::Node.get_or_new(node)
    end
  when 'role'
    get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |role|
      Spice::Role.get_or_new(role)
    end
  when 'client'
    get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |client|
      Spice::Client.get_or_new(client)
    end
  when 'environment'
    get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |env|
      env['attrs'] = env.delete('attributes')
      Spice::Environment.get_or_new(env)
    end
  else
    # assume it's a data bag
    get("/search/#{CGI::escape(index.to_s)}?#{params}")['rows'].map do |db|
      data = db['raw_data']
      Spice::DataBagItem.get_or_new(data)
    end
  end
end