polysearch 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 21bf6d369ed9516908e5ac52bb3b5688335e05952e920741cccd40d73b2279d8
4
+ data.tar.gz: 7257fa7063ad0b250c7d3b602a7acc5bc3b42984b6fefa47202629eedbb03018
5
+ SHA512:
6
+ metadata.gz: 8995e6d99585aadc2add2e47b9d18829313dd29853468c706e0297a4361b633acbe593db5fcd34e0a2890cc1832f4758d95f74d3a10ad990fde0dc41e91f348b
7
+ data.tar.gz: c6e75132f957e688b23ef5499fc8af50189edeb470ed664ad3dab4ca5096a3a343e7e33902d98fb0db2113b7993610b8126ee20653bac026787c7c3397c9d380
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in polysearch.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,180 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ polysearch (0.1.0)
5
+ rails (>= 6.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ actioncable (6.1.3.2)
11
+ actionpack (= 6.1.3.2)
12
+ activesupport (= 6.1.3.2)
13
+ nio4r (~> 2.0)
14
+ websocket-driver (>= 0.6.1)
15
+ actionmailbox (6.1.3.2)
16
+ actionpack (= 6.1.3.2)
17
+ activejob (= 6.1.3.2)
18
+ activerecord (= 6.1.3.2)
19
+ activestorage (= 6.1.3.2)
20
+ activesupport (= 6.1.3.2)
21
+ mail (>= 2.7.1)
22
+ actionmailer (6.1.3.2)
23
+ actionpack (= 6.1.3.2)
24
+ actionview (= 6.1.3.2)
25
+ activejob (= 6.1.3.2)
26
+ activesupport (= 6.1.3.2)
27
+ mail (~> 2.5, >= 2.5.4)
28
+ rails-dom-testing (~> 2.0)
29
+ actionpack (6.1.3.2)
30
+ actionview (= 6.1.3.2)
31
+ activesupport (= 6.1.3.2)
32
+ rack (~> 2.0, >= 2.0.9)
33
+ rack-test (>= 0.6.3)
34
+ rails-dom-testing (~> 2.0)
35
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
36
+ actiontext (6.1.3.2)
37
+ actionpack (= 6.1.3.2)
38
+ activerecord (= 6.1.3.2)
39
+ activestorage (= 6.1.3.2)
40
+ activesupport (= 6.1.3.2)
41
+ nokogiri (>= 1.8.5)
42
+ actionview (6.1.3.2)
43
+ activesupport (= 6.1.3.2)
44
+ builder (~> 3.1)
45
+ erubi (~> 1.4)
46
+ rails-dom-testing (~> 2.0)
47
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
48
+ activejob (6.1.3.2)
49
+ activesupport (= 6.1.3.2)
50
+ globalid (>= 0.3.6)
51
+ activemodel (6.1.3.2)
52
+ activesupport (= 6.1.3.2)
53
+ activerecord (6.1.3.2)
54
+ activemodel (= 6.1.3.2)
55
+ activesupport (= 6.1.3.2)
56
+ activestorage (6.1.3.2)
57
+ actionpack (= 6.1.3.2)
58
+ activejob (= 6.1.3.2)
59
+ activerecord (= 6.1.3.2)
60
+ activesupport (= 6.1.3.2)
61
+ marcel (~> 1.0.0)
62
+ mini_mime (~> 1.0.2)
63
+ activesupport (6.1.3.2)
64
+ concurrent-ruby (~> 1.0, >= 1.0.2)
65
+ i18n (>= 1.6, < 2)
66
+ minitest (>= 5.1)
67
+ tzinfo (~> 2.0)
68
+ zeitwerk (~> 2.3)
69
+ ast (2.4.2)
70
+ builder (3.2.4)
71
+ concurrent-ruby (1.1.8)
72
+ crass (1.0.6)
73
+ erubi (1.10.0)
74
+ globalid (0.4.2)
75
+ activesupport (>= 4.2.0)
76
+ i18n (1.8.10)
77
+ concurrent-ruby (~> 1.0)
78
+ loofah (2.9.1)
79
+ crass (~> 1.0.2)
80
+ nokogiri (>= 1.5.9)
81
+ magic_frozen_string_literal (1.2.0)
82
+ mail (2.7.1)
83
+ mini_mime (>= 0.1.1)
84
+ marcel (1.0.1)
85
+ method_source (1.0.0)
86
+ mini_mime (1.0.3)
87
+ mini_portile2 (2.5.1)
88
+ minitest (5.14.4)
89
+ nio4r (2.5.7)
90
+ nokogiri (1.11.4)
91
+ mini_portile2 (~> 2.5.0)
92
+ racc (~> 1.4)
93
+ nokogiri (1.11.4-arm64-darwin)
94
+ racc (~> 1.4)
95
+ parallel (1.20.1)
96
+ parser (3.0.1.1)
97
+ ast (~> 2.4.1)
98
+ racc (1.5.2)
99
+ rack (2.2.3)
100
+ rack-test (1.1.0)
101
+ rack (>= 1.0, < 3)
102
+ rails (6.1.3.2)
103
+ actioncable (= 6.1.3.2)
104
+ actionmailbox (= 6.1.3.2)
105
+ actionmailer (= 6.1.3.2)
106
+ actionpack (= 6.1.3.2)
107
+ actiontext (= 6.1.3.2)
108
+ actionview (= 6.1.3.2)
109
+ activejob (= 6.1.3.2)
110
+ activemodel (= 6.1.3.2)
111
+ activerecord (= 6.1.3.2)
112
+ activestorage (= 6.1.3.2)
113
+ activesupport (= 6.1.3.2)
114
+ bundler (>= 1.15.0)
115
+ railties (= 6.1.3.2)
116
+ sprockets-rails (>= 2.0.0)
117
+ rails-dom-testing (2.0.3)
118
+ activesupport (>= 4.2.0)
119
+ nokogiri (>= 1.6)
120
+ rails-html-sanitizer (1.3.0)
121
+ loofah (~> 2.3)
122
+ railties (6.1.3.2)
123
+ actionpack (= 6.1.3.2)
124
+ activesupport (= 6.1.3.2)
125
+ method_source
126
+ rake (>= 0.8.7)
127
+ thor (~> 1.0)
128
+ rainbow (3.0.0)
129
+ rake (13.0.3)
130
+ regexp_parser (2.1.1)
131
+ rexml (3.2.5)
132
+ rubocop (1.14.0)
133
+ parallel (~> 1.10)
134
+ parser (>= 3.0.0.0)
135
+ rainbow (>= 2.2.2, < 4.0)
136
+ regexp_parser (>= 1.8, < 3.0)
137
+ rexml
138
+ rubocop-ast (>= 1.5.0, < 2.0)
139
+ ruby-progressbar (~> 1.7)
140
+ unicode-display_width (>= 1.4.0, < 3.0)
141
+ rubocop-ast (1.5.0)
142
+ parser (>= 3.0.1.1)
143
+ rubocop-performance (1.11.2)
144
+ rubocop (>= 1.7.0, < 2.0)
145
+ rubocop-ast (>= 0.4.0)
146
+ ruby-progressbar (1.11.0)
147
+ sprockets (4.0.2)
148
+ concurrent-ruby (~> 1.0)
149
+ rack (> 1, < 3)
150
+ sprockets-rails (3.2.2)
151
+ actionpack (>= 4.0)
152
+ activesupport (>= 4.0)
153
+ sprockets (>= 3.0.0)
154
+ standard (1.1.1)
155
+ rubocop (= 1.14.0)
156
+ rubocop-performance (= 1.11.2)
157
+ standardrb (1.0.0)
158
+ standard
159
+ thor (1.1.0)
160
+ tzinfo (2.0.4)
161
+ concurrent-ruby (~> 1.0)
162
+ unicode-display_width (2.0.0)
163
+ websocket-driver (0.7.3)
164
+ websocket-extensions (>= 0.1.0)
165
+ websocket-extensions (0.1.5)
166
+ zeitwerk (2.4.2)
167
+
168
+ PLATFORMS
169
+ aarch64-linux-musl
170
+ arm64-darwin-20
171
+
172
+ DEPENDENCIES
173
+ bundler (~> 2.0)
174
+ magic_frozen_string_literal
175
+ polysearch!
176
+ rake (~> 13.0)
177
+ standardrb
178
+
179
+ BUNDLED WITH
180
+ 2.2.15
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Hopsoft
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,67 @@
1
+ # Polysearch
2
+
3
+ #### Simplified polymorphic full text + similarity search based on postgres
4
+
5
+ ## Requirements
6
+
7
+ - Postgresql >= 11
8
+ - Rails >= 6.0
9
+
10
+ ## Usage
11
+
12
+ 1. Add the gem to your project
13
+
14
+ ```sh
15
+ bundle add polysearch
16
+ ```
17
+
18
+ 1. Run the generator
19
+
20
+ ```sh
21
+ bundle exec rails g polysearch:migration
22
+ ```
23
+
24
+ You can also specify a datatype that your app uses for primary keys (default is `bigint`).
25
+ For example, if your application uses `uuid` primary keys, you install the migration like this.
26
+
27
+ ```sh
28
+ bundle exec rails g polysearch:migration uuid
29
+ ```
30
+
31
+ 1. Migrate the database
32
+
33
+ ```sh
34
+ bundle exec rails db:migrate
35
+ ```
36
+
37
+ 1. Update the model(s) you'd like to search
38
+
39
+ ```ruby
40
+ class User < ApplicationRecord
41
+ include Polysearch::Searchable
42
+
43
+ after_save_commit :update_polysearch
44
+
45
+ def to_tsvectors
46
+ []
47
+ .then { |list| list << make_tsvector(first_name, weight: "A") }
48
+ .then { |list| list << make_tsvector(last_name, weight: "A") }
49
+ .then { |list| list << make_tsvector(email, weight: "B") }
50
+ end
51
+ end
52
+ ```
53
+
54
+ 1. Start searching
55
+
56
+ ```
57
+ User.create first_name: "Nate", last_name: "Hopkins", email: "nhopkins@mailinator.com"
58
+
59
+ User.polysearch("nate")
60
+ User.polysearch("ntae") # misspellings also return results
61
+ User.polysearch("nate").where(created_at: 1.day.ago..Current.time) # active record chaining
62
+ User.polysearch("nate").order(created_at: :desc) # chain additional ordering after the polysearch scope
63
+ ```
64
+
65
+ ## License
66
+
67
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polysearch
4
+ module Searchable
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ def ngrams(value, min: 1, max: 24)
9
+ value = value.to_s
10
+ Set.new.tap do |set|
11
+ (min..max).each do |num|
12
+ value.scan(/\w{#{num}}/).each { |item| set << item }
13
+ end
14
+ end.to_a
15
+ end
16
+
17
+ def fts_words(value)
18
+ Loofah.fragment(value.to_s).scrub!(:whitewash).to_text.gsub(/\W/, " ").squeeze(" ").downcase.split
19
+ end
20
+
21
+ def fts_string(value)
22
+ value = fts_words(value).join(" ")
23
+ value = value.chop while value.bytes.size > 2046
24
+ value
25
+ end
26
+
27
+ def sanitize_sql_value(value)
28
+ sanitize_sql_array ["?", value]
29
+ end
30
+ end
31
+
32
+ delegate :ngrams, :fts_words, :fts_string, :sanitize_sql_value, to: "self.class"
33
+ delegate :quote_column_name, to: "self.class.connection"
34
+
35
+ included do
36
+ has_one :polysearch, as: :searchable, class_name: "Polysearch::Record", inverse_of: "searchable"
37
+ after_destroy :destroy_polysearch
38
+
39
+ scope :polysearch, ->(value) {
40
+ if value.blank?
41
+ all
42
+ else
43
+ fts_rank_alias = "#{table_name.singularize}_fts_rank"
44
+ similarity_rank_alias = "#{table_name.singularize}_similarity_rank"
45
+
46
+ fts = Polysearch::Record
47
+ .select_fts_rank(value, :searchable_id, rank_alias: fts_rank_alias)
48
+ .select_similarity_rank(value, :searchable_id, rank_alias: similarity_rank_alias)
49
+ .where(searchable_type: name)
50
+ .fts(value).or(Polysearch::Record.similar(value))
51
+
52
+ query = <<~SQL
53
+ SELECT searchables.*, fts.#{fts_rank_alias}, fts.#{similarity_rank_alias} from (#{fts.to_sql}) fts
54
+ LEFT JOIN LATERAL (select * from #{table_name} WHERE id = fts.searchable_id) searchables ON TRUE
55
+ SQL
56
+
57
+ select(Arel.star).from(Arel::Nodes::SqlLiteral.new("(#{query})").as(table_name))
58
+ .reorder(fts_rank_alias => :desc, similarity_rank_alias => :desc)
59
+ end
60
+ }
61
+ end
62
+
63
+ def update_polysearch
64
+ tsvectors = to_tsvectors.compact.uniq
65
+ return if tsvectors.blank?
66
+ tsvectors.pop while tsvectors.size > 500
67
+ tsvectors.concat similarity_words_tsvectors
68
+ tsvector = tsvectors.join(" || ")
69
+ fts = Polysearch::Record.where(searchable: self).first_or_create
70
+ fts.update_value tsvector
71
+ fts.update_columns words: similarity_words.join(" ")
72
+ end
73
+
74
+ # Polysearch::Searchable#to_tsvectors is abstract... a noop by default
75
+ # it must be implemented in including ActiveRecord models if this behavior is desired
76
+ #
77
+ # Example:
78
+ #
79
+ # def to_tsvectors
80
+ # []
81
+ # .then { |result| result << make_tsvector(EXAMPLE_COLUMN_OR_PROPERTY, weight: "A") }
82
+ # .then { |result| EXAMPLE_TAGS_COLUMN.each_with_object(result) { |tag, memo| memo << make_tsvector(tag, weight: "B") } }
83
+ # end
84
+ #
85
+ def to_tsvectors
86
+ []
87
+ end
88
+
89
+ def similarity_words
90
+ tsvectors = to_tsvectors.compact.uniq
91
+ return [] if tsvectors.blank?
92
+ tsvector = tsvectors.join(" || ")
93
+
94
+ ts_stat = Arel::Nodes::NamedFunction.new("ts_stat", [
95
+ Arel::Nodes::SqlLiteral.new(sanitize_sql_value("SELECT #{tsvector}"))
96
+ ])
97
+ length = Arel::Nodes::NamedFunction.new("length", [Arel::Nodes::SqlLiteral.new(quote_column_name(:word))])
98
+ query = self.class.select(:word).from(ts_stat.to_sql).where(length.gteq(3)).to_sql
99
+ result = self.class.connection.execute(query)
100
+ result.values.flatten
101
+ end
102
+
103
+ def similarity_ngrams
104
+ similarity_words.each_with_object(Set.new) do |word, memo|
105
+ ngrams(word).each { |ngram| memo << ngram }
106
+ end.to_a.sort_by(&:length)
107
+ end
108
+
109
+ def similarity_words_tsvectors(weight: "D")
110
+ similarity_ngrams.each_with_object([]) do |ngram, memo|
111
+ memo << make_tsvector(ngram, weight: weight)
112
+ end
113
+ end
114
+
115
+ protected
116
+
117
+ def make_tsvector(value, weight: "D")
118
+ value = fts_string(value).gsub(/\W/, " ").squeeze.downcase
119
+ return nil if value.blank?
120
+ to_tsv = Arel::Nodes::NamedFunction.new("to_tsvector", [
121
+ Arel::Nodes::SqlLiteral.new("'simple'"),
122
+ Arel::Nodes::SqlLiteral.new(sanitize_sql_value(value))
123
+ ])
124
+ setweight = Arel::Nodes::NamedFunction.new("setweight", [
125
+ to_tsv,
126
+ Arel::Nodes::SqlLiteral.new(sanitize_sql_value(weight))
127
+ ])
128
+ setweight.to_sql
129
+ end
130
+
131
+ private
132
+
133
+ def destroy_polysearch
134
+ polysearch&.destroy
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,99 @@
1
+ # frozen_string_literal: true
2
+
3
+ # == Schema Information
4
+ #
5
+ # Table name: polysearches
6
+ #
7
+ # id :uuid not null, primary key
8
+ # searchable_type :string not null
9
+ # value :tsvector
10
+ # words :text
11
+ # created_at :datetime not null
12
+ # updated_at :datetime not null
13
+ # searchable_id :uuid not null
14
+ #
15
+ # Indexes
16
+ #
17
+ # index_polysearches_on_created_at (created_at)
18
+ # index_polysearches_on_searchable_type_and_searchable_id (searchable_type,searchable_id) UNIQUE
19
+ # index_polysearches_on_updated_at (updated_at)
20
+ # index_polysearches_on_value (value) USING gin
21
+ # index_polysearches_on_words (words) USING gin
22
+ #
23
+ module Polysearch
24
+ class Record < ActiveRecord::Base
25
+ # extends ...................................................................
26
+ # includes ..................................................................
27
+
28
+ # relationships .............................................................
29
+ belongs_to :searchable, polymorphic: true, inverse_of: "polysearch"
30
+
31
+ # validations ...............................................................
32
+ # callbacks .................................................................
33
+
34
+ # scopes ....................................................................
35
+
36
+ scope :select_fts_rank, ->(value, *selects, rank_alias: nil) {
37
+ value = value.to_s.gsub(/\W/, " ").squeeze(" ").downcase.strip
38
+ value = Arel::Nodes::SqlLiteral.new(sanitize_sql_array(["?", value.to_s]))
39
+ plainto_tsquery = Arel::Nodes::NamedFunction.new("plainto_tsquery", [Arel::Nodes::SqlLiteral.new("'simple'"), value])
40
+ ts_rank = Arel::Nodes::NamedFunction.new("ts_rank", [arel_table[:value], plainto_tsquery])
41
+
42
+ rank_alias ||= "fts_rank"
43
+ selects << Arel.star if selects.blank?
44
+ selects << ts_rank.as(rank_alias)
45
+ select(*selects).order("#{rank_alias} desc")
46
+ }
47
+
48
+ scope :fts, ->(value) {
49
+ value = value.to_s.gsub(/\W/, " ").squeeze(" ").downcase.strip
50
+ value = Arel::Nodes::SqlLiteral.new(sanitize_sql_array(["?", value.to_s]))
51
+ plainto_tsquery = Arel::Nodes::NamedFunction.new("plainto_tsquery", [Arel::Nodes::SqlLiteral.new("'simple'"), value])
52
+ where(Arel::Nodes::InfixOperation.new("@@", arel_table[:value], plainto_tsquery))
53
+ }
54
+
55
+ scope :select_similarity_rank, ->(value, *selects, rank_alias: nil) {
56
+ value = value.to_s.gsub(/\W/, " ").squeeze(" ").downcase.strip
57
+ value = Arel::Nodes::SqlLiteral.new(sanitize_sql_array(["?", value.to_s]))
58
+
59
+ rank_alias ||= "similarity_rank"
60
+ selects << Arel.star if selects.blank?
61
+ selects << Arel::Nodes::NamedFunction.new("similarity", [arel_table[:words], value]).as(rank_alias)
62
+ select(*selects).order("#{rank_alias} desc")
63
+ }
64
+
65
+ scope :similar, ->(value, range: 0.01) {
66
+ value = value.to_s.gsub(/\W/, " ").squeeze(" ").downcase.strip
67
+ value = Arel::Nodes::SqlLiteral.new(sanitize_sql_array(["?", value.to_s]))
68
+ where Arel::Nodes::NamedFunction.new("similarity", [arel_table[:words], value]).gteq(range)
69
+ }
70
+
71
+ # additional config (i.e. accepts_nested_attribute_for etc...) ..............
72
+ self.table_name = "polysearches"
73
+ self.primary_key = :id
74
+
75
+ # class methods .............................................................
76
+ class << self
77
+ end
78
+
79
+ # public instance methods ...................................................
80
+
81
+ def update_value(tsvector_sql)
82
+ sql = <<~SQL
83
+ UPDATE polysearches
84
+ SET value = (#{tsvector_sql})
85
+ WHERE searchable_type = :searchable_type
86
+ AND searchable_id = :searchable_id;
87
+ SQL
88
+ self.class.connection.execute self.class.sanitize_sql_array([
89
+ sql,
90
+ searchable_type: searchable_type,
91
+ searchable_id: searchable_id
92
+ ])
93
+ end
94
+
95
+ # protected instance methods ................................................
96
+
97
+ # private instance methods ..................................................
98
+ end
99
+ end
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "polysearch"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/bin/standardize ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env bash
2
+
3
+ bundle exec magic_frozen_string_literal
4
+ bundle exec standardrb --fix
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # require "rails/generators/base"
4
+
5
+ module Polysearch
6
+ module Generators
7
+ class MigrationGenerator < Rails::Generators::Base
8
+ desc "Copy polysearch database migrations into your project"
9
+ argument :searchable_id_datatype, type: :string, default: "bigint"
10
+ source_root File.expand_path("../templates", __FILE__)
11
+
12
+ def copy_polysearch_migration
13
+ template "migration.rb", "db/migrate/#{timestamp}_add_polysearch.rb",
14
+ searchable_id_datatype: searchable_id_datatype,
15
+ migration_version: migration_version
16
+ end
17
+
18
+ private
19
+
20
+ def timestamp
21
+ DateTime.current.strftime "%Y%m%d%H%M%S"
22
+ end
23
+
24
+ def migration_version
25
+ "[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AddPolysearch < ActiveRecord::Migration<%= migration_version %>
4
+ def change
5
+ enable_extension :pg_trgm
6
+
7
+ create_table :polysearches, primary_key: [:id, :searchable_type], options: "PARTITION BY HASH (searchable_type)" do |t|
8
+ t.uuid :id, null: false, default: "gen_random_uuid()"
9
+ t.string :searchable_type, null: false
10
+ t.<%= searchable_id_datatype %> :searchable_id, null: false
11
+ t.text :words
12
+ t.tsvector :value
13
+ t.timestamps
14
+
15
+ t.index [:searchable_type, :searchable_id], unique: true
16
+ t.index :words, using: :gin, opclass: :gin_trgm_ops
17
+ t.index :value, using: :gin
18
+ t.index :created_at
19
+ t.index :updated_at
20
+ end
21
+
22
+ reversible do |dir|
23
+ dir.up do
24
+ execute "CREATE TABLE polysearches_01 PARTITION OF polysearches FOR VALUES WITH (MODULUS 4, REMAINDER 0);"
25
+ execute "CREATE TABLE polysearches_02 PARTITION OF polysearches FOR VALUES WITH (MODULUS 4, REMAINDER 1);"
26
+ execute "CREATE TABLE polysearches_03 PARTITION OF polysearches FOR VALUES WITH (MODULUS 4, REMAINDER 2);"
27
+ execute "CREATE TABLE polysearches_04 PARTITION OF polysearches FOR VALUES WITH (MODULUS 4, REMAINDER 3);"
28
+ end
29
+ dir.down do
30
+ drop_table :polysearches_01
31
+ drop_table :polysearches_02
32
+ drop_table :polysearches_03
33
+ drop_table :polysearches_04
34
+ end
35
+ end
36
+ end
37
+ end
data/lib/polysearch.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "polysearch/version"
4
+ require_relative "../app/models/record"
5
+ require_relative "../app/models/concerns/searchable"
6
+
7
+ module Polysearch
8
+ class Engine < Rails::Engine
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Polysearch
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require File.expand_path("../lib/polysearch/version", __FILE__)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "polysearch"
7
+ gem.license = "MIT"
8
+ gem.version = Polysearch::VERSION
9
+ gem.authors = ["Nathan Hopkins"]
10
+ gem.email = ["natehop@gmail.com"]
11
+ gem.homepage = "https://github.com/hopsoft/polysearch"
12
+ gem.summary = "Simplified polymorphic full text + similarity search based on postgres"
13
+
14
+ gem.metadata = {
15
+ "homepage_uri" => gem.homepage,
16
+ "source_code_uri" => gem.homepage
17
+ }
18
+
19
+ gem.files = Dir["app/**/*", "lib/**/*", "bin/*", "[A-Z]*"]
20
+ gem.test_files = Dir["test/**/*.rb"]
21
+
22
+ gem.add_dependency "rails", ">= 6.0"
23
+
24
+ gem.add_development_dependency "bundler", "~> 2.0"
25
+ gem.add_development_dependency "rake"
26
+ gem.add_development_dependency "standardrb"
27
+ gem.add_development_dependency "magic_frozen_string_literal"
28
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polysearch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Hopkins
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '6.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: standardrb
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: magic_frozen_string_literal
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - natehop@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - Gemfile.lock
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - app/models/concerns/searchable.rb
96
+ - app/models/record.rb
97
+ - bin/console
98
+ - bin/setup
99
+ - bin/standardize
100
+ - lib/generators/polysearch/migration_generator.rb
101
+ - lib/generators/polysearch/templates/migration.rb
102
+ - lib/polysearch.rb
103
+ - lib/polysearch/version.rb
104
+ - polysearch.gemspec
105
+ homepage: https://github.com/hopsoft/polysearch
106
+ licenses:
107
+ - MIT
108
+ metadata:
109
+ homepage_uri: https://github.com/hopsoft/polysearch
110
+ source_code_uri: https://github.com/hopsoft/polysearch
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubygems_version: 3.2.15
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Simplified polymorphic full text + similarity search based on postgres
130
+ test_files: []