Home > Software design >  PGSearch Gem -- is it possible to group/weight by the :against elements?
PGSearch Gem -- is it possible to group/weight by the :against elements?

Time:03-30

I'm using PG Search to allow a user to enter a query and search across a couple different attributes on models. I have the following model;

class Movie < ApplicationRecord
  include PgSearch::Model

  pg_search_scope :search_for,
                  against: { title: 'A', short_description: 'B', long_description: 'C' },
                  using: { tsearch: { prefix: true } }

end

Which means you can do something like;

Movie.search_for("person") and it would find results that have the word person in the title, short_description and long description. HOWEVER, it doesn't seem to group in any logical manner.. the results feel very random.

Ultimately I'd like it to group by the against hash.. So Title first, then Short description, then long description. Meaning all those titles that have "person" in the title are at the top of the list, then short_description, then long description, and with those that have the query in more than one to be higher in ranking.. so if the query is in the title and the short description it would be above one that just has it in the title.

Or if it's in the short description 10 times but not int he title at all.. the ones with it in the title should still be higher up.. Sort of a weighting system?

How does one go about doing this?

Thanks!

CodePudding user response:

Your code example is weighting by the against elements, title first, short_description second, etc.

You should consider implementing a ranked_by to further tweak your results to your desired output.

The pg_search_rank method will be especially helpful for debugging.

  • Related