Class: Chef::PartialSearch

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url = nil) ⇒ PartialSearch

Returns a new instance of PartialSearch.



35
36
37
# File 'lib/chef/search/partial_search.rb', line 35

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.



33
34
35
# File 'lib/chef/search/partial_search.rb', line 33

def rest
  @rest
end

Instance Method Details

#list_indexesObject



73
74
75
# File 'lib/chef/search/partial_search.rb', line 73

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

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

Search Solr for objects of a given type, for a given query. If you give it a block, it will handle the paging for you dynamically.

Raises:

  • (ArgumentError)


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
67
68
69
70
71
# File 'lib/chef/search/partial_search.rb', line 41

def search(type, query='*:*', args={}, &block)
  raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol))

  sort = args.include?(:sort) ? args[:sort] : 'X_CHEF_id_CHEF_X asc'
  start = args.include?(:start) ? args[:start] : 0
  rows = args.include?(:rows) ? args[:rows] : 1000
  query_string = "search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}"
  if args[:keys]
    response = @rest.post_rest(query_string, args[:keys])
    response_rows = response['rows'].map { |row| row['data'] }
  else
    response = @rest.get_rest(query_string)
    response_rows = response['rows']
  end
  if block
    response_rows.each { |o| block.call(o) unless o.nil?}
    unless (response["start"] + response_rows.length) >= response["total"]
      nstart = response["start"] + rows
      args_hash = {
        :keys => args[:keys],
        :sort => sort,
        :start => nstart,
        :rows => rows
      }
      search(type, query, args_hash, &block)  
    end
    true
  else
    [ response_rows, response["start"], response["total"] ]
  end
end