Class: Chef::Search::Query

Inherits:
Object show all
Defined in:
lib/chef/search/query.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ Query

Returns a new instance of Query.



34
35
36
# File 'lib/chef/search/query.rb', line 34

def initialize(url=nil)
  @rest = Chef::REST.new(url ||Chef::Config[:chef_server_url])
end

Instance Attribute Details

#restObject

Returns the value of attribute rest.



32
33
34
# File 'lib/chef/search/query.rb', line 32

def rest
  @rest
end

Instance Method Details

#list_indexesObject



110
111
112
# File 'lib/chef/search/query.rb', line 110

def list_indexes
  @rest.get_rest("search")
end

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

This search is only kept for backwards compatibility, since the results of the new filtered search method will be in a slightly different format



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/chef/search/query.rb', line 41

def partial_search(type, query='*:*', *args, &block)
  Chef::Log.warn("DEPRECATED: The 'partial_search' api is deprecated, please use the search api with 'filter_result'")
  # accept both types of args
  if args.length == 1 && args[0].is_a?(Hash)
    args_hash = args[0].dup
    # partial_search implemented in the partial search cookbook uses the
    # arg hash :keys instead of :filter_result to filter returned data
    args_hash[:filter_result] = args_hash[:keys]
  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

  unless block.nil?
    raw_results = search(type,query,args_hash)
  else
    raw_results = search(type,query,args_hash,&block)
  end
  results = Array.new
  raw_results[0].each do |r|
    results << r["data"]
  end
  return results
end

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

New search input, designed to be backwards compatible with the old method signature ‘type’ and ‘query’ are the same as before, args now will accept either a Hash of search arguments with symbols as the keys (ie :sort, :start, :rows) and a :filter_result option.

:filter_result should be in the format of another Hash with the structure of:

:returned_name1 => ["path", "to", "variable"],
:returned_name2 => ["shorter", "path"]

a real world example might be something like:

:ip_address => ["ipaddress"],
:ruby_version => ["languages", "ruby", "version"]

this will bring back 2 variables 'ip_address' and 'ruby_version' with whatever value was found

an example of the returned json may be: “ruby_version”: “1.9.3”



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chef/search/query.rb', line 88

def search(type, query='*:*', *args, &block)
  validate_type(type)
  validate_args(args)

  scrubbed_args = Hash.new

  # argify everything
  if args[0].kind_of?(Hash)
    scrubbed_args = args[0]
  else
    # This api will be deprecated in a future release
    scrubbed_args = { :sort => args[0], :start => args[1], :rows => args[2] }
  end

  # set defaults, if they haven't been set yet.
  scrubbed_args[:sort] ||= 'X_CHEF_id_CHEF_X asc'
  scrubbed_args[:start] ||= 0
  scrubbed_args[:rows] ||= 1000

  do_search(type, query, scrubbed_args, &block)
end