开发者

Rails Thinking sphinx add rating value to relevance

I am using Thinking Sphinx for a Rails application. Everything works perfectly. I just want my search results to take the ratings of my items into account (items with higher relevance appear first).

define_index('item') do 
  # ... 
  indexes 'items.ratings_sum/items.ratings_count', 
    :as         => :rating, 
    :sortable   => true
  # ... 
end

I used the "sort_mode" option, but it puts too much emphasis on the rating, not enough on the keywords.

How can I use开发者_如何转开发 this rating to influence to order of the search results?


Firstly, you're going to want that rating value as an attribute - it's not like people are going to search for it, right? And it's better if it's stored as a float? So:

has 'items.ratings_sum/items.ratings_count',
  :as   => :rating,
  :type => :float

The next step - and this is something you'll have to tweak to get it exactly how you like - is to use the expression sort mode:

Item.search 'foo',
  :sort_mode => :expr,
  :order     => "@relevance * rating"

Maybe you only want the rating to have a 10% impact on the @relevance? @relevance * (rating / 10). Or you just want to add the two? @relevance + rating. Play around with it until you find a solution you like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