Skip to content

Instantly share code, notes, and snippets.

@arafalov
Created June 18, 2014 05:46
Show Gist options
  • Save arafalov/1a4d1841dd8c83b614f4 to your computer and use it in GitHub Desktop.
Save arafalov/1a4d1841dd8c83b614f4 to your computer and use it in GitHub Desktop.
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