Casecommons/pg_search

How to get exact matches to rank at the top?

abevoelker opened this issue · 2 comments

I've got a very simple model I'm trying to search against a single column with:

class Person < ApplicationRecord
  include PgSearch::Model
  pg_search_scope :search_by_first_and_last_name,
    against: [:name_first_and_last],
    using: [:tsearch]
end

However my searches don't seem to rank an "exact match" higher, so I'm getting searches where the first and last name match being flipped ranks exactly the same as an exact match:

> xs = Person.search_by_first_and_last_name("Alexander Bernard").with_pg_search_rank
> xs.each{|x| puts "#{x.name_first_and_last} => #{x.pg_search_rank}"}; 0

BERNARD ALEXANDER => 0.09910322
Alexander Bernard => 0.09910322
Alexander Bernard => 0.09910322
Alexander Bernard => 0.09910322
Bernard Alexander => 0.09910322
BERNARD ALEXANDER => 0.09910322
ALEXANDER SANDBERG-BERNARD => 0.0973584

I must be missing something obvious with how to get this to work, right?

I actually have the first and last name fields broken out into separate columns as well, but attempts at using 'A' / 'B' weights searching against the two different columns didn't fix the problem.

Take a look at the :normalization option in the README.

Thanks for the suggestion but I tried every normalization bit (although not every permutation) and none of them helped. The pg_search_rank numbers change but "Bernard Alexander" and "Alexander Bernard" still rank identically. I assume because "Bernard Alexander" and "Alexander Bernard" have identical document lengths.

I guess I'll just make separate search scopes for first and last names.