2021-09-18 15:32:39 +02:00
|
|
|
package search
|
2021-08-29 16:16:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-12-15 12:24:05 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/sanitize"
|
2021-08-29 16:16:49 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
|
|
|
|
|
|
"github.com/jinzhu/inflection"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-17 16:07:31 +01:00
|
|
|
s = txt.StripOr(sanitize.SearchQuery(s))
|
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-09-18 15:32:39 +02:00
|
|
|
for _, k := range strings.Split(s, txt.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 {
|
2022-03-24 18:30:59 +01:00
|
|
|
words = txt.UniqueWords(strings.Fields(k))
|
2021-08-29 19:19:54 +02:00
|
|
|
}
|
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 {
|
2022-03-24 18:30:59 +01:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s%%'", col, SqlLike(w)))
|
2021-08-29 16:16:49 +02:00
|
|
|
} else {
|
2022-03-24 18:30:59 +01:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s'", col, SqlLike(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 {
|
2022-03-24 18:30:59 +01:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s'", col, SqlLike(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 {
|
2022-03-24 18:30:59 +01:00
|
|
|
words = txt.UniqueWords(strings.Fields(s))
|
2021-08-29 19:19:54 +02:00
|
|
|
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 {
|
2022-03-24 18:30:59 +01:00
|
|
|
wheres = append(wheres, fmt.Sprintf("%s LIKE '%s%%'", col, SqlLike(w)))
|
2021-08-29 16:16:49 +02:00
|
|
|
} else {
|
2022-03-24 18:30:59 +01:00
|
|
|
wheres = append(wheres, fmt.Sprintf("%s LIKE '%s'", col, SqlLike(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) {
|
2021-10-09 11:35:36 +02:00
|
|
|
if len(cols) == 0 || len(s) < 1 {
|
2021-08-30 11:26:57 +02:00
|
|
|
return wheres
|
|
|
|
}
|
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
for _, k := range strings.Split(s, txt.And) {
|
2021-09-06 14:16:46 +02:00
|
|
|
var orWheres []string
|
2021-08-30 11:26:57 +02:00
|
|
|
|
2021-09-18 15:32:39 +02:00
|
|
|
for _, w := range strings.Split(k, txt.Or) {
|
2021-09-06 14:16:46 +02:00
|
|
|
w = strings.TrimSpace(w)
|
2021-08-30 11:26:57 +02:00
|
|
|
|
2021-10-09 11:35:36 +02:00
|
|
|
if w == txt.Empty {
|
2021-09-06 14:16:46 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cols {
|
2021-10-09 11:35:36 +02:00
|
|
|
if strings.Contains(w, txt.Space) {
|
2022-03-24 18:30:59 +01:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%s%%'", c, SqlLike(w)))
|
2021-09-06 14:16:46 +02:00
|
|
|
} else {
|
2022-03-24 18:30:59 +01:00
|
|
|
orWheres = append(orWheres, fmt.Sprintf("%s LIKE '%%%s%%'", c, SqlLike(w)))
|
2021-09-06 14:16:46 +02:00
|
|
|
}
|
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)
|
|
|
|
|
2022-03-24 21:46:25 +01:00
|
|
|
words = append(words, txt.Slug(w))
|
2021-08-29 16:16:49 +02:00
|
|
|
|
|
|
|
if !txt.ContainsASCIILetters(w) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
singular := inflection.Singular(w)
|
|
|
|
|
|
|
|
if singular != w {
|
2022-03-24 21:46:25 +01:00
|
|
|
words = append(words, txt.Slug(singular))
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(words) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, w := range words {
|
2022-03-24 18:30:59 +01:00
|
|
|
wheres = append(wheres, fmt.Sprintf("%s = '%s'", col, SqlLike(w)))
|
2021-08-29 16:16:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(wheres, " OR ")
|
|
|
|
}
|
2021-09-20 23:32:35 +02:00
|
|
|
|
|
|
|
// AnyInt returns a where condition that matches any integer within a range.
|
|
|
|
func AnyInt(col, numbers, sep string, min, max int) (where string) {
|
|
|
|
if numbers == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if sep == "" {
|
|
|
|
sep = txt.Or
|
|
|
|
}
|
|
|
|
|
|
|
|
var matches []int
|
|
|
|
var wheres []string
|
|
|
|
|
|
|
|
for _, n := range strings.Split(numbers, sep) {
|
|
|
|
i := txt.Int(n)
|
|
|
|
|
|
|
|
if i == 0 || i < min || i > max {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
matches = append(matches, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(matches) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, n := range matches {
|
|
|
|
wheres = append(wheres, fmt.Sprintf("%s = %d", col, n))
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(wheres, " OR ")
|
|
|
|
}
|
2021-09-29 20:09:34 +02:00
|
|
|
|
|
|
|
// OrLike returns a where condition and values for finding multiple terms combined with OR.
|
|
|
|
func OrLike(col, s string) (where string, values []interface{}) {
|
2022-03-24 18:30:59 +01:00
|
|
|
if txt.IsEmpty(col) || txt.IsEmpty(s) {
|
2021-09-29 20:09:34 +02:00
|
|
|
return "", []interface{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.ReplaceAll(s, "*", "%")
|
|
|
|
s = strings.ReplaceAll(s, "%%", "%")
|
|
|
|
|
|
|
|
terms := strings.Split(s, txt.Or)
|
|
|
|
values = make([]interface{}, len(terms))
|
|
|
|
|
|
|
|
for i := range terms {
|
2022-01-17 16:07:31 +01:00
|
|
|
values[i] = terms[i]
|
2021-09-29 20:09:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
like := fmt.Sprintf("%s LIKE ?", col)
|
|
|
|
where = like + strings.Repeat(" OR "+like, len(terms)-1)
|
|
|
|
|
|
|
|
return where, values
|
|
|
|
}
|