Class: Chef::Solr

Inherits:
Object
  • Object
show all
Includes:
Mixin::XMLEscape
Defined in:
lib/chef/solr.rb,
lib/chef/solr/index.rb,
lib/chef/solr/query.rb,
lib/chef/solr/application/solr.rb,
lib/chef/solr/application/indexer.rb,
lib/chef/solr/application/rebuild.rb,
lib/chef/solr/index_queue_consumer.rb

Direct Known Subclasses

Index, Query

Defined Under Namespace

Classes: Application, Index, IndexQueueConsumer, Query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(solr_url = Chef::Config[:solr_url]) ⇒ Solr

Returns a new instance of Solr.



41
42
43
44
45
# File 'lib/chef/solr.rb', line 41

def initialize(solr_url=Chef::Config[:solr_url])
  @solr_url = solr_url
  uri = URI.parse(@solr_url)
  @http = Net::HTTP.new(uri.host, uri.port)
end

Instance Attribute Details

#httpObject

Returns the value of attribute http.



39
40
41
# File 'lib/chef/solr.rb', line 39

def http
  @http
end

#solr_urlObject

Returns the value of attribute solr_url.



39
40
41
# File 'lib/chef/solr.rb', line 39

def solr_url
  @solr_url
end

Instance Method Details

#post_to_solr(doc) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/chef/solr.rb', line 63

def post_to_solr(doc)
  Chef::Log.debug("POSTing document to SOLR:\n#{doc}")
  req = Net::HTTP::Post.new("/solr/update", "Content-Type" => "text/xml")
  req.body = doc.to_s
  res = @http.request(req)
  unless res.kind_of?(Net::HTTPSuccess)
    res.error!
  end
  res
end

#rebuild_index(url = , db = ) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/chef/solr.rb', line 117

def rebuild_index(url=Chef::Config[:couchdb_url], db=Chef::Config[:couchdb_database])
  solr_delete_by_query("X_CHEF_database_CHEF_X:#{db}")
  solr_commit
  
  results = {}
  [Chef::ApiClient, Chef::Node, Chef::OpenIDRegistration, Chef::Role, Chef::WebUIUser].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

#solr_add(data) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chef/solr.rb', line 74

def solr_add(data)
  data = [data] unless data.kind_of?(Array)
  
  Chef::Log.debug("adding to SOLR: #{data.inspect}")
  xml_document = LibXML::XML::Document.new
  xml_add = LibXML::XML::Node.new("add")
  data.each do |doc|
    xml_doc = LibXML::XML::Node.new("doc")
    doc.each do |field, values|
      values = [values] unless values.kind_of?(Array)
      values.each do |v|
        xml_field = LibXML::XML::Node.new("field")
        xml_field["name"] = field
        xml_field.content = xml_escape(v.to_s)
        xml_doc << xml_field
      end
    end
    xml_add << xml_doc
  end
  xml_document.root = xml_add
  post_to_solr(xml_document.to_s(:indent => false))
end

#solr_commit(opts = {}) ⇒ Object



97
98
99
# File 'lib/chef/solr.rb', line 97

def solr_commit(opts={})
  post_to_solr(generate_single_element("commit", opts))
end

#solr_delete_by_id(ids) ⇒ Object



109
110
111
# File 'lib/chef/solr.rb', line 109

def solr_delete_by_id(ids)
  post_to_solr(generate_delete_document("id", ids))
end

#solr_delete_by_query(queries) ⇒ Object



113
114
115
# File 'lib/chef/solr.rb', line 113

def solr_delete_by_query(queries)
  post_to_solr(generate_delete_document("query", queries))
end

#solr_optimize(opts = {}) ⇒ Object



101
102
103
# File 'lib/chef/solr.rb', line 101

def solr_optimize(opts={})
  post_to_solr(generate_single_element("optimize", opts))
end

#solr_rollbackObject



105
106
107
# File 'lib/chef/solr.rb', line 105

def solr_rollback
  post_to_solr(generate_single_element("rollback"))
end

#solr_select(database, type, options = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/chef/solr.rb', line 47

def solr_select(database, type, options={})
  options[:wt] = :ruby
  options[:indent] = "off"
  options[:fq] = if type.kind_of?(Array)
                   "+X_CHEF_database_CHEF_X:#{database} +X_CHEF_type_CHEF_X:#{type[0]} +data_bag:#{type[1]}"
                 else
                   "+X_CHEF_database_CHEF_X:#{database} +X_CHEF_type_CHEF_X:#{type}"
                 end
  select_url = "/solr/select?#{to_params(options)}"
  Chef::Log.debug("Sending #{select_url} to Solr")
  req = Net::HTTP::Get.new(select_url)
  res = @http.request(req)
  res.error! unless res.kind_of?(Net::HTTPSuccess)
  eval(res.body)
end