Skip to content

Adds support for tsvector_column in associated_against (pg_search_scope) - #504

Closed
mhenrixon wants to merge 1 commit into
Casecommons:masterfrom
hldesign:2.3.6/single-association-performance
Closed

mhenrixon wants to merge 1 commit into
Casecommons:masterfrom
hldesign:2.3.6/single-association-performance

Conversation

@mhenrixon

@mhenrixon mhenrixon commented Jan 25, 2023

Copy link
Copy Markdown

Adds alternative option syntax for tsvector_column. Adds migration for tsvector aggregation.
Adds support for tsvector columns in associated models.

Closes #499 in favor of this

pg_search_scope :full_text_search, (lambda do |query, locale = "swedish"|
    {
      query: query,
      associated_against: {
        taggings: { name: "B" },
        editables: {
          content_tsvector: { tsvector_column: true },
        },
      },
      order_within_rank: "articles.id DESC",
      using: {
        tsearch: {
          dictionary: SearchDocument.dictionary(locale),
          prefix: true,
        },
      },
    }
  end)

@mhenrixon
mhenrixon force-pushed the 2.3.6/single-association-performance branch 3 times, most recently from c606a56 to 8d6bcfa Compare January 25, 2023 20:18
@mhenrixon
mhenrixon force-pushed the 2.3.6/single-association-performance branch 9 times, most recently from 4f528b8 to 1c633b5 Compare August 16, 2023 16:47
@Javierchik

Copy link
Copy Markdown

This is a nice addition @mhenrixon thanks.
Do you know if there are any plans to formally accept this PR?
cc @nertzy

@mhenrixon

Copy link
Copy Markdown
Author

@Javierchik i haven't heard anything back at all.

Someone posted on x.com searching for alternatives since pg_search seems dead or in maintenance mode.

The guys on @37signals are working on a rails default for search so that's probably not too far away.

Until then I am using this branch 🤷‍♂️

Lets a pg_search_scope search associated models through a precomputed
tsvector column instead of building to_tsvector(...) at query time:

    pg_search_scope :search, associated_against: {
      comments: { body_tsvector: { tsvector_column: true } }
    }

- Column accepts `{ tsvector_column: true, weight: ... }` in place of a bare
  weight and emits `coalesce(col::tsvector, '')` for such columns.
- Association selects cast to tsvector (singular) or aggregate with a new
  `tsvector_agg` (plural) instead of string_agg(...::text).
- Tsearch skips the to_tsvector() wrap for tsvector columns.
- `pg_search:migration:associated_against_tsvector` generator installs the
  `tsvector_agg` aggregate (sql/tsvector_agg.sql).

Rebased onto master (was PR Casecommons#504's 2.3.6-based branch), standardrb clean,
suite green on Ruby 4.0 / Active Record 8.1.
@mhenrixon
mhenrixon force-pushed the 2.3.6/single-association-performance branch from bd63c85 to 5707315 Compare September 1, 2026 05:45
@mhenrixon

Copy link
Copy Markdown
Author

Superseded by #580 — same change rebased onto current master from a correctly named branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants