Sunspot Showed Me the Light
Ladies and gentleman, gone are the dark ages of using SQL (a known gateway drug) to perform searches. Thats right, I’m here today to spotlight Sunspot, “a Ruby library for expressive, powerful interaction with the Solr search engine.”
Ever since that fated column of light struck my monitor and forced my browser to google Solr, I’ve had the strength to overcome my addiction to SQL searches. Gone are the days of “select intervention_date from developers where search_method like ‘%sql%’”
Get out of that darkness cupcake! Let some light brighten up that dark and dreary world of murky sql search statements and enter a radiant and luminous world of vibrant Sunspot search!
We can get through this together…
Step 1: Installation
gem install sunspot gem install sunspot_rails
Step 2: Define What to Index
class Rehab
#...
end
Sunspot.setup(Rehab) do
text :addiction
integer :relapses
float :relapse_average
time :admitted_at
string :cure do
addiction.gsub(/(darkness|clouds|shadows)/, 'sunshine')
end
end
Step 3: Search
search = Sunspot.search Rehab do
keywords 'light sunshine'
with :patient_name, 'Benny Freshness'
with(:relapses).any_of [2, 14]
with(:relapse_average).less_than 4
any_of do
with(:admitted_at).less_than(Time.now)
end
without :cure, 'Moar SQL'
order_by :average_relapse, :desc
facet :cure
end
Step 4: Profit
In all cerealness, I’d like to shed light on the line ‘facet :cure’ for a lightsecond as this really illuminates the vibrance of Sunspot. A “facet” is a glint of search aurora. Say my search lights up 45 results, by faceting on cure, we can get tallies of how many of each distinct cures are retuned in the result set e.g ‘moar SQL’:10, ‘Solr’:5, ‘Sunspot’:25, ‘Zippo’:1
By faceting we can irradiate our result set and re-kindle that old flame for search burning inside us all.
Furthermore, feast on these tasty Sunspot nuggets:
- kindergarden easy index control
- keyword search, ordering, scoping, awesomeing
- indexing custom/dynamic field goodness
- DSL which feels like your native toungue
- faceting for days, CUSTOM FACETS!!
- extensible, pick an ORM, any ORM
- will_play with will_paginate
- order by field value, relevance, geographical distance, or your face
Well folks, thats all I can illuminate for you here today. Hopefully my annoying “light” analogies don’t have you plotting to kill me by now. I also hope I cast some light on something you might be able to use or find interesting.
So remember, use the force, don’t join the dark side of SQL searching, step into the magnificent light of Sunspot!
20/05/2010 at 7:51 am Permalink
Nicely written…
20/05/2010 at 9:17 am Permalink
It works real well with a large database too….just kidding. Like most ORM/Ruby it fails and turns into a pile of shit under load and large data set.
26/05/2010 at 12:16 am Permalink
Sunspot is just a Ruby API for Solr which is written in Java.
25/07/2010 at 11:58 pm Permalink
language of this article sucks !! you should just stick to explaining the solr and not confuse everyone with your flowery crap
04/08/2010 at 9:45 pm Permalink
sorry RV, guess my vivid enlightening of solr didn’t conform to your dreary world my dark lord… another approach could be asking me specific questions here in the comments instead of creating darkness, shine baby!