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.3k stars 369 forks source link

Weights on associations #529

Open trevorfoxsoft opened 7 months ago

trevorfoxsoft commented 7 months ago

I am using search scopes and want to apply weights to associations. I have seen #132 and #40 but the syntax presented in them doesn’t seem to work.

  pg_search_scope :full_search,
                  against: {
                    title: "A",
                    introduction: "B",
                    hashtag: "C"
                  },
                  associated_against: {
                    content_rows: [
                      { text_1: "D" },
                      { text_2: "E" },
                      { image_caption_1: "F" },
                      { image_caption_2: "G" }
                    ]
                  }

Gives the error PG::UndefinedColumn: ERROR: column content_rows.{:text_1=>"D"} does not exist LINE 1: ...IN (SELECT "content_pages"."id" AS id, string_agg("content_r... ^

And

  pg_search_scope :full_search,
                  against: {
                    title: "A",
                    introduction: "B",
                    hashtag: "C"
                  },
                  associated_against: {
                    content_rows: {
                      text_1: "D",
                      text_2: "E",
                      image_caption_1: "F",
                      image_caption_2: "G"
                    }
                  }

gives PG::InternalError: ERROR: unrecognized weight: 69.

Is there a way to do this?

trevorfoxsoft commented 7 months ago

Also just tried this syntax, which gives me the same unrecognized weight error

  pg_search_scope :full_search,
                  against: [
                    [ "title", "A" ],
                    [ "introduction", "B" ],
                    [ "hashtag", "E" ]
                  ],
                  associated_against: {
                    content_rows: [
                      [ "text_1", "C" ],
                      [ "text_2", "D" ],
                      [ "image_caption_1", "F" ],
                      [ "image_caption_2", "G" ]
                    ]
                  },
ngcheung commented 5 months ago

the second syntax is correct, but the error is because 'E' isn't a supported weight by postgres.setweight only goes up to 'D'. https://www.postgresql.org/docs/current/textsearch-controls.html

tsvallender commented 4 months ago

Aah excellent, thanks. I'll forward this on to someone still working on the project it was relevant to as I never did get it fixed.