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 11, 2023
1 parent 96bfffd commit 3739a67
Show file tree
Hide file tree
Showing 14 changed files with 583 additions and 95 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
Gemfile.lock
doc
tags
/.tool-versions
27 changes: 23 additions & 4 deletions lib/pg_search/configuration/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ def table_name
@model.reflect_on_association(@name).table_name
end

def collection?
@model.reflect_on_association(@name).collection?
end

def join(primary_key)
"LEFT OUTER JOIN (#{relation(primary_key).to_sql}) #{subselect_alias} ON #{subselect_alias}.id = #{primary_key}"
end
Expand All @@ -30,6 +34,8 @@ def subselect_alias
private

def selects
postgresql_version = @model.connection.send(:postgresql_version)

if singular_association?
selects_for_singular_association
else
Expand All @@ -39,7 +45,20 @@ def selects

def selects_for_singular_association
columns.map do |column|
"#{column.full_name}::text AS #{column.alias}"
if collection?
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
else
"#{column.full_name} AS #{column.alias}"
end
end.join(", ")
end

Expand All @@ -50,9 +69,9 @@ def selects_for_multiple_association
end

def relation(primary_key)
result = @model.unscoped.joins(@name).select("#{primary_key} AS id, #{selects}")
result = result.group(primary_key) unless singular_association?
result
query = @model.unscoped.joins(@name).select("#{primary_key} AS id, #{selects}")
query = query.group(primary_key) if collection?
query
end

def singular_association?
Expand Down
17 changes: 13 additions & 4 deletions lib/pg_search/configuration/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,31 @@
module PgSearch
class Configuration
class Column
attr_reader :weight, :name
attr_reader :weight, :tsvector_column, :name

def initialize(column_name, weight, model)
def initialize(column_name, options, model)
@name = column_name.to_s
@column_name = column_name.to_s
@weight = weight
@model = model
@connection = model.connection
if options.is_a?(Hash)
@weight = options[:weight]
@tsvector_column = options[:tsvector_column]
else
@weight = options
end
end

def full_name
"#{table_name}.#{column_name}"
end

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

private
Expand Down
5 changes: 2 additions & 3 deletions lib/pg_search/configuration/foreign_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
module PgSearch
class Configuration
class ForeignColumn < Column
attr_reader :weight

def initialize(column_name, weight, model, association)
super(column_name, weight, model)
def initialize(column_name, options, model, association)
super(column_name, options, model)
@association = association
end

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 @@ -192,10 +192,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
11 changes: 11 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,11 @@
require 'pg_search/migration/generator'

module PgSearch
module Migration
class AssociatedAgainstTsvectorGenerator < Generator
def migration_name
'add_pg_search_associated_against_tsvector_support_functions'.freeze
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 3739a67

Please sign in to comment.