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 +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +85 -7
- data/lib/autosuggest/version.rb +1 -1
- data/lib/generators/autosuggest/suggestions_generator.rb +33 -0
- data/lib/generators/autosuggest/templates/migration.rb.tt +11 -0
- data/lib/generators/autosuggest/templates/model.rb.tt +3 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3f04e20f21653bcc9a8941c6da1eddae7726cf775abea699b78022b64fec9e48
|
4
|
+
data.tar.gz: 7a5c362428558d17808310245d7cad1bdf693800419665d650ab9026e5aa8628
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62c38fb482638185c18dde9b0b4db1c9aa4deaa6e8f0fb4341220f61c28b218c51f79376b1080fb954982f0aa2b9a979b20b4212bfb704f73fe2e92be8b4373e
|
7
|
+
data.tar.gz: 84383dd6de2c654aefabe546f3c01335f906d5cfb517fd1890b26376ed9f482651b6e7e8d1b5082d120d228603b7b0002227b4293d2668c5de51a464aa831fa5
|
data/CHANGELOG.md
CHANGED
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
|
-
####
|
88
|
+
#### Generate suggestions
|
89
89
|
|
90
|
-
|
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
|
-
|
132
|
+
And suggestions matching user input with:
|
97
133
|
|
98
|
-
|
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
|
data/lib/autosuggest/version.rb
CHANGED
@@ -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
|
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.
|
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-
|
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
|