Sunspot Solr Boost on text field

145 Views Asked by At

I want to boost documents in the result based on any text field.

eg. consider following schema

class Post < ActiveRecord::Base
  searchable do
    text :title, :body
    text :comments do
      comments.map { |comment| comment.body }
    end
    text :tags
    boolean :featured
  end
end

to boost on the Boolean field we can simple add a boost clause like this:

Post.search do
  fulltext '*:*' do
    boost(2.0){with(:featured, true)}
  end
end

I want to achieve boosting on text field, let's say I want to get all the post, but post tagged 'important' must come first, something like this:

Post.search do
  fulltext '*:*' do
    boost(2.0){fulltext 'important', fields: :tags}
  end
end

I know above code is not correct, but explains the required behaviour.

1

There are 1 best solutions below

0
On

Try this

   Post.search do
      fulltext '*:*' do
         boost(2) { with(:tags).equal_to('important') }
      end
    end