Class: Solr::Connection
- Inherits:
-
Object
- Object
- Solr::Connection
- Defined in:
- lib/solr/connection.rb
Overview
TODO: add a convenience method to POST a Solr .xml file, like Solr’s example post.sh
Constant Summary collapse
- ILLEGAL_XML_CHARS =
/\x00|\x01|\x02|\x03|\x04|\x05|\x06|\x07|\x08|\x0B|\x0C|\x0E|\x0F|\x10|\x11|\x12|\x13|\x14|\x15|\x16|\x17|\x18|\x19|\x1A|\x1B|\x1C|\x1D|\x1E|\x1F/
Instance Attribute Summary collapse
-
#autocommit ⇒ Object
readonly
Returns the value of attribute autocommit.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#add(doc) ⇒ Object
add a document to the index.
-
#commit(options = {}) ⇒ Object
sends a commit message to the server.
-
#delete(document_id) ⇒ Object
delete a document from the index using the document id.
-
#delete_by_query(query) ⇒ Object
delete using a query.
- #info ⇒ Object
-
#initialize(url = "http://localhost:8983/solr", opts = {}) ⇒ Connection
constructor
create a connection to a solr instance using the url for the solr application context:.
-
#optimize ⇒ Object
sends an optimize message to the server.
-
#ping ⇒ Object
pings the connection and returns true/false if it is alive or not.
-
#post(request) ⇒ Object
send the http post request to solr; for convenience there are shortcuts to some requests: add(), query(), commit(), delete() or send().
-
#query(query, options = {}, &action) ⇒ Object
performs a standard query and returns a Solr::Response::Standard.
-
#search(query, options = {}, &action) ⇒ Object
performs a dismax search and returns a Solr::Response::Standard.
-
#send(request) ⇒ Object
send a given Solr::Request and return a RubyResponse or XmlResponse depending on the type of request.
-
#update(doc) ⇒ Object
update a document in the index (really just an alias to add).
Constructor Details
#initialize(url = "http://localhost:8983/solr", opts = {}) ⇒ Connection
create a connection to a solr instance using the url for the solr application context:
conn = Solr::Connection.new("http://example.com:8080/solr")
if you would prefer to have all adds/updates autocommitted, use :autocommit => :on
conn = Solr::Connection.new('http://example.com:8080/solr',
:autocommit => :on)
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/solr/connection.rb', line 33 def initialize(url="http://localhost:8983/solr", opts={}) @url = URI.parse(url) unless @url.kind_of? URI::HTTP raise "invalid http url: #{url}" end # TODO: Autocommit seems nice at one level, but it currently is confusing because # only calls to Connection#add/#update/#delete, though a Connection#send(AddDocument.new(...)) # does not autocommit. Maybe #send should check for the request types that require a commit and # commit in #send instead of the individual methods? @autocommit = opts[:autocommit] == :on # Not actually opening the connection yet, just setting up the persistent connection. @connection = Net::HTTP.new(@url.host, @url.port) @connection.read_timeout = opts[:timeout] if opts[:timeout] end |
Instance Attribute Details
#autocommit ⇒ Object (readonly)
Returns the value of attribute autocommit.
18 19 20 |
# File 'lib/solr/connection.rb', line 18 def autocommit @autocommit end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
18 19 20 |
# File 'lib/solr/connection.rb', line 18 def connection @connection end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
18 19 20 |
# File 'lib/solr/connection.rb', line 18 def url @url end |
Instance Method Details
#add(doc) ⇒ Object
add a document to the index. you can pass in either a hash
conn.add(:id => 123, :title => 'Tlon, Uqbar, Orbis Tertius')
or a Solr::Document
conn.add(Solr::Document.new(:id => 123, :title = 'On Writing')
true/false will be returned to designate success/failure
61 62 63 64 65 66 |
# File 'lib/solr/connection.rb', line 61 def add(doc) request = Solr::Request::AddDocument.new(doc) response = send(request) commit if @autocommit return response.ok? end |
#commit(options = {}) ⇒ Object
sends a commit message to the server
111 112 113 114 |
# File 'lib/solr/connection.rb', line 111 def commit(={}) response = send(Solr::Request::Commit.new()) return response.ok? end |
#delete(document_id) ⇒ Object
delete a document from the index using the document id
133 134 135 136 137 |
# File 'lib/solr/connection.rb', line 133 def delete(document_id) response = send(Solr::Request::Delete.new(:id => document_id)) commit if @autocommit response.ok? end |
#delete_by_query(query) ⇒ Object
delete using a query
140 141 142 143 144 |
# File 'lib/solr/connection.rb', line 140 def delete_by_query(query) response = send(Solr::Request::Delete.new(:query => query)) commit if @autocommit response.ok? end |
#info ⇒ Object
146 147 148 |
# File 'lib/solr/connection.rb', line 146 def info send(Solr::Request::IndexInfo.new) end |
#optimize ⇒ Object
sends an optimize message to the server
117 118 119 120 |
# File 'lib/solr/connection.rb', line 117 def optimize response = send(Solr::Request::Optimize.new) return response.ok? end |
#ping ⇒ Object
pings the connection and returns true/false if it is alive or not
123 124 125 126 127 128 129 130 |
# File 'lib/solr/connection.rb', line 123 def ping begin response = send(Solr::Request::Ping.new) return response.ok? rescue return false end end |
#post(request) ⇒ Object
send the http post request to solr; for convenience there are shortcuts to some requests: add(), query(), commit(), delete() or send()
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/solr/connection.rb', line 159 def post(request) if ENV["DEBUG"] puts "POST #{@url.path + "/" + request.handler}" puts "-- DATA -------------------" puts request.to_s puts "-- END DATA ---------------" end response = @connection.post(@url.path + "/" + request.handler, request.to_s.gsub(ILLEGAL_XML_CHARS, ''), { "Content-Type" => request.content_type }) case response when Net::HTTPSuccess then response.body else response.error! end end |
#query(query, options = {}, &action) ⇒ Object
performs a standard query and returns a Solr::Response::Standard
response = conn.query('borges')
alternative you can pass in a block and iterate over hits
conn.query('borges') do |hit|
puts hit
end
options include:
:sort, :default_field, :rows, :filter_queries, :debug_query,
:explain_other, :facets, :highlighting, :mlt,
:operator => :or / :and
:start => defaults to 0
:field_list => array, defaults to ["*", "score"]
92 93 94 95 |
# File 'lib/solr/connection.rb', line 92 def query(query, ={}, &action) # TODO: Shouldn't this return an exception if the Solr status is not ok? (rather than true/false). create_and_send_query(Solr::Request::Standard, .update(:query => query), &action) end |
#search(query, options = {}, &action) ⇒ Object
performs a dismax search and returns a Solr::Response::Standard
response = conn.search('borges')
options are same as query, but also include:
:tie_breaker, :query_fields, :minimum_match, :phrase_fields,
:phrase_slop, :boost_query, :boost_functions
106 107 108 |
# File 'lib/solr/connection.rb', line 106 def search(query, ={}, &action) create_and_send_query(Solr::Request::Dismax, .update(:query => query), &action) end |
#send(request) ⇒ Object
send a given Solr::Request and return a RubyResponse or XmlResponse depending on the type of request
152 153 154 155 |
# File 'lib/solr/connection.rb', line 152 def send(request) data = post(request) Solr::Response::Base.make_response(request, data) end |
#update(doc) ⇒ Object
update a document in the index (really just an alias to add)
70 71 72 |
# File 'lib/solr/connection.rb', line 70 def update(doc) return add(doc) end |