pg_search_multiple_highlight 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bdd0c8ec85bf0daecf8946a71e289a4fdcfcd3868455e06dbe858fda00e8278f
4
+ data.tar.gz: f2e3fa6a6c9e56dde6dae516ba78a25d270b3ee310d7018b71083dc3eead90eb
5
+ SHA512:
6
+ metadata.gz: e5268edfd87e56068b4eff82fd54e3f3a109d51ca8aeb0a7dc47a0a55523384ae3b2dc147f86e88030c941d3875597013029376f7c2c886c9e4c25928b8bf25c
7
+ data.tar.gz: 55e53e2d7bba07d5f781702267688d062874cb55ba33c79bb67017fed75d09ab3d4cdd2dc5dc2458db41c0132c0a3bf85269aad9e16ccfaaa4a6992e3c58901c
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Metrics/ClassLength:
17
+ Max: 200
18
+
19
+ Metrics/MethodLength:
20
+ Max: 20
21
+
22
+ Metrics/AbcSize:
23
+ Max: 20
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-08
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at slmusayev@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in pg_search_multiple_highlight.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+ gem "minitest", "~> 5.0"
10
+ gem "rubocop", "~> 1.21"
11
+ gem "single_cov", "~> 1.9"
12
+ gem "bundle-audit", "~> 0.1.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,76 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ pg_search_multiple_highlight (0.1.0)
5
+ pg_search (~> 2.3)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (7.0.6)
11
+ activesupport (= 7.0.6)
12
+ activerecord (7.0.6)
13
+ activemodel (= 7.0.6)
14
+ activesupport (= 7.0.6)
15
+ activesupport (7.0.6)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 1.6, < 2)
18
+ minitest (>= 5.1)
19
+ tzinfo (~> 2.0)
20
+ ast (2.4.2)
21
+ bundle-audit (0.1.0)
22
+ bundler-audit
23
+ bundler-audit (0.9.1)
24
+ bundler (>= 1.2.0, < 3)
25
+ thor (~> 1.0)
26
+ concurrent-ruby (1.2.2)
27
+ i18n (1.14.1)
28
+ concurrent-ruby (~> 1.0)
29
+ json (2.6.3)
30
+ language_server-protocol (3.17.0.3)
31
+ minitest (5.19.0)
32
+ parallel (1.23.0)
33
+ parser (3.2.2.3)
34
+ ast (~> 2.4.1)
35
+ racc
36
+ pg_search (2.3.6)
37
+ activerecord (>= 5.2)
38
+ activesupport (>= 5.2)
39
+ racc (1.7.1)
40
+ rainbow (3.1.1)
41
+ rake (13.0.6)
42
+ regexp_parser (2.8.1)
43
+ rexml (3.2.6)
44
+ rubocop (1.55.1)
45
+ json (~> 2.3)
46
+ language_server-protocol (>= 3.17.0)
47
+ parallel (~> 1.10)
48
+ parser (>= 3.2.2.3)
49
+ rainbow (>= 2.2.2, < 4.0)
50
+ regexp_parser (>= 1.8, < 3.0)
51
+ rexml (>= 3.2.5, < 4.0)
52
+ rubocop-ast (>= 1.28.1, < 2.0)
53
+ ruby-progressbar (~> 1.7)
54
+ unicode-display_width (>= 2.4.0, < 3.0)
55
+ rubocop-ast (1.29.0)
56
+ parser (>= 3.2.1.0)
57
+ ruby-progressbar (1.13.0)
58
+ single_cov (1.11.0)
59
+ thor (1.2.2)
60
+ tzinfo (2.0.6)
61
+ concurrent-ruby (~> 1.0)
62
+ unicode-display_width (2.4.2)
63
+
64
+ PLATFORMS
65
+ x86_64-darwin-22
66
+
67
+ DEPENDENCIES
68
+ bundle-audit (~> 0.1.0)
69
+ minitest (~> 5.0)
70
+ pg_search_multiple_highlight!
71
+ rake (~> 13.0)
72
+ rubocop (~> 1.21)
73
+ single_cov (~> 1.9)
74
+
75
+ BUNDLED WITH
76
+ 2.4.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Suleyman Musayev
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # PgSearchMultipleHighlight
2
+
3
+ The `pg_search_multiple_highlight` gem extends the functionality of the popular `pg_search`
4
+ gem to overcome its limitation when performing searches against multiple columns and
5
+ attempting to highlight results. The core issue arises when using the `:highlight` option
6
+ within the `:tsearch` scope on multiple columns.
7
+
8
+ This gem addresses this limitation by introducing the `:multiple_highlight` option,
9
+ offering a comprehensive solution for highlighting results across multiple columns.
10
+
11
+ ## Installation
12
+ To install the gem run the following command in the terminal:
13
+
14
+ $ bundle add pg_search_multiple_highlight
15
+
16
+ If bundler is not being used to manage dependencies, install the gem by executing:
17
+
18
+ $ gem install pg_search_multiple_highlight
19
+
20
+ ## Usage
21
+
22
+ To extend functionality of the `pg_search` gem, require the gemfile in the same file, where
23
+ a PgSearch module is included:
24
+ ```ruby
25
+ require 'pg_search_multiple_highlight'
26
+
27
+ class Shape < ActiveRecord::Base
28
+ include PgSearch::Model
29
+ end
30
+ ```
31
+
32
+ Usage of the `:multiple_highlight` is similar to the standard `:highlight`.
33
+
34
+ Adding `.with_pg_search_multiple_highlight` after the `pg_search_scope` you can access
35
+ `pg_multiple_highlight` attribute for each object.
36
+
37
+
38
+ ```ruby
39
+ require 'pg_search_multiple_highlight'
40
+
41
+ class Article < ActiveRecord::Base
42
+ include PgSearch::Model
43
+
44
+ pg_search_scope :search,
45
+ against: { title: 'A', description: 'B' },
46
+ using: {
47
+ tsearch: {
48
+ multiple_highlight: {
49
+ StartSel: '<b>',
50
+ StopSel: '</b>',
51
+ MaxWords: 400,
52
+ MinWords: 200,
53
+ ShortWord: 4,
54
+ HighlightAll: true,
55
+ MaxFragments: 3,
56
+ FragmentDelimiter: '&hellip;'
57
+ }
58
+ }
59
+ }
60
+ end
61
+
62
+ Article.create!(:title => "Quick brown fox", :description => "The quick brown fox jumps over the lazy dog.")
63
+
64
+ first_match = Article.search("brown").with_pg_search_multiple_highlight.first
65
+ first_match.pg_search_multiple_highlight
66
+ # => {:id=>1,
67
+ # :title=>"Quick <b>brown</b> fox",
68
+ # :description=>"The quick <b>brown</b> fox jumps over the lazy dog."}
69
+ ```
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome at https://github.com/msuliq/pg_search_multiple_highlight.
74
+
75
+ 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.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
80
+
81
+ ## Code of Conduct
82
+
83
+ Everyone interacting in the PgSearchMultipleHighlight project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/msuliq/pg_search_multiple_highlight/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ require "rubocop/rake_task"
13
+
14
+ RuboCop::RakeTask.new
15
+
16
+ task default: %i[test rubocop]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgSearcMultiplehHighlight
4
+ class Error < StandardError; end
5
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgSearch
4
+ # Extends standard scope options for pg_search so :multiple_highlight can be used
5
+ # the same way as other search options
6
+ class ScopeOptions
7
+ def apply(scope)
8
+ scope = include_table_aliasing_for_rank(scope)
9
+ rank_table_alias = scope.pg_search_rank_table_alias(include_counter: true)
10
+
11
+ scope
12
+ .joins(rank_join(rank_table_alias))
13
+ .order(Arel.sql("#{rank_table_alias}.rank DESC, #{order_within_rank}"))
14
+ .extend(WithPgSearchRank)
15
+ .extend(WithPgSearchHighlight[feature_for(:tsearch)])
16
+ .extend(WithPgSearchMultipleHighlight[feature_for(:tsearch)])
17
+ end
18
+
19
+ # This a workaround https://github.com/Casecommons/pg_search/issues/336 issue of the pg_search gem that does not
20
+ # support highlighting results when a tsearch is performed against more than one column
21
+ module WithPgSearchMultipleHighlight
22
+ def self.[](tsearch)
23
+ Module.new do
24
+ include WithPgSearchMultipleHighlight
25
+ define_method(:tsearch) { tsearch }
26
+ end
27
+ end
28
+
29
+ def tsearch
30
+ raise TypeError, "You need to instantiate this module with []"
31
+ end
32
+
33
+ def with_pg_search_multiple_highlight
34
+ scope = self
35
+ scope.select("#{table_name}.*") unless scope.select_values.any?
36
+
37
+ tsearch.multiple_highlight
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgSearch
4
+ # nodoc
5
+ module Features
6
+ # Extends class to enable multiple_ighlight method for the search results
7
+ class TSearch < Feature
8
+ def self.valid_options
9
+ super + %i[multiple_highlight]
10
+ end
11
+
12
+ def multiple_highlight
13
+ config = @normalizer.instance_variable_get(:@config)
14
+
15
+ against_columns = config.instance_variable_get(:@options)[:against].keys
16
+
17
+ @model.search(query).map do |result|
18
+ highlighted_fields = {
19
+ id: result.id
20
+ }
21
+ against_columns.each do |column|
22
+ highlighted_fields[column] = highlight_multiple_fields(result.send(column), query)
23
+ end
24
+
25
+ highlighted_fields
26
+ end
27
+ end
28
+
29
+ def highlight_multiple_fields(text, query)
30
+ tsquery = ActiveRecord::Base.connection.quote(query)
31
+
32
+ dictionary = @options[:dictionary] || "simple"
33
+
34
+ raw_options = @options[:multiple_highlight]
35
+
36
+ scope_options = raw_options.map { |key, value| "#{key}=#{value}" }.join(", ")
37
+
38
+ sanitized_sql = ActiveRecord::Base.send(
39
+ :sanitize_sql_array, [
40
+ "SELECT ts_headline(
41
+ ?, to_tsquery('#{dictionary}', ?),
42
+ '#{scope_options}'
43
+ ) AS highlighted_text",
44
+ text, tsquery
45
+ ]
46
+ )
47
+ ActiveRecord::Base.connection.execute(sanitized_sql).first["highlighted_text"]
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgSearchMultipleHighlight
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pg_search"
4
+ require_relative "pg_search_multiple_highlight/version"
5
+ require_relative "pg_search_multiple_highlight/error"
6
+ require_relative "pg_search_multiple_highlight/tsearch"
7
+ require_relative "pg_search_multiple_highlight/scope_options"
@@ -0,0 +1,4 @@
1
+ module PgSearchMultipleHighlight
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_search_multiple_highlight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Suleyman Musayev
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg_search
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ description: |-
28
+ The 'pg_search_multiple_highlight' gem extends the functionality of
29
+ the popular 'pg_search' gem to overcome its limitation when performing searches against
30
+ multiple columns and attempting to highlight results. The core issue arises when using
31
+ the ':highlight' option within the ':tsearch' scope on multiple columns. This gem
32
+ addresses this limitation by introducing the ':multiple_highlight' option, offering a
33
+ comprehensive solution for highlighting results across multiple columns.
34
+
35
+ Key Features:
36
+ New Scope Option: The gem introduces the ':multiple_highlight' scope option, allowing
37
+ users to perform searches on multiple columns and highlight matching terms.
38
+ Enhanced Search Results: The gem enables the extraction of highlighted results from
39
+ multiple columns, providing a unified view of highlighted content.
40
+ Usage Convenience: Users can easily integrate the ':multiple_highlight' option into
41
+ their existing 'pg_search' queries by calling the '.with_pg_search_multiple_highlight'
42
+ method on the search object.
43
+ Flexible Customization: The gem's options can be tailored to match specific
44
+ highlighting requirements, such as custom start and stop markers for highlighting.
45
+ Comprehensive Documentation: The README file explains the limitations of 'pg_search'
46
+ regarding highlighting, demonstrates how the ':multiple_highlight' option resolves
47
+ this issue, and offers clear usage examples for quick integration.
48
+ email:
49
+ - slmusayev@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - ".rubocop.yml"
55
+ - CHANGELOG.md
56
+ - CODE_OF_CONDUCT.md
57
+ - Gemfile
58
+ - Gemfile.lock
59
+ - LICENSE.txt
60
+ - README.md
61
+ - Rakefile
62
+ - lib/pg_search_multiple_highlight.rb
63
+ - lib/pg_search_multiple_highlight/error.rb
64
+ - lib/pg_search_multiple_highlight/scope_options.rb
65
+ - lib/pg_search_multiple_highlight/tsearch.rb
66
+ - lib/pg_search_multiple_highlight/version.rb
67
+ - sig/pg_search_multiple_highlight.rbs
68
+ homepage: https://github.com/msuliq/pg_search_multiple_highlight
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ homepage_uri: https://github.com/msuliq/pg_search_multiple_highlight
73
+ source_code_uri: https://github.com/msuliq/pg_search_multiple_highlight
74
+ changelog_uri: https://github.com/msuliq/pg_search_multiple_highlight/blob/main/CHANGELOG.md
75
+ rubygems_mfa_required: 'true'
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: 2.6.0
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubygems_version: 3.3.7
92
+ signing_key:
93
+ specification_version: 4
94
+ summary: The 'pg_search_multiple_highlight' gem enhances the 'pg_search' gem by introducing
95
+ a new scope option, ':multiple_highlight', which enables highlighting of search
96
+ results across multiple columns, addressing the limitation of 'pg_search' in handling
97
+ multiple column searches.
98
+ test_files: []