Class: Traject::SolrJWriter
- Inherits:
-
Object
- Object
- Traject::SolrJWriter
- Includes:
- QualifiedConstGet
- Defined in:
- lib/traject/solrj_writer.rb
Overview
Writes to a Solr using SolrJ, and the SolrJ HttpSolrServer.
(sub-class later for the ConcurrentUpdate server?)
settings:
[solr.url] Your solr url (required)
[solrj_writer.server_class_name] Defaults to "HttpSolrServer". You can specify
another Solr Server sub-class, but it has
to take a one-arg url constructor. Maybe
subclass this writer class and overwrite
instantiate_solr_server! otherwise
[solrj.jar_dir] Custom directory containing all of the SolrJ jars. All
jars in this dir will be loaded. Otherwise,
we load our own packaged solrj jars. This setting
can't really be used differently in the same app instance,
since jars are loaded globally.
[solrj_writer.parser_class_name] A String name of a class in package
org.apache.solr.client.solrj.impl,
we'll instantiate one with a zero-arg
constructor, and pass it as an arg to setParser on
the SolrServer instance, if present.
NOTE: For contacting a Solr 1.x server, with the
recent version of SolrJ used by default, set to
"XMLResponseParser"
[solrj_writer.commit_on_close] If true (or string 'true'), send a commit to solr
at end of #process.
Instance Attribute Summary collapse
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
- #solr_server ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
-
#ensure_solrj_loaded! ⇒ Object
Loads solrj if not already loaded.
-
#initialize(argSettings) ⇒ SolrJWriter
constructor
A new instance of SolrJWriter.
- #instantiate_solr_server! ⇒ Object
- #put(hash) ⇒ Object
- #settings_check!(settings) ⇒ Object
Methods included from QualifiedConstGet
Constructor Details
#initialize(argSettings) ⇒ SolrJWriter
Returns a new instance of SolrJWriter.
35 36 37 38 39 40 41 42 |
# File 'lib/traject/solrj_writer.rb', line 35 def initialize(argSettings) @settings = argSettings settings_check!(settings) ensure_solrj_loaded! solr_server # init end |
Instance Attribute Details
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
33 34 35 |
# File 'lib/traject/solrj_writer.rb', line 33 def settings @settings end |
#solr_server ⇒ Object
95 96 97 |
# File 'lib/traject/solrj_writer.rb', line 95 def solr_server @solr_server ||= instantiate_solr_server! end |
Instance Method Details
#close ⇒ Object
87 88 89 90 91 92 |
# File 'lib/traject/solrj_writer.rb', line 87 def close solr_server.commit if settings["solrj_writer.commit_on_close"].to_s == "true" solr_server.shutdown @solr_server = nil end |
#ensure_solrj_loaded! ⇒ Object
Loads solrj if not already loaded. By loading all jars found in settings
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/traject/solrj_writer.rb', line 46 def ensure_solrj_loaded! unless defined?(HttpSolrServer) && defined?(SolrInputDocument) require 'java' tries = 0 begin tries += 1 java_import org.apache.solr.client.solrj.impl.HttpSolrServer java_import org.apache.solr.common.SolrInputDocument rescue NameError => e # /Users/jrochkind/code/solrj-gem/lib" included_jar_dir = File.("../../vendor/solrj/lib", File.dirname(__FILE__)) jardir = settings["solrj.jar_dir"] || included_jar_dir Dir.glob("#{jardir}/*.jar") do |x| require x end if tries > 1 raise LoadError.new("Can not find SolrJ java classes") else retry end end end end |
#instantiate_solr_server! ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/traject/solrj_writer.rb', line 102 def instantiate_solr_server! server_class = qualified_const_get( settings["solrj_writer.server_class_name"] || "HttpSolrServer" ) server = server_class.new( settings["solr.url"].to_s ); if parser_name = settings["solrj_writer.parser_class_name"] parser = org.apache.solr.client.solrj.impl.const_get(parser_name).new server.setParser( parser ) end server end |
#put(hash) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/traject/solrj_writer.rb', line 73 def put(hash) doc = SolrInputDocument.new hash.each_pair do |key, value_array| value_array.each do |value| doc.addField( key, value ) end end # TODO: Buffer docs internally, add in arrays, one http # transaction per array. Is what solrj wiki recommends. solr_server.add(doc) end |
#settings_check!(settings) ⇒ Object
114 115 116 117 118 |
# File 'lib/traject/solrj_writer.rb', line 114 def settings_check!(settings) unless settings.has_key?("solr.url") && ! settings["solr.url"].nil? raise ArgumentError.new("SolrJWriter requires a 'solr.url' solr url in settings") end end |