Class: Chef::PartialSearch
- Inherits:
-
Object
- Object
- Chef::PartialSearch
- Defined in:
- lib/chef/search/partial_search.rb
Instance Attribute Summary collapse
-
#rest ⇒ Object
Returns the value of attribute rest.
Instance Method Summary collapse
-
#initialize(url = nil) ⇒ PartialSearch
constructor
A new instance of PartialSearch.
- #list_indexes ⇒ Object
-
#search(type, query = '*:*', args = {}, &block) ⇒ Object
Search Solr for objects of a given type, for a given query.
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
#rest ⇒ Object
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_indexes ⇒ Object
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.
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 |