Sha256: 383e32e2df54d81550bcd709b8c85c983c747141e82746e6f7b98c6733bbaee3

Contents?: true

Size: 1.5 KB

Versions: 1

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module PgSearch
  # nodoc
  module Features
    # Extends class to enable multiple_ighlight method for the search results
    class TSearch < Feature
      def self.valid_options
        super + %i[dictionary prefix negation any_word normalization tsvector_column highlight multiple_highlight]
      end

      def multiple_highlight
        config = @normalizer.instance_variable_get(:@config)

        against_columns = config.instance_variable_get(:@options)[:against].keys

        @model.search(query).map do |result|
          highlighted_fields = {
            id: result.id
          }
          against_columns.each do |column|
            highlighted_fields[column] = highlight_multiple_fields(result.send(column), query)
          end

          highlighted_fields
        end
      end

      def highlight_multiple_fields(text, query)
        tsquery = ActiveRecord::Base.connection.quote(query)

        dictionary = @options[:dictionary] || "simple"

        raw_options = @options[:multiple_highlight]

        scope_options = raw_options.map { |key, value| "#{key}=#{value}" }.join(", ")

        sanitized_sql = ActiveRecord::Base.send(
          :sanitize_sql_array, [
            "SELECT ts_headline(
              ?, to_tsquery('#{dictionary}', ?),
              '#{scope_options}'
              ) AS highlighted_text",
            text, tsquery
          ]
        )
        ActiveRecord::Base.connection.execute(sanitized_sql).first["highlighted_text"]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_search_multiple_highlight-0.2.0 lib/pg_search_multiple_highlight/tsearch.rb