How to use the coordinate in the index?

7 views
Skip to first unread message

Tom Cocca

unread,
Nov 5, 2009, 10:44:48 AM11/5/09
to Sunspot
I am using Sunspot 0.10.5 and sunspot_rails 0.10.9 and I am having
some issues with the coordinates piece in the index:

I have the following:

class Property < ActiveRecord::Base
belongs_to :building

searchable do
....
coordinates :lat_lng
...
end

# define this hear because coordinates can't take a block, only a
method name
def lat_lng
building.lat_lng
end
end

Then:

class Building < ActiveRecord::Base
acts_as_geocodable

def lat
geocode.latitude.to_f
end

def long
geocode.longitude.to_f
end

def lat_lng
[lat, long]
end
end

I started the solr that shipped with sunspot using rake
sunspot:solr:start

Now when I run the command:

Property.reindex(:include => [:building])

I get the following:

RSolr::RequestError: #<RSolr::RequestError:0xb5bc60b4>
from /usr/lib/ruby/gems/1.8/gems/mwmitchell-rsolr-0.9.6/lib/rsolr/
http_client.rb:64:in `post'
from /usr/lib/ruby/gems/1.8/gems/mwmitchell-rsolr-0.9.6/lib/rsolr/
connection/adapter/http.rb:32:in `request'
from /usr/lib/ruby/gems/1.8/gems/mwmitchell-rsolr-0.9.6/lib/rsolr/
connection.rb:40:in `request'
from /usr/lib/ruby/gems/1.8/gems/mwmitchell-rsolr-0.9.6/lib/rsolr/
connection.rb:28:in `update'
from /usr/lib/ruby/gems/1.8/gems/mwmitchell-rsolr-0.9.6/lib/rsolr/
connection.rb:52:in `add'
from /home/tom/rails/logic_rentals/vendor/gems/sunspot-0.10.5/lib/
sunspot/indexer.rb:85:in `add_documents'
from /home/tom/rails/logic_rentals/vendor/gems/sunspot-0.10.5/lib/
sunspot/indexer.rb:26:in `add'
from /home/tom/rails/logic_rentals/vendor/gems/sunspot-0.10.5/lib/
sunspot/session.rb:72:in `index'
from /home/tom/rails/logic_rentals/vendor/gems/sunspot-0.10.5/lib/
sunspot.rb:170:in `index'
from /home/tom/rails/logic_rentals/vendor/gems/sunspot_rails-0.10.9/
lib/sunspot/rails/searchable.rb:190:in `reindex'
from /home/tom/rails/logic_rentals/vendor/gems/sunspot_rails-0.10.9/
lib/sunspot/rails/searchable.rb:253:in `benchmark'
from /home/tom/rails/logic_rentals/vendor/gems/sunspot_rails-0.10.9/
lib/sunspot/rails/searchable.rb:189:in `reindex'
from (irb):4

If I take the coordinates out of the index definition the whole thing
runs fine. Anybody had any success with coordinates?

Thanks,
~ Tom

Mat Brown

unread,
Nov 5, 2009, 11:09:06 AM11/5/09
to ruby-s...@googlegroups.com
Hi Tom,

First, sorry about not being able to define the coordinates field with
a block - I actually just patched that this morning, for a release
that should go out today or tomorrow. Anyway, an oversight on my part.

Anyway, it looks like you're doing everything right in your code - are
you running the Solr instance that is packaged with Sunspot? Also, do
you have a solr home in your Rails directory? If so, you'll want to
copy the following files from the gem directory into your solr home:

