Question

I have the Rails project with User model.
It has two fields: :name, :surname Also I have sunset search:

searchable do
  text :name
  text :surname
end

When I do such search: User.solr_search { fulltext 'Name Surname' } .results how to get results all results in such order:

  1. where name and surname are 'Name Surname'
  2. where only surname is 'Surname'
  3. where only name is 'Name'

Also I found, but I don't understand is that what I need https://github.com/sunspot/sunspot/wiki/Matching-substrings-in-fulltext-search

Was it helpful?

Solution

You can use boosters, so that surname results come before name results and surname&name results before surname results.

searchable do
    text :surname, boost: 2.0
    text :name,    boost: 1.0
end

You ask any term to be matched and then order them descendingly on score. Results matching name and surname will score higher, only surname lower and only name lowest.

@search=User.search
    fulltext "#{name_variable} #{surname_variable}", :minimum_match => 1
    order_by(:score, :desc)
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top