From 9c43e4894049c0b4257ef03aaa11cd310f0aa9bd Mon Sep 17 00:00:00 2001 From: Paul B Date: Mon, 16 May 2022 16:33:28 +0200 Subject: [PATCH] tsearch: add configuration to customize columns separator When performing tsearch with Highlighting, the resulting query result concatenates the searched columns with a space. It can be hard to extract the different column data result if both data are text (containing spaces). In order to avoid this issue, we can allow users to customize the columns separator (which needs to be one of the word boundaries used by the query dictionnary). Partial solution for #336. --- lib/pg_search/features/feature.rb | 4 ++-- lib/pg_search/features/tsearch.rb | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/pg_search/features/feature.rb b/lib/pg_search/features/feature.rb index 4d598d63..cacde06b 100644 --- a/lib/pg_search/features/feature.rb +++ b/lib/pg_search/features/feature.rb @@ -24,8 +24,8 @@ def initialize(query, options, all_columns, model, normalizer) attr_reader :query, :options, :all_columns, :model, :normalizer - def document - columns.map(&:to_sql).join(" || ' ' || ") + def document(separator = " ") + columns.map(&:to_sql).join(" || '#{separator}' || ") end def columns diff --git a/lib/pg_search/features/tsearch.rb b/lib/pg_search/features/tsearch.rb index d4136ef3..0d1125b8 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -29,6 +29,7 @@ def highlight def ts_headline Arel::Nodes::NamedFunction.new("ts_headline", [ dictionary, + arel_wrap(document((options[:highlight] || {})[:separator])), arel_wrap(document), arel_wrap(tsquery), Arel::Nodes.build_quoted(ts_headline_options)