From c8b90a4c49aa683bc55cc6c43e849b9f441d596a 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 | 2 +- 2 files changed, 3 insertions(+), 3 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 a4136bde..a8a9115b 100644 --- a/lib/pg_search/features/tsearch.rb +++ b/lib/pg_search/features/tsearch.rb @@ -29,7 +29,7 @@ def highlight def ts_headline Arel::Nodes::NamedFunction.new("ts_headline", [ dictionary, - arel_wrap(document), + arel_wrap(document((options[:highlight] || {})[:separator])), arel_wrap(tsquery), Arel::Nodes.build_quoted(ts_headline_options) ]).to_sql