Use ts_query on Postgres for performance reasons
Untested.
Cette révision appartient à :
Parent
a3bf2ec128
révision
38f23a3d7f
2 fichiers modifiés avec 27 ajouts et 10 suppressions
|
@ -46,4 +46,4 @@ func (wh *wrappedHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
func wrapHandler(handler http.Handler) http.Handler {
|
||||
return &wrappedHandler{handler}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,17 +18,29 @@ import (
|
|||
)
|
||||
|
||||
var searchOperator string
|
||||
var useTSQuery bool
|
||||
|
||||
func Configure(conf *config.SearchConfig) (err error) {
|
||||
// SQLite has case-insensitive LIKE, but no ILIKE
|
||||
if db.ORM.Dialect().GetName() == "sqlite3" {
|
||||
searchOperator = "LIKE ?"
|
||||
} else {
|
||||
// Postgres needs ILIKE for case-insensitivity
|
||||
if db.ORM.Dialect().GetName() == "postgres" {
|
||||
searchOperator = "ILIKE ?"
|
||||
useTSQuery = true
|
||||
} else {
|
||||
searchOperator = "LIKE ?"
|
||||
useTSQuery = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func stringIsAscii(input string) bool {
|
||||
for _, char := range input {
|
||||
if char > 127 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func SearchByQuery(r *http.Request, pagenum int) (search common.SearchParam, tor []model.Torrent, count int, err error) {
|
||||
search, tor, count, err = searchByQuery(r, pagenum, true)
|
||||
return
|
||||
|
@ -164,7 +176,7 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
|
|||
}
|
||||
|
||||
searchQuerySplit := strings.Fields(search.Query)
|
||||
for i, word := range searchQuerySplit {
|
||||
for _, word := range searchQuerySplit {
|
||||
firstRune, _ := utf8.DecodeRuneInString(word)
|
||||
if len(word) == 1 && unicode.IsPunct(firstRune) {
|
||||
// some queries have a single punctuation character
|
||||
|
@ -175,9 +187,14 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
|
|||
continue
|
||||
}
|
||||
|
||||
// TODO: make this faster ?
|
||||
conditions = append(conditions, "torrent_name "+searchOperator)
|
||||
parameters.Params = append(parameters.Params, "%"+searchQuerySplit[i]+"%")
|
||||
if useTSQuery && stringIsAscii(word) {
|
||||
conditions = append(conditions, "torrent_name @@ plainto_tsquery(?)")
|
||||
parameters.Params = append(parameters.Params, word)
|
||||
} else {
|
||||
// TODO: possible to make this faster?
|
||||
conditions = append(conditions, "torrent_name "+searchOperator)
|
||||
parameters.Params = append(parameters.Params, "%"+word+"%")
|
||||
}
|
||||
}
|
||||
|
||||
parameters.Conditions = strings.Join(conditions[:], " AND ")
|
||||
|
@ -194,4 +211,4 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool) (
|
|||
return
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
|
Référencer dans un nouveau ticket