log0
log0

Reputation: 551

How do I boost date/datetime fields in Sunspot Rails?

I'd like to boost by recency but not order by datetime/date field absolutely (since there are other factors at play).

How do I boost a date/datetime field in Sunspot Rails?

Upvotes: 2

Views: 763

Answers (1)

Andrew
Andrew

Reputation: 41

Sunspot does not have built-in capability to boost a date/datetime field, however, it is possible to do so using solr itself, as evidenced by this article:

https://wiki.apache.org/solr/FunctionQuery#Date_Boosting

Fortunately, sunspot provides a way of manually adding params to the solr query. What you'll want to do here is make sure that the date field you are using to boost is contained in the searchable block on the model.rb:

searchable do    
   time :datetime_field, stored: true, trie: true
end

Then, in the search block that is probably in your models_controller.rb, add the date boost function:

@search = Model.search do
  # perform search
  adjust_solr_params do |sunspot_params|
    sunspot_params[:boost] = 'recip(ms(NOW,datetime_field_dts),3.16e-11,1,1)'
  end
end

Note that sunspot adds '_dts' to the end of your time field, so you will need to include this at the end of the variable name in the query string.

Upvotes: 1

Related Questions