dcsimg

Sunspot: A Solr-Powered Search Engine for Ruby

Search can make or break your website. Sunspot and Solr give you an intuitive engine that maps directly to your Ruby objects.

As your site amasses content — be it stories, SKUs, or statistics — a tailored, effective, and exacting search engine becomes increasingly vital. Imagine a bookstore that doesn’t index its tomes by title or author, or a clothing retailer that doesn’t index garments by size. Without search, each site is useless. In general, the quality and relevance of search results makes or breaks a site.

Typically, at least some search results are generated by the site’s underlying database. A database can maintain and catalog enormous volumes of data. Thus, an inquiry for an obscure piece of content would likely fall to the database since it’s the canonical repository. Complex, multi-variate queries may also fall to the database as it’s designed specifically for the purpose.

However, a database query can be slow. Like any engineering feat, a database has strengths and weaknesses and one size rarely fits all. Hence, it’s also typical for other actors to provide search results. For instance, a content management application might use an entirely separate engine to index the prose and respond to keyword, phrase, and proximate searches.

Of course, it’s also de rigueur for an application to maintain one or more RAM-resident indices to speed common lookups and preclude repetitive computations. Additionally, software such as memcached provides surrogate memory extending across multiple machines.

Each technique—the (relational or object) database, special-purpose engine, and in-memory data structures—is a viable option; whether one, some, or all of the approaches are valid depends on the application at hand.

You are no doubt familiar with MySQL, SQLite, Oracle, and any number of other database packages. Each is capable, and there are scores of strategies to tune performance, from writing efficient queries, to tuning the database host’s I/O subsystem. You are also likely familiar with memcached, Squid, and other helpful proxies (both figurative and literal) that offload work from the database and application server. You can find plenty of books on the latter subjects.

And what about the specialized search engine? Certainly, there are plenty of commercial text indexers (FAST, Documentum) and even a few open source solutions (Zebra, Lucene, Sphinx), but what if you need to index 350,000 auto parts, 50,000 email messages, or 4,000 grocery items?

If you code in Ruby, Sunspot is an ideal solution for any of those inventories. Created by Mat Brown, Sunspot is an intuitive and expressive domain specific-language for indexing and searching Ruby objects. Sunspot is powered by Solr, the open source enterprise search server based on Lucene. Solr can highlight matches, replicate its index across many servers, shard indices, and, for the purposes of this discussion, perform advanced full-text and faceted searches.

Wanted: Indexing Engine. Apply Within.

To search your data with Sunspot, you create an index for one or more classes, populate the index, and then search using any of the indexed fields. Here’s an example use of Sunspot to index books for sale.

class Book
  # A book includes instance variables for
  # the author, a title, a publisher, an edition, a 10- and 13-digit
  # ISBN number, a blurb, a publication date, and a price.
end

To create an index, call the class method Sunspot.setup(class) block, where class is the name of the Ruby class to catalog, and block is a list of Sunspot declarations (part of Sunspot’s domain-specific language) to describe how Solr should treat one, some, or all class attributes.

Sunspot.setup(Book) do
  text    :author
  text    :blurb
  integer :edition
  string  :isbn10, :isbn13
  float   :price
  time    :published_at
  text    :publisher
  string  :sort_title do
    title.downcase.sub(/^(an?|the)\W+/, '') if title = self.title
  end
  text    :title
end

float and time and the others are Sunspot DSL keywords to define Solr types. Most of the type keywords are eponymous, but string and text require a little clarification. Use the former for values (like a UPC code) where full-text indexing doesn’t make sense; use the latter for full-text fields.

The code above builds an index for all attributes of a Book, since a consumer might want to search or sort on any of those values. The code also adds a virtual attribute, :sort_title, to help improve the readability of search results. You can search the entire text of :title and sort results either by :title or :sort_title.

The next step is to populate the index with data. (The previous step merely describes what fields to index, but does not catalog values). Assuming you’ve created a new collection of books in the array book_list, adding the books to the index requires just two statements.

