Sunspot Showed Me the Light

admin » 20 May 2010 » In rails »

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:

  1. kindergarden easy index control
  2. keyword search, ordering, scoping, awesomeing
  3. indexing custom/dynamic field goodness
  4. DSL which feels like your native toungue
  5. faceting for days, CUSTOM FACETS!!
  6. extensible, pick an ORM, any ORM
  7. will_play with will_paginate
  8. 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!

Tags: , , , ,

Trackback URL

9 Comments on "Sunspot Showed Me the Light"

  1. bennyfreshness
    Esme
    20/05/2010 at 7:51 am Permalink

    Nicely written…

  2. bennyfreshness
    Reality
    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.

  3. bennyfreshness
    admin
    26/05/2010 at 12:16 am Permalink

    Sunspot is just a Ruby API for Solr which is written in Java.

  4. bennyfreshness
    RV
    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

  5. bennyfreshness
    admin
    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!

  6. bennyfreshness
    TĂșlio Ornelas
    05/11/2010 at 11:15 am Permalink

    Hi, I am using sunspot with solr too but how do you work with querys that use some operators, like: … keywords ‘street OR village’ or more complex queries keywords ‘location AND (street OR village).

    o/

  7. bennyfreshness
    admin
    08/11/2010 at 11:53 am Permalink

    I’ve had some good luck with “negative” queries in a past life before sunspot..

    e.g. -city:paris

    this article might be useful to you..

    http://khaidoan.wikidot.com/solr-boolean-operators

  8. bennyfreshness
    TĂșlio Ornelas
    08/11/2010 at 1:28 pm Permalink

    I already found a solution, take a look:

    Sunspot.search(MyModel) do
    adjust_solr_params do |params|
    params['q'] = query
    end
    end

    o/

  9. bennyfreshness
    lucygenik
    20/05/2013 at 7:37 pm Permalink

    Excellent use of ‘face’. .. love your work :D What the industry really needs is more stiff asshats like RV.

Hi Stranger, leave a comment:

ALLOWED XHTML TAGS:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to Comments