autosuggest 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '083927cb6e763a26bad61351189dd1a1c1e66d31da0a4844635ec5440f4620d0'
4
- data.tar.gz: 94245fc7eaeb98f54669b561311d9dee4ac43788f88cef3768a33891c8b530ad
3
+ metadata.gz: 3f04e20f21653bcc9a8941c6da1eddae7726cf775abea699b78022b64fec9e48
4
+ data.tar.gz: 7a5c362428558d17808310245d7cad1bdf693800419665d650ab9026e5aa8628
5
5
  SHA512:
6
- metadata.gz: f20b852dfcb4cf4249b4c4b2a72e7f58c2c2b642a11b14b33e632ee934736c721d70bcd87f7b6e20659ba5e82f05ce679b69d0e5e72f3b4c64d8187078d2b8c3
7
- data.tar.gz: 6e99a02d77dc1ea2cf06e0903d6adebbbe73579496de092efff471369226dfb917df9bd31cee7697bc2d0fcd9392cbe6393186a6c4a747b49a6b03cee348108a
6
+ metadata.gz: 62c38fb482638185c18dde9b0b4db1c9aa4deaa6e8f0fb4341220f61c28b218c51f79376b1080fb954982f0aa2b9a979b20b4212bfb704f73fe2e92be8b4373e
7
+ data.tar.gz: 84383dd6de2c654aefabe546f3c01335f906d5cfb517fd1890b26376ed9f482651b6e7e8d1b5082d120d228603b7b0002227b4293d2668c5de51a464aa831fa5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.1.3 (2021-11-23)
2
+
3
+ - Added model generator
4
+
1
5
  ## 0.1.2 (2021-11-22)
2
6
 
3
7
  - Added `filter` option to `suggestions` method
data/README.md CHANGED
@@ -85,17 +85,90 @@ There are two ways to build the corpus, which can be used together.
85
85
  autosuggest.block_words ["boom"]
86
86
  ```
87
87
 
88
- #### Profit
88
+ #### Generate suggestions
89
89
 
90
- Get suggestions with:
90
+ Generate suggestions with:
91
91
 
92
92
  ```ruby
93
- autosuggest.suggestions(filter: true)
93
+ suggestions = autosuggest.suggestions(filter: true)
94
+ ```
95
+
96
+ #### Save suggestions
97
+
98
+ Save suggestions in your database or another data store.
99
+
100
+ With Rails, you can generate a simple model with:
101
+
102
+ ```sh
103
+ rails generate autosuggest:suggestions
104
+ rails db:migrate
105
+ ```
106
+
107
+ And update suggestions with:
108
+
109
+ ```ruby
110
+ now = Time.now
111
+ records = suggestions.map { |s| s.slice(:query, :score).merge(updated_at: now) }
112
+ Autosuggest::Suggestion.transaction do
113
+ Autosuggest::Suggestion.upsert_all(records, unique_by: :query)
114
+ Autosuggest::Suggestion.where("updated_at < ?", now).delete_all
115
+ end
116
+ ```
117
+
118
+ Leave out `unique_by` for MySQL, and use [activerecord-import](https://github.com/zdennis/activerecord-import) for upserts with Rails < 6.
119
+
120
+ #### Show suggestions
121
+
122
+ Use a JavaScript autocomplete library like [typeahead.js](https://github.com/twitter/typeahead.js) to show suggestions in the UI.
123
+
124
+ If you only have a few thousand suggestions, it’s much faster to load them all at once instead of as a user types (eliminates network requests).
125
+
126
+ With Rails, you can load all suggestions with:
127
+
128
+ ```ruby
129
+ Autosuggest::Suggestion.order(score: :desc).pluck(:query)
94
130
  ```
95
131
 
96
- Filter queries without results and you’re set. We also prefer to have someone manually approve them by hand.
132
+ And suggestions matching user input with:
97
133
 
98
- ## Full Example
134
+ ```ruby
135
+ input = params[:query]
136
+ Autosuggest::Suggestion
137
+ .order(score: :desc)
138
+ .where("query LIKE ?", "%#{Autosuggest::Suggestion.sanitize_sql_like(input.downcase)}%")
139
+ .pluck(:query)
140
+ ```
141
+
142
+ You can also cache suggestions for performance.
143
+
144
+ ```ruby
145
+ Rails.cache.fetch("suggestions", expires_in: 5.minutes) do
146
+ Autosuggest::Suggestion.order(score: :desc).pluck(:query)
147
+ end
148
+ ```
149
+
150
+ #### Additional considerations
151
+
152
+ You may want to have someone manually approve suggestions:
153
+
154
+ ```ruby
155
+ Autosuggest::Suggestion.where(approved: true)
156
+ ```
157
+
158
+ Or filter suggestions without results:
159
+
160
+ ```ruby
161
+ Autosuggest::Suggestion.find_each do |suggestion|
162
+ suggestion.has_results = Product.search(suggestion.query, load: false, limit: 1).any?
163
+ suggestion.save! if suggestion.changed?
164
+ end
165
+
166
+ Autosuggest::Suggestion.where(has_results: true)
167
+ ```
168
+
169
+ You can add additional fields to your model/data store to accomplish this.
170
+
171
+ ## Example
99
172
 
100
173
  ```ruby
