Parent

Included Modules

Class/Module Index [+]

Quicksearch

Chef::SolrQuery

Attributes

params[RW]
query[RW]

Public Class Methods

from_params(params, couchdb=nil) click to toggle source
# File lib/chef/solr_query.rb, line 64
def self.from_params(params, couchdb=nil)
  query = new(couchdb)
  query.params = VALID_PARAMS.inject({}) do |p, param_name|
    p[param_name] = params[param_name] if params.key?(param_name)
    p
  end
  query.update_filter_query_from_params
  query.update_query_from_params
  query
end
new(couchdb = nil) click to toggle source

Create a new Query object - takes the solr_url and optional Chef::CouchDB object to inflate objects into.

# File lib/chef/solr_query.rb, line 45
def initialize(couchdb = nil)
  @filter_query = {}
  @params = {}

  if couchdb.nil?
    @database = Chef::Config[:couchdb_database]
    @couchdb = Chef::CouchDB.new(nil, Chef::Config[:couchdb_database])
  else
    unless couchdb.respond_to?(:couchdb_database)
      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

Public Instance Methods

commit(opts={}) click to toggle source
# File lib/chef/solr_query.rb, line 140
def commit(opts={})
  SolrHTTPRequest.update("#{START_XML}#{COMMIT}")
end
delete_database(db) click to toggle source
# File lib/chef/solr_query.rb, line 144
def delete_database(db)
  query_data = xml_escape("X_CHEF_database_CHEF_X:#{db}")
  xml = "#{START_XML}#{START_DELETE_BY_QUERY}#{query_data}#{END_DELETE_BY_QUERY}"
  SolrHTTPRequest.update(xml)
  commit
end
filter_by(filter_query_params) click to toggle source
# File lib/chef/solr_query.rb, line 75
def filter_by(filter_query_params)
  filter_query_params.each do |key, value|
    @filter_query[FILTER_PARAM_MAP[key]] = value
  end
end
filter_by_type(type) click to toggle source
# File lib/chef/solr_query.rb, line 85
def filter_by_type(type)
  case type
  when *BUILTIN_SEARCH_TYPES
    filter_by(:type => type)
  else
    filter_by(:type => DATA_BAG_ITEM, :data_bag => type)
  end
end
filter_query() click to toggle source
# File lib/chef/solr_query.rb, line 81
def filter_query
  @filter_query.map { |param, value| "+#{param}:#{value}" }.join(' ')
end
object_ids() click to toggle source
# File lib/chef/solr_query.rb, line 114
def object_ids
  @object_ids ||= results["response"]["docs"].map { |d| d[ID_KEY] }
end
objects() click to toggle source
# File lib/chef/solr_query.rb, line 104
def objects
  if !object_ids.empty?
    @bulk_objects ||= @couchdb.bulk_get(object_ids)
    Chef::Log.debug { "Bulk get of objects: #{@bulk_objects.inspect}" }
    @bulk_objects
  else
    []
  end
end
rebuild_index(db=Chef::Config[:couchdb_database]) click to toggle source
# File lib/chef/solr_query.rb, line 151
def rebuild_index(db=Chef::Config[:couchdb_database])
  delete_database(db)

  results = {}
  [Chef::ApiClient, Chef::Node, Chef::Role, Chef::Environment].each do |klass|
    results[klass.name] = reindex_all(klass) ? "success" : "failed"
  end
  databags = Chef::DataBag.cdb_list(true)
  Chef::Log.info("Reloading #{databags.size.to_s} #{Chef::DataBag} objects into the indexer")
  databags.each { |i| i.add_to_index; i.list(true).each { |x| x.add_to_index } }
  results[Chef::DataBag.name] = "success"
  results
end
reindex_all(klass, metadata={}) click to toggle source
# File lib/chef/solr_query.rb, line 165
def reindex_all(klass, metadata={})
  begin
    items = klass.cdb_list(true)
    Chef::Log.info("Reloading #{items.size.to_s} #{klass.name} objects into the indexer")
    items.each { |i| i.add_to_index }
  rescue Net::HTTPServerException => e
    # 404s are okay, there might not be any of that kind of object...
    if e.message =~ /Not Found/
      Chef::Log.warn("Could not load #{klass.name} objects from couch for re-indexing (this is ok if you don't have any of these)")
      return false
    else
      raise e
    end
  rescue Exception => e
    Chef::Log.fatal("Chef encountered an error while attempting to load #{klass.name} objects back into the index")
    raise e
  end
  true
end
results() click to toggle source
# File lib/chef/solr_query.rb, line 118
def results
  @results ||= SolrHTTPRequest.select(self.to_hash)
end
search() click to toggle source

Search Solr for objects of a given type, for a given query.

# File lib/chef/solr_query.rb, line 123
def search
  { "rows" => objects, "start" => results["response"]["start"],
    "total" => results["response"]["numFound"] }
end
to_hash() click to toggle source
# File lib/chef/solr_query.rb, line 128
def to_hash
  options = DEFAULT_PARAMS.merge(params)
  options[:fq] = filter_query
  options[:q] = @query
  options
end
update_filter_query_from_params() click to toggle source
# File lib/chef/solr_query.rb, line 94
def update_filter_query_from_params
  filter_by(:database => @database)
  filter_by_type(params.delete(:type))
end
update_query_from_params() click to toggle source
# File lib/chef/solr_query.rb, line 99
def update_query_from_params
  original_query = URI.decode(params.delete(:q) || "*:*")
  @query = Chef::SolrQuery::QueryTransform.transform(original_query)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.