Skip to content

Commit

Permalink
Adds support for tsvector_column in associated_against (pg_search_scope)
Browse files Browse the repository at this point in the history
Adds alternative option syntax for tsvector_column.
Adds migration for tsvector aggregation.
Adds support for tsvector columns in associated models.
Missing: More tests and documentation.
  • Loading branch information
Joao-Anselmo authored and mhenrixon committed Jan 25, 2023
1 parent 9d718b5 commit e3fae89
Show file tree
Hide file tree
Showing 12 changed files with 596 additions and 101 deletions.
11 changes: 10 additions & 1 deletion lib/pg_search/configuration/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,16 @@ def selects

def selects_for_singular_association
columns.map do |column|
"#{column.full_name}::text AS #{column.alias}"
if column.tsvector_column
"tsvector_agg(#{column.full_name}) AS #{column.alias}"
else
case postgresql_version
when 0..90000
"array_to_string(array_agg(#{column.full_name}::text), ' ') AS #{column.alias}"
else
"string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
end
end
end.join(", ")
end

Expand Down
15 changes: 12 additions & 3 deletions lib/pg_search/configuration/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
module PgSearch
class Configuration
class Column
attr_reader :weight, :name
attr_reader :weight, :tsvector_column, :name

def initialize(column_name, weight, model)
@name = column_name.to_s
@column_name = column_name.to_s
@weight = weight
if weight.is_a?(Hash)
@weight = weight[:weight]
@tsvector_column = weight[:tsvector_column]
else
@weight = options
end
@model = model
@connection = model.connection
end
Expand All @@ -20,7 +25,11 @@ def full_name
end

def to_sql
"coalesce(#{expression}::text, '')"
if tsvector_column
"coalesce(#{expression}, '')"
else
"coalesce(#{expression}::text, '')"
end
end

private
Expand Down
13 changes: 9 additions & 4 deletions lib/pg_search/features/tsearch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,15 @@ def columns_to_use
end

def column_to_tsvector(search_column)
tsvector = Arel::Nodes::NamedFunction.new(
"to_tsvector",
[dictionary, Arel.sql(normalize(search_column.to_sql))]
).to_sql
tsvector =
if search_column.tsvector_column
search_column.to_sql
else
Arel::Nodes::NamedFunction.new(
"to_tsvector",
[dictionary, Arel.sql(normalize(search_column.to_sql))]
).to_sql
end

if search_column.weight.nil?
tsvector
Expand Down
13 changes: 13 additions & 0 deletions lib/pg_search/migration/associated_against_tsvector_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'pg_search/migration/generator'

module PgSearch
module Migration
class AssociatedAgainstTsvectorGenerator < Generator
def migration_name
'add_pg_search_associated_against_tsvector_support_functions'
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddPgSearchAssociatedAgainstTsvectorSupportFunctions < ActiveRecord::Migration
def self.up
say_with_time("Adding tsvector support functions for pg_search :associated_against") do
execute <<-'SQL'
<%= read_sql_file "tsvector_agg" %>
SQL
end
end

def self.down
say_with_time("Dropping tsvector support functions for pg_search :associated_against") do
execute <<-'SQL'
<%= read_sql_file "uninstall_tsvector_agg" %>
SQL
end
end
end
2 changes: 2 additions & 0 deletions lib/pg_search/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class Railtie < Rails::Railtie
generators do
require "pg_search/migration/multisearch_generator"
require "pg_search/migration/dmetaphone_generator"
require "pg_search/migration/associated_against_generator"
require "pg_search/migration/associated_against_tsvector_generator"
end
end
end
Loading

0 comments on commit e3fae89

Please sign in to comment.