Casecommons / pg_search

pg_search builds ActiveRecord named scopes that take advantage of PostgreSQL’s full text search
http://www.casebook.net
MIT License
1.31k stars 369 forks source link

Can't set weight with `tsvector_column` #439

Open bkeepers opened 4 years ago

bkeepers commented 4 years ago

It looks like weighting only works for columns in the against option. When specifying the tsvector_column, the results seem to ignore the weights.

Is this expected? Is there a technical reason that weights can't be used with tsvector columns?

Here's the example configuration that I'm using:

class Sailboat < ApplicationRecord
  include PgSearch::Model

  pg_search_scope(:advanced_search,
    against: {
      title: "A",
      notes: "B",
    },
    using: {
      tsearch: {
        tsvector_column: [
          "tsvector_title",
          "tsvector_notes"
        ]
      },
      trigram: {
        only: [:title]
      }
  )
end

And here's the setup for my test:

sk45 = Sailboat.create! title: "Jeanneau Sun Kissed 45"
sk47 = Sailboat.create! title: "Jeanneau Sun Kissed 47", notes: "An extended version of the Sun Kissed 45"

And here's the failing result:

  1) Search ranks title over notes
     Failure/Error: expect(Sailboat.advanced_search("Jeanneau Sun Kiss 45")).to eq([sk45, sk47])

       expected: ["Jeanneau Sun Kiss 45", "Jeanneau Sun Kiss 47"]
            got: ["Jeanneau Sun Kiss 47", "Jeanneau Sun Kiss 45"]

cc @andrewbredow

nertzy commented 4 years ago

I think the most correct way to do this would be to set the weight in the expression that creates the tsvector. You can also make a single tsvector that combines the two columns, each with their own weight.

There is also a chance that we could do the weighting you want by calling the setweight(tsvector, "char") function after the fact.