Class: Chef::Solr::Query
- Inherits:
-
Chef::Solr
- Object
- Chef::Solr
- Chef::Solr::Query
- Defined in:
- lib/chef/solr/query.rb
Constant Summary
Constants inherited from Chef::Solr
CLOSE_FIELD, END_XML, FIELD_ATTR, FIELD_ATTR_END, START_XML, VERSION
Instance Attribute Summary
Attributes inherited from Chef::Solr
Instance Method Summary collapse
-
#initialize(solr_url = Chef::Config[:solr_url], couchdb = nil) ⇒ Query
constructor
Create a new Query object - takes the solr_url and optional Chef::CouchDB object to inflate objects into.
-
#raw(type, options = {}) ⇒ Object
A raw query against CouchDB - takes the type of object to find, and raw Solr options.
-
#search(type, query = "*:*", sort = 'X_CHEF_id_CHEF_X asc', start = 0, rows = 1000, &block) ⇒ Object
Search Solr for objects of a given type, for a given query.
Methods inherited from Chef::Solr
#post_to_solr, #rebuild_index, #solr_add, #solr_commit, #solr_delete_by_id, #solr_delete_by_query, #solr_optimize, #solr_rollback, #solr_select
Constructor Details
#initialize(solr_url = Chef::Config[:solr_url], couchdb = nil) ⇒ Query
Create a new Query object - takes the solr_url and optional Chef::CouchDB object to inflate objects into.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/chef/solr/query.rb', line 35 def initialize(solr_url=Chef::Config[:solr_url], couchdb = nil) super(solr_url) if couchdb.nil? @database = Chef::Config[:couchdb_database] @couchdb = Chef::CouchDB.new(nil, Chef::Config[:couchdb_database]) else unless couchdb.kind_of?(Chef::CouchDB) Chef::Log.warn("Passing the database name to Chef::Solr::Query initialization is deprecated. Please pass in the Chef::CouchDB object instead.") @database = couchdb @couchdb = Chef::CouchDB.new(nil, couchdb) else @database = couchdb.couchdb_database @couchdb = couchdb end end end |
Instance Method Details
#raw(type, options = {}) ⇒ Object
A raw query against CouchDB - takes the type of object to find, and raw Solr options.
You’ll wind up having to page things yourself.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/chef/solr/query.rb', line 56 def raw(type, ={}) qtype = case type when "role",:role,"node",:node,"client",:client type else [ "data_bag_item", type ] end results = solr_select(@database, qtype, ) Chef::Log.debug("Searching #{@database} #{qtype.inspect} for #{.inspect} with results:\n#{results.inspect}") objects = if results["response"]["docs"].length > 0 bulk_objects = @couchdb.bulk_get( results["response"]["docs"].collect { |d| d["X_CHEF_id_CHEF_X"] } ) Chef::Log.debug("bulk get of objects: #{bulk_objects.inspect}") bulk_objects else [] end [ objects, results["response"]["start"], results["response"]["numFound"], results["responseHeader"] ] end |
#search(type, query = "*:*", sort = 'X_CHEF_id_CHEF_X asc', start = 0, rows = 1000, &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.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/chef/solr/query.rb', line 77 def search(type, query="*:*", sort='X_CHEF_id_CHEF_X asc', start=0, rows=1000, &block) = { :q => query, :start => start, :rows => rows } [:sort] = sort if sort && ! sort.empty? objects, start, total, response_header = raw(type, ) if block objects.each { |o| block.call(o) } unless (start + objects.length) >= total nstart = start + rows search(type, query, sort, nstart, rows, &block) end true else [ objects, start, total ] end end |