My app have a list of Routes, and each route has a list of Steps. I'm using Solr/Sunpost to index the step lat/lng coordinate do do some searches. Piece of code:
class Step < ActiveRecord::Base
belongs_to :route
searchable do
integer :route_id, :references => Route
latlon(:coordinates) { Sunspot::Util::Coordinates.new(lat, lng) }
end
end
I can make searches using the order_by_geodist() from Sunspot, it works ok:
s = Step.search do
order_by_geodist(:coordinates, lat, lng)
end
The thing is: is there a way to group the results by route_id? In other words, I want to return only one step for each route.
Resolved with the "group" option
s = Step.search do
group :route_id
order_by_geodist(:coordinates, lat, lng)
end
s.group(:route_id).groups.each do |g|
logger.info g.value
logger.info g.results[0].inspect
end