1. Skip to navigation
  2. Skip to content

The ELC Community Blog

A knowledge exchange on Ruby on Rails and Agile Development


Advanced db_free_solr

by jmoline on March 31, 2008
If you've implemented the db_free_solr plugin in your application, it's probably safe to assume that performance is pretty important to you. For those of you that are looking to squeeze just a little bit more efficiency out of your Solr implementation, here's another feature you might want to know about.

The out-of-box functionality that db_free_solr provides, much the same as acts_as_solr, is designed to make things as easy as possible for you. To that end, the default behavior is to index and store every field you call out in your models. The truth, though, is that you won't need every field you index to be returned with the results. Likewise, some of the fields you want to have returned will, no doubt, not be necessary to index for searching purposes.

So, how do we go about avoiding unnecessary indexing and storing?

The answer is actually simpler than you might think. Acts_as_solr allows you to specify the datatypes of the fields you tell it to index (if not specified, the index assumes a datatype of text). Like so:
acts_as_solr :fields => [{:field1 => :string}, {:field2 => :integer}, {:field3 => :date}]
So, what I've done, is to add a few datatypes to correspond with the newly available indexing options. As I said above, the default is to both index and store, so what I've added are datatypes to indicate that a field should be marked for storage only ("_so") or indexing only ("_io"). These can be used like so:
acts_as_solr :fields => [{:field1 => :string_io}, {:field2 => :integer_so}, {:field3 => :date}]
It's also worth noting that you can specify datatypes for each and every field, or only a select few. In other words, this will work just fine:
acts_as_solr :fields => [:field1, {:field2 => :integer_so}, :field3]
Here's a complete listing of the supported datatypes:

Symbol Definition
:text text, indexed and stored
:text_so text, stored only
:text_io text, indexed only
:string string, indexed and stored
:string_so string, stored only
:string_io string, indexed only
:date date, indexed and stored
:date_so date, stored only
:date_io date, indexed only
:integer integer, indexed and stored
:integer_so integer, stored only
:integer_io integer, indexed only
:float float, indexed and stored
:float_so float, stored only
:float_io float, indexed only
:boolean boolean, indexed and stored
:boolean_so boolean, stored only
:boolean_io boolean, indexed only

That's all for now. Happy Programming!

Comments

Add a comment

You can use textile. For code, wrap in a <code lang="..."> tag.
home | services | Ruby on Rails Development | code | blog | company