This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
196 lines (163 loc) · 4.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package gortex
import (
"errors"
"fmt"
"github.com/jnfeinstein/gorm"
"regexp"
"strings"
)
type SearchSqlFormat interface {
Rank(field string, opts map[string]interface{}) string
Condition(field string, opts map[string]interface{}) string
}
type normalSearchFmt struct{}
func getLanguage(opts map[string]interface{}) (language string) {
language = "simple"
if l, ok := opts["language"].(string); ok {
language = l
}
return
}
func getExclusive(opts map[string]interface{}) (exclusive bool) {
exclusive = true
if e, ok := opts["exclusive"].(bool); ok {
exclusive = e
}
return
}
func getLimit(opts map[string]interface{}) (limit float64) {
limit = -1
if val, ok := opts["limit"]; ok {
switch l := val.(type) {
case float32:
limit = float64(l)
case float64:
limit = l
}
}
return
}
func (n normalSearchFmt) Rank(field string, opts map[string]interface{}) string {
language := getLanguage(opts)
return fmt.Sprintf("COALESCE(ts_rank(to_tsvector('%v', %v), to_tsquery('%v', ?)), 0)", language, field, language)
}
func (n normalSearchFmt) Condition(field string, opts map[string]interface{}) string {
language := getLanguage(opts)
return fmt.Sprintf("to_tsvector('%v', %v) @@ to_tsquery('%v', ?)", language, field, language)
}
type fuzzySearchFmt struct{}
func (f fuzzySearchFmt) Rank(field string, opts map[string]interface{}) string {
return fmt.Sprintf("similarity(%v, ?)", field)
}
func (f fuzzySearchFmt) Condition(field string, opts map[string]interface{}) string {
return fmt.Sprintf("(%v %% ?)", field)
}
func makeSearchScope(sqlFmt SearchSqlFormat, clause interface{}, o ...map[string]interface{}) func(*gorm.DB) *gorm.DB {
var opts map[string]interface{}
if len(o) == 0 {
opts = map[string]interface{}{
"language": "simple",
"exclusive": true,
}
} else {
opts = o[0]
}
exclusive := getExclusive(opts)
return func(db *gorm.DB) (scopeDB *gorm.DB) {
scopeDB = db
scope := db.NewScope("")
var searchTerms map[string]interface{} = make(map[string]interface{})
switch value := clause.(type) {
case map[string]interface{}:
searchTerms = value
case interface{}:
newScope := scopeDB.NewScope(value)
scopeDB = scopeDB.Table(newScope.TableName())
for _, field := range newScope.Fields() {
if !field.IsBlank {
searchTerms[field.DBName] = field.Field.Interface()
}
}
}
if len(searchTerms) == 0 {
return scopeDB
}
var rankTerms []interface{}
var rankSqls []string
for key, val := range searchTerms {
rankQuery := sqlFmt.Rank(scope.Quote(key), opts)
rankSqls = append(rankSqls, rankQuery)
if regexp.MustCompile("\\?").MatchString(rankQuery) {
rankTerms = append(rankTerms, val)
}
whereQuery := sqlFmt.Condition(scope.Quote(key), opts)
if regexp.MustCompile("\\?").MatchString(whereQuery) {
if exclusive {
scopeDB = scopeDB.Where(whereQuery, val)
} else {
scopeDB = scopeDB.Or(whereQuery, val)
}
} else {
if exclusive {
scopeDB = scopeDB.Where(whereQuery)
} else {
scopeDB = scopeDB.Or(whereQuery)
}
}
}
rankStmt := fmt.Sprintf("%s AS %s", strings.Join(rankSqls, "+"), scope.Quote("rank"))
scopeDB = scopeDB.Select(rankStmt, rankTerms...)
orderStmt := fmt.Sprintf("%s desc", scope.Quote("rank"))
scopeDB = scopeDB.Order(orderStmt)
return
}
}
func NewSearchScope(clause interface{}, opts ...map[string]interface{}) func(*gorm.DB) *gorm.DB {
return makeSearchScope(normalSearchFmt{}, clause, opts...)
}
func NewFuzzySearchScope(clause interface{}, opts ...map[string]interface{}) func(*gorm.DB) *gorm.DB {
return makeSearchScope(fuzzySearchFmt{}, clause, opts...)
}
func InitFuzzySearch(db *gorm.DB) error {
return db.Exec("CREATE EXTENSION pg_trgm").Error
}
func SetFuzzySearchLimit(db *gorm.DB, limit float64) error {
return db.Exec("SELECT SET_LIMIT(?)", limit).Error
}
func NewCustomSearchScope(format SearchSqlFormat, clause interface{}, opts ...map[string]interface{}) func(*gorm.DB) *gorm.DB {
return makeSearchScope(format, clause, opts...)
}
func AutoIndex(db *gorm.DB, language string, clause interface{}, table ...string) error {
var fields []string
var actualTable string
scope := db.NewScope(nil)
switch value := clause.(type) {
case string:
if len(table) == 0 {
return errors.New("Table argument is required when clause is type string")
}
fields = append(fields, value)
actualTable = table[0]
case []string:
if len(table) == 0 {
return errors.New("Table argument is required when clause is type []string")
}
fields = value
actualTable = table[0]
case interface{}:
newScope := db.NewScope(value)
actualTable = newScope.TableName()
for _, field := range newScope.Fields() {
if field.IsPrimaryKey {
continue
}
fields = append(fields, field.DBName)
}
}
for _, field := range fields {
idxName := fmt.Sprintf("idx_%v_%v", actualTable, field)
query := fmt.Sprintf("CREATE INDEX %v on %v using gin(to_tsvector('%v', %v))", idxName, scope.Quote(actualTable), language, scope.Quote(field))
db.Exec(query)
}
return nil
}