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
> 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)"
]
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