Ruby Client API

Like JavaScript and SolrJ, Ruby can also connect with Solr using the Solr API and performs various operations such as search, indexing, and removal over the HTTP protocol. Solr also provides sufficient API support for the Ruby programming language. So, the application that builds on a Ruby platform can also take advantage of Solr. Solr reverts a query response in Ruby format, which can be interpreted easily in Ruby programming. 

For Ruby applications, to communicate with Solr, rsolr is an extensive library. It provides all the functionalities needed to meet your expectations. To install the rsolr dependency, please refer to http://www.rubydoc.info/gems/rsolr. To install rsolr, use this command:

gem install rsolr

Now let's search using rsolr for the query q=ipod:

require 'rsolr'

solr = RSolr.connect :url => 'http://localhost:8983/solr/techproducts'

response = solr.get 'select', :params => {:q => 'ipod',:fl=>'id,name',:wt=>:ruby}

puts response

Response:

{"responseHeader"=>{"status"=>0, "QTime"=>2, "params"=>{"q"=>"ipod", "fl"=>"id,name", "wt"=>"ruby"}}, "response"=>{"numFound"=>3, "start"=>0, "docs"=>[{"id"=>"IW-02", "name"=>"iPod & iPod Mini USB 2.0 Cable"}, {"id"=>"F8V7067-APL-KIT", "name"=>"Belkin Mobile Power Cord for iPod w/ Dock"}, {"id"=>"MA147LL/A", "name"=>"Apple 60 GB iPod with Video Playback Black"}]}, "spellcheck"=>{"suggestions"=>[], "correctlySpelled"=>false, "collations"=>[]}}

Solr treats the response format based on the :wt parameter value. For :wt=>:ruby, the response format will be a hash (a similar object returned by Solr but evaluated as Ruby), and for :wt=>"ruby", the response will be a string. For other formats, Solr returns the response as expected. 

Now let's try to add a document (index) to Solr:

require 'rsolr'

solr = RSolr.connect :url => 'http://localhost:8983/solr/techproducts'

response = solr.add(:id=>'HPPRO445', :name=>'HP Probook 445', :manu=>'Hewlett Packard', :features=>'8GB DDR3LSD RAM', :weight=>1.2, :price=>800)

solr.commit

puts response

After adding, if we search for the query q=HP Probook 445, it will give the following response, provided it was added successfully:

{"responseHeader"=>{"status"=>0, "QTime"=>4, "params"=>{"q"=>"HP Probook 445", "wt"=>"ruby"}}, "response"=>{"numFound"=>1, "start"=>0, "docs"=>[{"id"=>"HPPRO445", "name"=>"HP Probook 445", "manu"=>"Hewlett Packard", "features"=>["8GB DDR3LSD RAM"], "weight"=>1.2, "price"=>800.0, "price_c"=>"800,USD", "_version_"=>1591126555425243136, "price_c____l_ns"=>80000}]}, "spellcheck"=>{"suggestions"=>[], "correctlySpelled"=>false, "collations"=>[]}}

After adding/updating/deleting, don't forgot to commit the transaction using solr.commit(), or else the indexes will not be affected.

We can add multiple documents at a time:

require 'rsolr'

solr = RSolr.connect :url => 'http://localhost:8983/solr/techproducts'

response = solr.add([
{ :id => 'id1', :name => "product1"},
{ :id => 'id2', :name => "product2"},
{ :id => 'idn', :name => "productn"}
])

solr.commit

puts response

Now delete a document using rsolr, like this:

require 'rsolr'

solr = RSolr.connect :url => 'http://localhost:8983/solr/techproducts'

response = solr.delete_by_id 'HPPRO445'

solr.commit

puts response

Now if we search for the query q=HP Probook 445, it should not give any response for the preceding ID if it was deleted successfully. In the same way, we can delete multiple documents in a single transaction:

require 'rsolr'

solr = RSolr.connect :url => 'http://localhost:8983/solr/techproducts'

response = solr.delete_by_id ['id1','id2']

solr.commit

puts response

Here we have explored various Solr APIs using basic Ruby programming steps. Now, referring to this, we can take a deep dive and add more configurations to Ruby and Solr API connectivity to achieve the expected results.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.183.252