Skip to content

Instantly share code, notes, and snippets.

@arafalov arafalov/test.rb
Created Jun 18, 2014

Embed
What would you like to do?
rsolr basic example
require 'rubygems'
require 'rsolr'
# 1. Connect to an empty example collection
solr = RSolr.connect :url => 'http://localhost:8983/solr'
puts('Before anything')
puts(solr.get 'select', :params => {:q => '*:*'})
# 2. Add two documents with data from ipod_other.xml
solr.add(
:id => "F8V7067-APL-KIT",
:name => "Belkin Mobile Power Cord for iPod w/ Dock",
:manu => "Belkin",
:manu_id_s => "belkin", #dynamic string field
:cat => ["electronics","connector"], #multivalued
:features => "car power adapter, white",
:price => 19.95, #float
:popularity => 1, #int
:inStock => false, #boolean
:store => "45.18014,-93.87741", #location
:manufacturedate_dt => "2005-08-01T16:30:25Z" # datatime,
)
# Can also add an array of hashes/objects
solr.add([{
:id => "IW-02",
:name =>"iPod & iPod Mini USB 2.0 Cable", # will & need to be escaped?
:manu => "Belkin",
:manu_id_s => "belkin", #dynamic string field
:cat => ["electronics","connector"], #multivalued
:features => "car power adapter for iPod, white",
:price => 11.50, #float
:popularity => 1, #int
:inStock => true, #boolean, the other one
:store => "37.7752,-122.4232", #location
:manufacturedate_dt => "2006-02-14T23:55:59Z" # datatime,
}])
solr.commit
# 3. Query all documents (*:*) and print them out
puts('Inserted:')
response = solr.get 'select', :params => {:q => '*:*'}
puts response
response["response"]["docs"].each{|doc| puts doc }
# 4. Query by price = 11.50 should return single document
puts('Query by price:')
response = solr.get 'select', :params => {:q => "price:11.50"}
puts(response)
# 5. Delete all documents
solr.delete_by_query("*:*")
solr.update :data => '<commit/>' # The other way to do commit and XML control commands
puts('Deleted:')
puts(solr.get 'select', :params => {:q => '*:*'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.