101
174
  top_queries = Searchjoy::Search.group(:normalized_query)
@@ -110,9 +183,14 @@ autosuggest.prefer brand_names
110
183
  autosuggest.not_duplicates [["straws", "straus"]]
111
184
  autosuggest.block_words ["boom"]
112
185
 
113
- puts autosuggest.pretty_suggestions
114
- # or
115
186
  suggestions = autosuggest.suggestions(filter: true)
187
+
188
+ now = Time.now
189
+ records = suggestions.map { |s| s.slice(:query, :score).merge(updated_at: now) }
190
+ Autosuggest::Suggestion.transaction do
191
+ Autosuggest::Suggestion.upsert_all(records, unique_by: :query)
192
+ Autosuggest::Suggestion.where("updated_at < ?", now).delete_all
193
+ end
116
194
  ```
117
195
 
118
196
  ## History
@@ -1,3 +1,3 @@
1
1
  class Autosuggest
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,33 @@
1
+ require "rails/generators/active_record"
2
+
3
+ class Autosuggest
4
+ module Generators
5
+ class SuggestionsGenerator < Rails::Generators::Base
6
+ include ActiveRecord::Generators::Migration
7
+ source_root File.join(__dir__, "templates")
8
+
9
+ def copy_templates
10
+ template "model.rb", "app/models/autosuggest/suggestion.rb"
11
+ migration_template "migration.rb", "db/migrate/create_autosuggest_suggestions.rb", migration_version: migration_version
12
+ end
13
+
14
+ def migration_version
15
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
16
+ end
17
+
18
+ def mysql?
19
+ adapter =~ /mysql/i
20
+ end
21
+
22
+ # use connection_config instead of connection.adapter
23
+ # so database connection isn't needed
24
+ def adapter
25
+ if ActiveRecord::VERSION::STRING.to_f >= 6.1
26
+ ActiveRecord::Base.connection_db_config.adapter.to_s
27
+ else
28
+ ActiveRecord::Base.connection_config[:adapter].to_s
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,11 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
+ def change
3
+ create_table :autosuggest_suggestions do |t|
4
+ t.string :query
5
+ t.float :score
6
+ t.datetime :updated_at<%= mysql? ? ", precision: 6" : "" %>
7
+ end
8
+
9
+ add_index :autosuggest_suggestions, :query, unique: true
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ class Autosuggest::Suggestion < ApplicationRecord
2
+ self.table_name = "autosuggest_suggestions"
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: autosuggest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-11-23 00:00:00.000000000 Z
11
+ date: 2021-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-stemmer
@@ -49,6 +49,9 @@ files:
49
49
  - README.md
50
50
  - lib/autosuggest.rb
51
51
  - lib/autosuggest/version.rb
52
+ - lib/generators/autosuggest/suggestions_generator.rb
53
+ - lib/generators/autosuggest/templates/migration.rb.tt
54
+ - lib/generators/autosuggest/templates/model.rb.tt
52
55
  homepage: https://github.com/ankane/autosuggest
53
56
  licenses:
54
57
  - MIT