DevZona

Rails, Heroku, AWS and other awesome technologies

Upgrade From Sunspot-1.3 Sunspot-2.0

| Comments

Overview

A couple month ago, after seven month of pre-release, sunspot-2.0 was released. It added several nice Solr 3 features including spatial search and field collapsing. Upgrading to sunspot-2.0 is pretty easy. However, you may run into a few obstacles, especcially if you are planning to use new spatial search functionality.

Updating

Updating to latest version of sunspot is easy, just run: bundle update sunspot_rails sunspot_solr. It almost entirely backwards compatable, unless you are planning to use Solr 3 features, such as: Spatial Search or Field Grouping.

New Field Type

In my case, I wanted use Solr 3 radius search search, so I re-defined location field of my model to be a new type of latlon:

app/models/job.rb
1
2
3
4
5
6
7
8
9
10
11
12
class Job < ActiveRecord::Base

  searchable do
    ...
    latlon(:location) { coordinates }
    ...
  end

  def coordinates
    Sunspot::Util::Coordinates.new(self.latitude, self.longitude)
  end
end

Additionally, I changed my search routines to use new sunspot-2.0 method in_radius for geolocation filtering:

app/models/job.rb
1
2
3
4
5
6
# Jobs within 100 kilometers from (lat, lng)
@search = Job.solr_search(include: :employer) do
  ...
  with(:location).in_radius(lat, lng, 100)
  ...
end

As soon as I did it, I tried re-indexing and got the following error:

Status 400 - ERROR: [doc=JobPosting 1] unknown field 'location_ll'

I figured that I needed to delete my /solr folder, and restart solr instance to re-generate config files. As soon as I did, it fixed the problem locally – I was able to re-index, and get all my tests to pass.

WebSolr Configuration

After I deployed my changes to Heroku, I got the same error:

1
2
3
rake aborted!
RSolr::Error::Http - 400 Bad Request
Error: <html><head><title>Apache Tomcat/6.0.28 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 400 - ERROR: [doc=JobPosting 1] unknown field 'location_ll'</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>ERROR: [doc=JobPosting 1] unknown field 'location_ll'</u></p><p><b>description</b> <u>The request sent by the client was syntactically incorrect (ERROR: [doc=JobPosting 1] unknown field 'location_ll').</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.28</h3></body></html>...

Apparently, all I had to do is to re-configure my index on WebSolr. In order to do that, I logged in to WebSolr Console, clicked on the index, and selected “Sunspot 2.0 for Ruby on Rails (Solr 3)” option from “Index Type” drop down, clicked “Change”, and waited for it to re-configure. After that, I re-indexed the data set, and was able to start using new sunspot-2.0 features.

Comments