2021-08-29 16:16:49 +02:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gosimple/slug"
|
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
|
|
|
|
"github.com/jinzhu/inflection"
|
|
|
|
)
|
|
|
|
|
2021-09-06 14:16:46 +02:00
|
|
|
// NormalizeSearchQuery replaces search operator with default symbols.
|
|
|
|
func NormalizeSearchQuery(s string) string {
|
|
|
|
s = strings.ToLower(txt.Clip(s, txt.ClipQuery))
|
|
|
|
s = strings.ReplaceAll(s, OrEn, Or)
|
|
|
|
s = strings.ReplaceAll(s, AndEn, And)
|
2021-09-06 15:54:38 +02:00
|
|
|
s = strings.ReplaceAll(s, WithEn, And)
|
2021-09-06 15:59:38 +02:00
|
|
|
s = strings.ReplaceAll(s, InEn, And)
|
2021-09-06 16:07:29 +02:00
|
|
|
s = strings.ReplaceAll(s, AtEn, And)
|
2021-09-06 14:16:46 +02:00
|
|
|
s = strings.ReplaceAll(s, Plus, And)
|
|
|
|
s = strings.ReplaceAll(s, "%", "*")
|
|
|
|
return strings.Trim(s, "+&|_-=!@$%^(){}\\<>,.;: ")
|
|
|
|
}
|
|
|
|
|
2021-09-17 15:52:25 +02:00
|
|
|
// IsTooShort tests if a search query is too short.
|
|
|
|
func IsTooShort(q string) bool {
|
|
|
|
q = strings.Trim(q, "- '")
|
|
|
|
|
|
|
|
return q != "" && len(q) < 3 && txt.IsLatin(q)
|
|
|
|
}
|
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
// LikeAny returns a single where condition matching the search words.
|
2021-08-30 11:26:57 +02:00
|
|
|
func LikeAny(col, s string, keywords, exact bool) (wheres []string) {
|
2021-08-29 19:19:54 +02:00
|
|
|
if s == "" {
|
|
|
|
return wheres
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.ReplaceAll(s, Or, " ")
|
|
|
|
s = strings.ReplaceAll(s, OrEn, " ")
|
|
|
|
s = strings.ReplaceAll(s, AndEn, And)
|
2021-09-06 14:16:46 +02:00
|
|
|
s = strings.ReplaceAll(s, Plus, And)
|
2021-08-29 19:19:54 +02:00
|
|
|
|
|
|
|
var wildcardThreshold int
|
|
|
|
|
2021-08-30 11:26:57 +02:00
|
|
|
if exact {
|
|
|
|
wildcardThreshold = -1
|
|
|
|
} else if keywords {
|
2021-08-29 19:19:54 +02:00
|
|
|
wildcardThreshold = 4
|
|
|
|
} else {
|
|
|
|
wildcardThreshold = 2
|
|
|
|
}
|
2021-08-29 16:16:49 +02:00
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
for _, k := range strings.Split(s, And) {
|
2021-08-29 16:16:49 +02:00
|
|
|
var orWheres []string
|
2021-08-29 19:19:54 +02:00
|
|
|
var words []string
|
2021-08-29 16:16:49 +02:00
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
if keywords {
|
|
|
|
words = txt.UniqueKeywords(k)
|
|
|
|
} else {
|
|
|
|
words = txt.UniqueWords(txt.Words(k))
|
|
|
|
}
|
2021-08-29 16:16:49 +02:00
|
|
|
|
|
|
|
if len(words) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range words {
|
2021-08-30 11:26:57 +02:00
|
|
|
if wildcardThreshold > 0 && len(w) >= wildcardThreshold {
|
2021-08-29 16:16:49 +02:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s%%'", col, w))
|
|
|
|
} else {
|
2021-08-29 19:19:54 +02:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s'", col, w))
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
if !keywords || !txt.ContainsASCIILetters(w) {
|
2021-08-29 16:16:49 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
singular := inflection.Singular(w)
|
|
|
|
|
|
|
|
if singular != w {
|
2021-08-29 19:19:54 +02:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s'", col, singular))
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(orWheres) > 0 {
|
|
|
|
wheres = append(wheres, strings.Join(orWheres, " OR "))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wheres
|
|
|
|
}
|
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
// LikeAnyKeyword returns a single where condition matching the search keywords.
|
|
|
|
func LikeAnyKeyword(col, s string) (wheres []string) {
|
2021-08-30 11:26:57 +02:00
|
|
|
return LikeAny(col, s, true, false)
|
2021-08-29 19:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LikeAnyWord returns a single where condition matching the search word.
|
|
|
|
func LikeAnyWord(col, s string) (wheres []string) {
|
2021-08-30 11:26:57 +02:00
|
|
|
return LikeAny(col, s, false, false)
|
2021-08-29 19:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LikeAll returns a list of where conditions matching all search words.
|
2021-08-30 11:26:57 +02:00
|
|
|
func LikeAll(col, s string, keywords, exact bool) (wheres []string) {
|
2021-08-29 19:19:54 +02:00
|
|
|
if s == "" {
|
|
|
|
return wheres
|
|
|
|
}
|
|
|
|
|
|
|
|
var words []string
|
|
|
|
var wildcardThreshold int
|
|
|
|
|
|
|
|
if keywords {
|
|
|
|
words = txt.UniqueKeywords(s)
|
|
|
|
wildcardThreshold = 4
|
|
|
|
} else {
|
|
|
|
words = txt.UniqueWords(txt.Words(s))
|
|
|
|
wildcardThreshold = 2
|
|
|
|
}
|
2021-08-29 16:16:49 +02:00
|
|
|
|
|
|
|
if len(words) == 0 {
|
|
|
|
return wheres
|
2021-08-30 11:26:57 +02:00
|
|
|
} else if exact {
|
|
|
|
wildcardThreshold = -1
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range words {
|
2021-08-30 11:26:57 +02:00
|
|
|
if wildcardThreshold > 0 && len(w) >= wildcardThreshold {
|
2021-08-29 16:16:49 +02:00
|
|
|
wheres = append(wheres, fmt.Sprintf("%s LIKE '%s%%'", col, w))
|
|
|
|
} else {
|
2021-08-29 19:19:54 +02:00
|
|
|
wheres = append(wheres, fmt.Sprintf("%s LIKE '%s'", col, w))
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return wheres
|
|
|
|
}
|
|
|
|
|
2021-08-29 19:19:54 +02:00
|
|
|
// LikeAllKeywords returns a list of where conditions matching all search keywords.
|
|
|
|
func LikeAllKeywords(col, s string) (wheres []string) {
|
2021-08-30 11:26:57 +02:00
|
|
|
return LikeAll(col, s, true, false)
|
2021-08-29 19:19:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// LikeAllWords returns a list of where conditions matching all search words.
|
|
|
|
func LikeAllWords(col, s string) (wheres []string) {
|
2021-08-30 11:26:57 +02:00
|
|
|
return LikeAll(col, s, false, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LikeAllNames returns a list of where conditions matching all names.
|
2021-09-01 12:48:17 +02:00
|
|
|
func LikeAllNames(cols Cols, s string) (wheres []string) {
|
|
|
|
if len(cols) == 0 || len(s) < 2 {
|
2021-08-30 11:26:57 +02:00
|
|
|
return wheres
|
|
|
|
}
|
|
|
|
|
2021-09-06 14:16:46 +02:00
|
|
|
for _, k := range strings.Split(s, And) {
|
|
|
|
var orWheres []string
|
2021-08-30 11:26:57 +02:00
|
|
|
|
2021-09-06 14:16:46 +02:00
|
|
|
for _, w := range strings.Split(k, Or) {
|
|
|
|
w = strings.TrimSpace(w)
|
2021-08-30 11:26:57 +02:00
|
|
|
|
2021-09-06 14:16:46 +02:00
|
|
|
if w == Empty || len(w) < 2 && txt.IsLatin(w) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cols {
|
|
|
|
if len(w) > 4 {
|
|
|
|
if strings.Contains(w, Space) {
|
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s%%'", c, w))
|
|
|
|
} else {
|
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%%%s%%'", c, w))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s' OR %s LIKE '%s %%'", c, w, c, w))
|
|
|
|
}
|
2021-09-01 12:48:17 +02:00
|
|
|
}
|
2021-08-30 11:26:57 +02:00
|
|
|
}
|
2021-09-06 14:16:46 +02:00
|
|
|
|
|
|
|
if len(orWheres) > 0 {
|
|
|
|
wheres = append(wheres, strings.Join(orWheres, " OR "))
|
|
|
|
}
|
2021-08-30 11:26:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return wheres
|
2021-08-29 19:19:54 +02:00
|
|
|
}
|
|
|
|
|
2021-08-29 16:16:49 +02:00
|
|
|
// AnySlug returns a where condition that matches any slug in search.
|
|
|
|
func AnySlug(col, search, sep string) (where string) {
|
|
|
|
if search == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if sep == "" {
|
|
|
|
sep = " "
|
|
|
|
}
|
|
|
|
|
|
|
|
var wheres []string
|
|
|
|
var words []string
|
|
|
|
|
|
|
|
for _, w := range strings.Split(search, sep) {
|
|
|
|
w = strings.TrimSpace(w)
|
|
|
|
|
|
|
|
words = append(words, slug.Make(w))
|
|
|
|
|
|
|
|
if !txt.ContainsASCIILetters(w) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
singular := inflection.Singular(w)
|
|
|
|
|
|
|
|
if singular != w {
|
|
|
|
words = append(words, slug.Make(singular))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(words) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range words {
|
|
|
|
wheres = append(wheres, fmt.Sprintf("%s = '%s'", col, w))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(wheres, " OR ")
|
|
|
|
}
|