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.



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

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

Instance Attribute Details

#restObject

Returns the value of attribute rest.



30
31
32
# File 'lib/chef/search/query.rb', line 30

def rest
  @rest
end

Instance Method Details

#list_indexesObject



57
58
59
# File 'lib/chef/search/query.rb', line 57

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

#search(type, query = "*:*", sort = nil, start = 0, rows = 20, &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.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/chef/search/query.rb', line 39

def search(type, query="*:*", sort=nil, start=0, rows=20, &block)
  unless type.kind_of?(String) || type.kind_of?(Symbol)
    raise ArgumentError, "Type must be a string or a symbol!" 
  end

  response = @rest.get_rest("search/#{type}?q=#{escape(query)}&sort=#{escape(sort)}&start=#{escape(start)}&rows=#{escape(rows)}")
  if block
    response["rows"].each { |o| block.call(o) unless o.nil?}
    unless (response["start"] + response["rows"].length) >= response["total"]
      nstart = response["start"] + rows
      search(type, query, sort, nstart, rows, &block)
    end
    true
  else
    [ response["rows"], response["start"], response["total"] ]
  end
end