# class Chef::Search::Query

### Attributes

rest[RW]

### Public Class Methods

new(url=nil) click to toggle source

    # File lib/chef/search/query.rb, line 33
    def initialize(url=nil)
      @rest = Chef::REST.new(url ||Chef::Config[:chef_server_url])
    end

### Public Instance Methods

list_indexes() click to toggle source

    # File lib/chef/search/query.rb, line 55
    def list_indexes
      @rest.get_rest("search")
    end

search(type, query="*:*", sort='X_CHEF_id_CHEF_X asc', start=0, rows=1000, &block) click to toggle source

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.

    # File lib/chef/search/query.rb, line 39
    def search(type, query="*:*", sort='X_CHEF_id_CHEF_X asc', start=0, rows=1000, &block)
      raise ArgumentError, "Type must be a string or a symbol!" unless (type.kind_of?(String) || type.kind_of?(Symbol))

      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