Sunspot.index( book_list )
Sunspot.commit

The first statement adds each book in the list book_list to the in-memory Solr index. The second statement commits the new additions to the index to the Solr instance’s persistent store.

Sunspot for Ruby includes other methods to manipulate the index, too, such as remove(instances) and remove_all(classes). The former removes one or more instances from the index; a subsequent commit is required to affect the persistent index. (Optionally, you can call remove!(instance) to delete and commit in one fell swoop.) The latter method removes all instances of one or multiple classes from the index, (and has an analog remove_all!(class(es)) to implicitly commit.

And, naturally, there is a method to search and provide a variety of criteria.

Sunspot.search(Book) do
  facet :publisher
  keywords 'Zaphod'
  with(:edition, 1)
  with(:price).less_than 19.99
  order_by :sort_title
end

The query above says, “Search the current index for all first edition books less than $19.99 where ‘Zaphod’ appears in a full-text field, and return the results sorted by the special sorting title.” In addition to those results, the statement facet :publisher also produces a list of publishers whose books match the criteria. You can use a facet to drill down and produce a list of matching books sold by a specific publisher.

Next: Show Me the Code

Comments on "Sunspot: A Solr-Powered Search Engine for Ruby"

Check beneath, are some absolutely unrelated sites to ours, however, they’re most trustworthy sources that we use.

The information and facts mentioned within the report are several of the very best available.

Below you?ll uncover the link to some sites that we think you need to visit.

Every when inside a whilst we pick out blogs that we read. Listed beneath would be the most up-to-date sites that we choose.

Usually posts some extremely interesting stuff like this. If you are new to this site.

Very couple of web-sites that take place to be in depth below, from our point of view are undoubtedly very well worth checking out.

The info talked about inside the write-up are some of the very best accessible.

Here are some hyperlinks to sites that we link to due to the fact we believe they may be worth visiting.

Check beneath, are some entirely unrelated web-sites to ours, nonetheless, they’re most trustworthy sources that we use.

When some one searches for his essential thing, thus he/she wants to
be available that in detail, so that thing is maintained over here.

Below you?ll obtain the link to some internet sites that we consider it is best to visit.

Hi, i think that i saw you visited my blog thus i came to “return the favor”.I’m trying to find things to improve my web site!I suppose
its ok to use some of your ideas!!

Greate pieces. Keep writing such kind of info on your page.
Im really impressed by your blog.
Hello there, You have done an incredible job.
I’ll certainly digg it and individually recommend to my friends.
I am sure they will be benefited from this website.

Here is my website … graphic designer salary range; stellaslillablogg.bloggplatsen.se,

I just want to mention I’m very new to blogging and site-building and really savored you’re web blog. More than likely I’m want to bookmark your website . You absolutely have exceptional writings. Bless you for sharing your website page.

Every after in a whilst we opt for blogs that we read. Listed beneath would be the latest sites that we choose.

Wow! This blog looks exactly like my old one! It’s on a entirely different subject but it has pretty
much the same page layout and design. Superb choice of colors!

My web-site … the best skype ip resolver free

That is a very good tip particularly to those new to the blogosphere.

Short but very accurate info… Thanks for sharing this
one. A must read article!

Feel free to surf to my webpage: mattress reviews 2015

Appreciating the persistence you put into your website and in depth
information you present. It’s awesome to come across
a blog every once in a while that isn’t the same out of
date rehashed information. Fantastic read!
I’ve bookmarked your site and I’m adding your RSS feeds to my Google account.

My blog post: hamsa bracelet amazon (http://severinos.com.br/)

I visit every day a few blogs and websites to read articles, however this
weblog provides quality based content.

my web blog; energy efficient electric heaters reviews

It’s a shame you don’t have a donate button! I’d definitely donate to this outstanding blog!

I suppose for now i’ll settle for bookmarking and adding your RSS feed
to my Google account. I look forward to brand new updates and will talk about this blog with my
Facebook group. Chat soon!

Feel free to visit my website: aca employer reporting instructions & information

This paragraph offers clear idea in support of the new
visitors of blogging, that really how to do blogging and site-building.

Also visit my webpage: best wood cleaners for furniture

I blog frequently and I truly thank you for your content.
The article has truly peaked my interest. I’m going to take
a note of your blog and keep checking for new details about once a week.
I subscribed to your RSS feed too.

Feel free to visit my site; sol republic headphones review

I think this is among the most significant info for me. And i’m glad reading
your article. But wanna remark on few general things, The site style
is great, the articles is really excellent : D.
Good job, cheers

Also visit my homepage … signing agent classes – crema.co.il,

Please take a look at the web sites we adhere to, including this one, because it represents our picks from the web.

Hi there, all is going well here and ofcourse every one is sharing information, that’s genuinely excellent, keep up writing.

Here is my weblog :: private blog network 2015 (yabanakimou-gr.lemonopoulos.com)

Always a massive fan of linking to bloggers that I adore but do not get a whole lot of link appreciate from.

This is a topic which is near to my heart… Best wishes!
Exactly where are your contact details though?

Look into my page … wedding dress taeyang lyrics

We are a gaggle of volunteers and starting a brand new scheme in our
community. Your site offered us with valuable information to work on. You’ve
performed a formidable task and our entire group will likely be
thankful to you.

Just beneath, are numerous completely not connected web pages to ours, nonetheless, they’re surely really worth going over.

The info mentioned inside the write-up are a number of the very best obtainable.

I read this post fully about the difference of most recent and previous technologies, it’s awesome article.

Thanks , I have recently been searching for information about this topic
for a long time and yours is the greatest I’ve came upon till
now. However, what in regards to the conclusion? Are you sure concerning the supply?

my page – hack clash of clans no download

But if you’re expecting to get the same quality that comes
for thousands by paying a price as low as $40, you may be trying to fool yourself.

Any woman would needs shoes that make her feel pretty.

The ‘Stars are Blind’ party professional wears size 11 shoes,
so she has all her shoes custom-designed to fit her large feet.

Also visit my site – louboutin outlet

??????????????????????? ??? ??????????????? http://www.linyuroom.com/ ??????????????????????????????????????????????????????????????????????????????????????????? ??? ???????????????????????????????????????????????http://www.linyuroom.com/???????????????????????????????????????????????????????????????????????????????????????????????????

Hi there Dear, are you genuinely visiting this website daily,
if so afterward you will absolutely take good knowledge.

Feel free to surf to my webpage: brewer sewing supplies

Hmm is anyone else having problems with the images on this blog loading?
I’m trying to find out if its a problem on my end or
if it’s the blog. Any responses would be greatly appreciated.

We came across a cool internet site that you could appreciate. Take a search for those who want.

Very few web sites that transpire to be detailed below, from our point of view are undoubtedly properly worth checking out.

Check below, are some entirely unrelated websites to ours, on the other hand, they are most trustworthy sources that we use.

I just want to mention I am just new to blogs and seriously enjoyed this blog. Likely I’m planning to bookmark your site . You really come with remarkable stories. Thanks a lot for sharing your web-site.

Asking questions are actually nice thing if you are not understanding anything totally, except
this paragraph gives good understanding even.

my web page; roofing specialist

I love it whenever people get together and share opinions.

Great website, keep it up!

Also visit my homepage roof repair tucson

Below you?ll come across the link to some websites that we think you must visit.

Great article, exactly what I was looking for.

Also visit my blog post best gun safes

Just beneath, are numerous absolutely not associated sites to ours, on the other hand, they are certainly really worth going over.

Below you will locate the link to some web sites that we feel it is best to visit.

Below you will obtain the link to some sites that we believe it is best to visit.

Although internet sites we backlink to beneath are considerably not connected to ours, we really feel they are truly worth a go by means of, so have a look.

Leave a Reply