Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for tsvector_column in associated_against (pg_search_scope) #504

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby 2.7.6
12 changes: 10 additions & 2 deletions lib/pg_search/configuration/association.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,21 @@ def selects

def selects_for_singular_association
columns.map do |column|
"#{column.full_name}::text AS #{column.alias}"
if column.tsvector_column
"#{column.full_name}::tsvector AS #{column.alias}"
else
"#{column.full_name}::text AS #{column.alias}"
end
end.join(", ")
end

def selects_for_multiple_association
columns.map do |column|
"string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
if column.tsvector_column
"tsvector_agg(#{column.full_name}) AS #{column.alias}"
else
"string_agg(#{column.full_name}::text, ' ') AS #{column.alias}"
end
end.join(", ")
end

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,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
@name = column_name.to_s
@column_name = column_name
@weight = weight
if weight.is_a?(Hash)
@weight = weight[:weight]
@tsvector_column = weight[:tsvector_column]
else
@weight = weight
end
@model = model
@connection = model.connection
end
Expand All @@ -22,7 +27,11 @@ def full_name
end

def to_sql
"coalesce((#{expression})::text, '')"
if tsvector_column
"coalesce((#{expression})::tsvector, '')"
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 @@ -194,10 +194,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