The project is in a healthy, maintained state
The 'pg_search_multiple_highlight' gem extends the functionality of the popular 'pg_search' gem to overcome its limitation when performing searches against multiple columns and attempting to highlight results. The core issue arises when using the ':highlight' option within the ':tsearch' scope on multiple columns. This gem addresses this limitation by introducing the ':multiple_highlight' option, offering a comprehensive solution for highlighting results across multiple columns. Key Features: New Scope Option: The gem introduces the ':multiple_highlight' scope option, allowing users to perform searches on multiple columns and highlight matching terms. Enhanced Search Results: The gem enables the extraction of highlighted results from multiple columns, providing a unified view of highlighted content. Usage Convenience: Users can easily integrate the ':multiple_highlight' option into their existing 'pg_search' queries by calling the '.with_pg_search_multiple_highlight' method on the search object. Flexible Customization: The gem's options can be tailored to match specific highlighting requirements, such as custom start and stop markers for highlighting. Comprehensive Documentation: The README file explains the limitations of 'pg_search' regarding highlighting, demonstrates how the ':multiple_highlight' option resolves this issue, and offers clear usage examples for quick integration.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 2.3
 Project Readme

PgSearchMultipleHighlight

The pg_search_multiple_highlight gem extends the functionality of the popular pg_search gem to overcome its limitation when performing searches against multiple columns and attempting to highlight results. The core issue arises when using the :highlight option within the :tsearch scope on multiple columns.

This gem addresses this limitation by introducing the :multiple_highlight option, offering a comprehensive solution for highlighting results across multiple columns.

Installation

To install the gem run the following command in the terminal:

$ bundle add pg_search_multiple_highlight

If bundler is not being used to manage dependencies, install the gem by executing:

$ gem install pg_search_multiple_highlight

Usage

To extend functionality of the pg_search gem, require the gemfile in the same file, where a PgSearch module is included:

require 'pg_search_multiple_highlight'

class Shape < ActiveRecord::Base
  include PgSearch::Model
end

Usage of the :multiple_highlight is similar to the standard :highlight.

Adding .with_pg_search_multiple_highlight after the pg_search_scope you can access pg_multiple_highlight attribute for each object.

require 'pg_search_multiple_highlight'

class Article < ActiveRecord::Base
  include PgSearch::Model

  pg_search_scope :search,
                  against: { title: 'A', description: 'B' },
                  using: {
                    tsearch: {
                      multiple_highlight: {
                        StartSel: '<b>',
                        StopSel: '</b>',
                        MaxWords: 400,
                        MinWords: 200,
                        ShortWord: 4,
                        HighlightAll: true,
                        MaxFragments: 3,
                        FragmentDelimiter: '&hellip;'
                      }
                    }
                  }
end

Article.create!(:title => "Quick brown fox", :description => "The quick brown fox jumps over the lazy dog.")

first_match = Article.search("brown").with_pg_search_multiple_highlight.first
first_match.pg_search_multiple_highlight
# => {:id=>1,
#     :title=>"Quick <b>brown</b> fox",
#     :description=>"The quick <b>brown</b> fox jumps over the lazy dog."}

Contributing

Bug reports and pull requests are welcome at https://github.com/msuliq/pg_search_multiple_highlight.

The best way to contribute would be to fork this repo, create a new branch from main, to merge the branch into main of the fork once the new code is in place and then open a pull request to merge forked main into the main of the present repo.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the PgSearchMultipleHighlight project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.