I have a model that has a location and description/title, this info is indexed in Solr. When I search for the data I'm trying to have the relevancy to be by location first and then by keywords, however, I cannot seem to accomplish it - if both keywords and location are provided the results are sorted by the keywords first.
searchable do
location :coordinates do
Sunspot::Util::Coordinates.new(self.location.latitude, self.location.longitude) if self.location
end
text :title
text :description
...
end
The searching is defined as such
search.build do
with(:coordinates).near(latitude, longitude, :precision=>radius)
keywords "#{search_words}", :fields => [:title, :description], :minimum_match => 1
end
If it helps, when keywords are not provided, results are already sorted by location
Try forcing the distance to take precedence to the keywords.
:score
is an internal field that contains the Solr dismax score of each search result. Your default ordering will be by :score, :desc
when you don't specify an order_by
.
Putting in order_by_geodist(:coordinates, latitude, longitude)
forces Solr to disregard the dismax score, so you get a distance ordering instead. Adding order_by(:score, :desc)
afterward will take dismax score back into account, but only when distances are equal (or missing).
search.build do
with(:coordinates).near(latitude, longitude, :precision=>radius)
keywords "#{search_words}", :fields => [:title, :description], :minimum_match => 1
order_by_geodist(:coordinates, latitude, longitude)
order_by(:score, :desc)
end