$ mkdir -v $RAILS_ROOT/solr/lib
$ cp -v $PATH_TO_YOUR_GEMS/sunspot-0.10.5/solr/solr/lib/*
$RAILS_ROOT/solr/lib
$ cp -v $PATH_TO_YOUR_GEMS/sunspot-0.10.5/solr/solr/conf/{schema,solrconfig}.xml
$RAILS_ROOT/solr/conf

Then be sure to restart your Solr instance. Let me know if that helps!

Mat

Tom Cocca

unread,
Nov 5, 2009, 11:31:19 AM11/5/09
to ruby-s...@googlegroups.com
Matt, thanks for the help.

No prob about the block on coords, easy to add a simple method, when the new version comes out i'll change it.

Another thing I figured out though, this line in your directions:
cp -v $PATH_TO_YOUR_GEMS/sunspot-0.
10.5/solr/solr/conf/{schema,solrconfig}.xml
$RAILS_ROOT/solr/conf

You also need to copy elevate.xml along with the other two or it blows up.
 


On a side note, I was reading your sunspot blog (which is awesome that you use gh pages to blog) here http://outoftime.github.com/2009/10/15/sunspot-0-10.html

and noticed that your have a text_fields piece to the query.  Is there any way to use that in the scope?  I am wondering if this might solve my issue of not being able ot use multiple keywords (i posted yesterday).

If I could use this in scope with the conjunctions anddisjunctions it might work? Any thoughs?

searchable do
  text(:comments)
  text(:public_descriptions) { property_notes.map{|pn| pn.public_description } }
  integer(:property_notes_company_ids, :multiple => true) { property_notes.map{|pn| pn.company_id} }
end

search = Property.search do |query|
  query.any_of do |disjunction|
    disjunction.text_fields do |tf|
      tf.with(:comments, keywords)
    end
    disjunction.all_of do |conjunction|
      conjunction.with(:property_notes_company_ids, [company_id])
      conjunction.text_fields do |tf|
        tf.with(:public_descriptions, keywords)
      end
    end
  end
end

Unless this seems really dumb... not sure.

Thanks,
~ Tom

Mat Brown

unread,
Nov 5, 2009, 11:46:54 AM11/5/09
to ruby-s...@googlegroups.com
Hi Tom,

GitHub Pages (and Jekyll generally) is great - nothing better than
being able to blog in vim!

A couple of problems with the approach you laid out:

1) When you use the :text_fields block, you're performing raw boolean
logic on text fields, rather than a dismax fulltext search. So, only
documents for which the exact search phrase appears will match that
(the equivalent of the phrase being in quotes in a normal dismax
search). Generally using the text_fields block is going to give
unexpected results to those without a pretty deep understanding of the
way Lucene tokenization and boolean logic work.
2) Even assuming the text_fields bit works, you're finding all
documents who meet the restrictions (has a property note for the
company AND has a property note matching the keywords). However, what
you want is (has a property note for the company that matches the
keyword). The latter simply isn't possible if you're indexing Property
objects. When thinking about indexed documents, it's important to
conceptualize them as flat documents, even though the data in them may
come from relational structures. Incidentally, the CompanyProperty
idea I mentioned yesterday probably does map closely to the resource
pattern in your application - presumably when you display a property
to a given company, you're presenting basically the same thing (the
information about a property as it relates to a particular company).

Hope that helps!
Mat

Tom Cocca

unread,
Nov 5, 2009, 12:26:54 PM11/5/09
to ruby-s...@googlegroups.com
Thanks matt,

So how does the boolean logic on text field compare to the dismax?

Say I have a comments that was:   "This unit has a renovated kitchen"

And I want to do a text_field lookup on comments for  "renovated kitchen"

text_fields do
  with :comments, 'renovated kitchen'
end

this would not match?  Or it would match because the phrase is there?

Where if I did a fulltext lookup it would match?

fulltext 'renovated kitchen' do
  fields :comments
end

But this would also match a comments like "This kitchen has recently been renovated" correct?

Thanks again for all your help.
~ Tom

Mat Brown

unread,
Nov 5, 2009, 12:33:04 PM11/5/09
to ruby-s...@googlegroups.com
Hi Tom,

I'm actually not entirely sure about the first case - I think it would
match, but I'm not positive if the query parser considers "\"renovated
kitchen\"" the same as "renovated\\ kitchen" - the latter being what
Sunspot passes in for a query on a string. Best way to find out is
probably to play around with it : )

Mat
Reply all
Reply to author
Forward
0 new messages