I try to use document boost on index time, but it seems, that it hasn't any effect. I've set up my model for Sunspot like
Spree::Product.class_eval do
searchable :auto_index => true, :auto_remove => true do
text :name, :boost => 2.0, stored: true
text :description, :boost => 1.2, stored: false
boost { boost_value }
end
end
The boost_value
field is a field in the database, where a user can change the boost in the frontend. It gets stored at index time (either the first time I build the index, or when a product is updated). I have about 3600 products in my database, with a default boost_value
of 1.0
. Two of the products got different boost_values
, one with 5.0
and the other with 2.0
.
However, If I just want to retrieve all products from Solr, the document boost seems to have no effect on the order or the score:
solr = ::Sunspot.new_search(Spree::Product) do |query|
query.order_by("score", "desc")
query.paginate(page: 1, per_page: Spree::Product.count)
end
solr.execute
solr.results.first
The Solr query itself looks like this:
http://localhost:8982/solr/collection1/select?sort=score+desc&start=0&q=*:*&wt=xml&fq=type:Spree\:\:Product&rows=3600&debugQuery=true
I've appended a debugQuery=true
at the end, to see what the scores are. But there are no scores shown.
The same things happens, when I search for a term. For examle, I have 2 products that have a unique string testtest
inside the name
field. When I search for this term, the document boost has no effect on the order.
So my questions are:
q=*:*
?In solr, the boosts only apply to text searches, so it applies only if you do a fulltext search. Something like this:
solr = ::Sunspot.new_search(Spree::Product) do |query|
fulltext 'somesearch'
query.order_by("score", "desc") # I think this isn't necesary
query.paginate(page: 1, per_page: Spree::Product.count)
end
If you want to boost certain products more than others:
solr = ::Sunspot.new_search(Spree::Product) do |query|
fulltext 'somesearch' do
boost(2.0) { with(:featured, true) }
end
query.paginate(page: 1, per_page: Spree::Product.count)
end
As you see, this is much powerfull than boosting at index time, and you could put different boostings for different conditions, all at query time with no need of reindexing if you want to change the boost or the conditions.