0
votes

I need to search locations with sort by distance from passed coordinates.

app/indices/location_index.rb

ThinkingSphinx::Index.define :location, :with => :active_record do
  indexes :name
  has latitude, longitude
end

Try to search:

> Location.search(:geo => [53.348962, 83.777988], :order => "@geodist DESC").size
ThinkingSphinx::SyntaxError: sphinxql: syntax error, unexpected USERVAR, expecting IDENT (or 5 other tokens) near '@geodist DESC LIMIT 0, 20; SHOW META'

> Location.search(:geo => [53.348962, 83.777988],:with => {"@geodist" => 0.0..5000.0}, :order => "@geodist DESC").size
ThinkingSphinx::SphinxError: sphinxql: only >=, <=, and BETWEEN floating-point filter types are supported in this version near '@geodist BETWEEN 0.0 AND 5000.0 AND sphinx_deleted = 0 ORDER BY @geodist DESC LIMIT 0, 20; SHOW META'
  • Sphinx 2.0.6-release (r3473; Oct 22, 2012)
  • thinking-sphinx (3.0.1)

Update:

Pat Allan suggested: Geodist no longer requires the @ symbol - so try the following instead:

Location.search(:geo => [53.348962, 83.777988], :order => "geodist DESC").size
Location.search(:geo => [53.348962, 83.777988],:with => {:geodist => 0.0..5000.0}, :order => "geodist DESC").size
1
I dont know enought about thinking-sphinx to really help. but perhaps try adding `compat_sphinxql_magics = 1' (or changing the existing line from 0) to your sphinx.conf file. @geodist is not supported in later interations of sphinxql, so have to reenable the legacy mode.barryhunter
@barryhunter, it hasn't helped. But thank you!Ivan Schneider

1 Answers

1
votes

Just in case people find this question thought I'd add the answer in the correct format and with a little more explanation.

Versions of Sphinx prior to 2.1.1 made the calculated distance available via the @geodist internal variable. In versions of Thinking Sphinx compatible with newer versions of Sphinx the value of GEODIST() has been aliased to geodist.

So this:

Location.search(:geo => [53.348962, 83.777988], :order => "@geodist DESC").size

Becomes:

Location.search(:geo => [53.348962, 83.777988], :order => "geodist DESC").size

It's also worth pointing out in this example that the coordinates being supplied in the above example are in the incorrect format. They're in degrees rather than radians: http://pat.github.io/thinking-sphinx/geosearching.html. To transform them you can do:

def degrees_to_radians(degrees)
  degrees * Math::PI / 180
end

Hope that helps!