Skip to content

Commit e3fae89

Browse files
Joao-Anselmomhenrixon
authored andcommitted
Adds support for tsvector_column in associated_against (pg_search_scope)
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.
1 parent 9d718b5 commit e3fae89

File tree

12 files changed

+596
-101
lines changed

12 files changed

+596
-101
lines changed

lib/pg_search/configuration/association.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,16 @@ def selects
3939

4040
def selects_for_singular_association
4141
columns.map do |column|
42-
"#{column.full_name}::text AS #{column.alias}"
42+
if column.tsvector_column
43+
"tsvector_agg(#{column.full_name}) AS #{column.alias}"
44+
else
45+
case postgresql_version
46+
when 0..90000
47+
"array_to_string(array_agg(#{column.full_name}::text), ' ') AS #{column.alias}"
48+
else
49+
"string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
50+
end
51+
end
4352
end.join(", ")
4453
end
4554

lib/pg_search/configuration/column.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
module PgSearch
66
class Configuration
77
class Column
8-
attr_reader :weight, :name
8+
attr_reader :weight, :tsvector_column, :name
99

1010
def initialize(column_name, weight, model)
1111
@name = column_name.to_s
1212
@column_name = column_name.to_s
13-
@weight = weight
13+
if weight.is_a?(Hash)
14+
@weight = weight[:weight]
15+
@tsvector_column = weight[:tsvector_column]
16+
else
17+
@weight = options
18+
end
1419
@model = model
1520
@connection = model.connection
1621
end
@@ -20,7 +25,11 @@ def full_name
2025
end
2126

2227
def to_sql
23-
"coalesce(#{expression}::text, '')"
28+
if tsvector_column
29+
"coalesce(#{expression}, '')"
30+
else
31+
"coalesce(#{expression}::text, '')"
32+
end
2433
end
2534

2635
private

lib/pg_search/features/tsearch.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,15 @@ def columns_to_use
193193
end
194194

195195
def column_to_tsvector(search_column)
196-
tsvector = Arel::Nodes::NamedFunction.new(
197-
"to_tsvector",
198-
[dictionary, Arel.sql(normalize(search_column.to_sql))]
199-
).to_sql
196+
tsvector =
197+
if search_column.tsvector_column
198+
search_column.to_sql
199+
else
200+
Arel::Nodes::NamedFunction.new(
201+
"to_tsvector",
202+
[dictionary, Arel.sql(normalize(search_column.to_sql))]
203+
).to_sql
204+
end
200205

201206
if search_column.weight.nil?
202207
tsvector
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require 'pg_search/migration/generator'
4+
5+
module PgSearch
6+
module Migration
7+
class AssociatedAgainstTsvectorGenerator < Generator
8+
def migration_name
9+
'add_pg_search_associated_against_tsvector_support_functions'
10+
end
11+
end
12+
end
13+
end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class AddPgSearchAssociatedAgainstTsvectorSupportFunctions < ActiveRecord::Migration
2+
def self.up
3+
say_with_time("Adding tsvector support functions for pg_search :associated_against") do
4+
execute <<-'SQL'
5+
<%= read_sql_file "tsvector_agg" %>
6+
SQL
7+
end
8+
end
9+
10+
def self.down
11+
say_with_time("Dropping tsvector support functions for pg_search :associated_against") do
12+
execute <<-'SQL'
13+
<%= read_sql_file "uninstall_tsvector_agg" %>
14+
SQL
15+
end
16+
end
17+
end

lib/pg_search/railtie.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ class Railtie < Rails::Railtie
99
generators do
1010
require "pg_search/migration/multisearch_generator"
1111
require "pg_search/migration/dmetaphone_generator"
12+
require "pg_search/migration/associated_against_generator"
13+
require "pg_search/migration/associated_against_tsvector_generator"
1214
end
1315
end
1416
end

0 commit comments

Comments
 (0)