Using Thinking Sphinx (3.0.1) purely for Geodistance

15 views
Skip to first unread message

Lonnie Warpup

unread,
Mar 6, 2013, 12:14:46 PM3/6/13
to thinkin...@googlegroups.com
So I'd like to use Thinking Sphinx purely as a tool to allow the user to order some associated companies based on location.  Here is the important information in my index:

ThinkingSphinx::Index.define :company, :with => :active_record do
  # Attributes (cannot be text)
  has id, :as => :company_id
  has longitude, :as => :lng, :type => :float
  has lattitude, :as => :lat, :type => :float # Forgive the spelling error
end

rake ts:rebuild done, and here is my console log:

> c = Company.find(53766); "Base: #{c.city}, #{c.state} [#{c.lattitude}, #{c.longitude}]"
"Base: Oak Park, Michigan [42.46714, -83.15695]"
 
> Company.search(:indices => ['company_core'], :geo => [c.lattitude, c.longitude], :order => "geodist ASC").map{|c| "#{c.city}, #{c.state} [#{c.lattitude}, #{c.longitude}] (#{c.id})" }
[
    [ 0] "Oak Park, Michigan [42.46714, -83.15695] (276075)",
    [ 1] "Ferndale, Michigan [42.46672, -83.1259] (3419)",
    [ 2] "Ferndale, Michigan [42.46825, -83.12812] (3214)",
    [ 3] "Tulsa, Oklahoma [36.18352, -95.7884] (82944)",
    [ 4] "Ferndale, Michigan [42.46381, -83.12867] (47512)",
    [ 5] "Houston, Texas [29.89859, -95.66493] (143434)",
    [ 6] "Greensboro, North Carolina [36.07354, -79.94699] (8618)",
    [ 7] "Catoosa, Oklahoma [36.18007, -95.72873] (26684)",
    [ 8] "Ferndale, Michigan [42.46246, -83.12375] (101585)",
    [ 9] "coimbatore, Tamil Nadu [10.94272, 76.97917] (89942)",
    [10] "coimbatore, Tamil Nadu [10.94272, 76.97917] (217885)",
    [11] "Warren, Michigan [42.47056, -83.08191] (24528)",
    [12] "Houston, Texas [29.8968, -95.65148] (79232)",
    [13] "Hazel Park, Michigan [42.47218, -83.10462] (4132)",
    [14] "Casilda  -  Santa Fe,  [-33.0483, -61.14761] (65558)",
    [15] "Ferndale, Michigan [42.47307, -83.1332] (107970)",
    [16] "Warren, Michigan [42.46676, -83.04572] (4209)",
    [17] "Warren, Michigan [42.46676, -83.04572] (91989)",
    [18] "Houston, Texas [29.8996, -95.61071] (4534)",
    [19] "Warren, Michigan [42.46892, -83.04759] (9379)"
]

And here is what was logged as the query:

  SELECT *, GEODIST(42.46714, -83.15695, lat, lng) AS geodist FROM `company_core` WHERE MATCH('@sphinx_internal_class_name (Company)') AND sphinx_deleted = 0 ORDER BY geodist ASC LIMIT 0, 20 OPTION max_matches=5000

The first few results are not bad, though I would have thought the base company would have appeared first.  Then things get even stranger.  Is there something fundamental that I am doing incorrect here?

-Lonnie

Timo Virkkala

unread,
Mar 6, 2013, 12:55:58 PM3/6/13
to thinkin...@googlegroups.com
Hi Lonnie,

Sphinx expects coordinates to be in radians. Yours seem to be in degrees.
From the Sphinx reference: "Note that both latitudes and longitudes
must be in radians and the result will be in meters"

What you want to do is probably something like this:
has "RADIANS(latittude)", :as => :lat, :type => :float
has "RADIANS(longitude)", :as => :lng, :type => :float

-T-
> --
> You received this message because you are subscribed to the Google Groups
> "Thinking Sphinx" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to thinking-sphi...@googlegroups.com.
> To post to this group, send email to thinkin...@googlegroups.com.
> Visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Lonnie Warpup

unread,
Mar 6, 2013, 1:08:08 PM3/6/13
to thinkin...@googlegroups.com
Hi Timo.

I tried that and without examining what a valid range of radians would be, deduced from the results that I didn't need to do that.  Now I realize my error was in my query when the database was properly indexed.  I was passing in degrees in the :geo => [lat, lng]... and that gave me bad results.

Thanks for the wake-up call.

-Lonnie
Reply all
Reply to author
Forward
0 new messages