Search: Use | as OR separator
This commit is contained in:
parent
387452858e
commit
0999eca362
7 changed files with 22 additions and 21 deletions
|
@ -204,7 +204,7 @@ func (m *Moments) Start() (err error) {
|
|||
} else {
|
||||
w := txt.Words(f.Label)
|
||||
w = append(w, mom.Label)
|
||||
f.Label = strings.Join(txt.UniqueWords(w), ",")
|
||||
f.Label = strings.Join(txt.UniqueWords(w), query.OrSep)
|
||||
}
|
||||
|
||||
if err := a.Update("AlbumFilter", f.Serialize()); err != nil {
|
||||
|
|
|
@ -117,7 +117,7 @@ func AlbumSearch(f form.AlbumSearch) (results AlbumResults, err error) {
|
|||
Where("albums.deleted_at IS NULL")
|
||||
|
||||
if f.ID != "" {
|
||||
s = s.Where("albums.album_uid IN (?)", strings.Split(f.ID, ","))
|
||||
s = s.Where("albums.album_uid IN (?)", strings.Split(f.ID, OrSep))
|
||||
|
||||
if result := s.Scan(&results); result.Error != nil {
|
||||
return results, result.Error
|
||||
|
@ -132,15 +132,15 @@ func AlbumSearch(f form.AlbumSearch) (results AlbumResults, err error) {
|
|||
}
|
||||
|
||||
if f.Type != "" {
|
||||
s = s.Where("albums.album_type IN (?)", strings.Split(f.Type, ","))
|
||||
s = s.Where("albums.album_type IN (?)", strings.Split(f.Type, OrSep))
|
||||
}
|
||||
|
||||
if f.Category != "" {
|
||||
s = s.Where("albums.album_category IN (?)", strings.Split(f.Category, ","))
|
||||
s = s.Where("albums.album_category IN (?)", strings.Split(f.Category, OrSep))
|
||||
}
|
||||
|
||||
if f.Location != "" {
|
||||
s = s.Where("albums.album_location IN (?)", strings.Split(f.Location, ","))
|
||||
s = s.Where("albums.album_location IN (?)", strings.Split(f.Location, OrSep))
|
||||
}
|
||||
|
||||
if f.Favorite {
|
||||
|
|
|
@ -98,7 +98,7 @@ func Geo(f form.GeoSearch) (results GeoResults, err error) {
|
|||
}
|
||||
|
||||
if f.Color != "" {
|
||||
s = s.Where("files.file_main_color IN (?)", strings.Split(strings.ToLower(f.Color), ","))
|
||||
s = s.Where("files.file_main_color IN (?)", strings.Split(strings.ToLower(f.Color), OrSep))
|
||||
}
|
||||
|
||||
if f.Favorite {
|
||||
|
@ -106,12 +106,12 @@ func Geo(f form.GeoSearch) (results GeoResults, err error) {
|
|||
}
|
||||
|
||||
if f.Country != "" {
|
||||
s = s.Where("photos.photo_country IN (?)", strings.Split(strings.ToLower(f.Country), ","))
|
||||
s = s.Where("photos.photo_country IN (?)", strings.Split(strings.ToLower(f.Country), OrSep))
|
||||
}
|
||||
|
||||
// Filter by media type.
|
||||
if f.Type != "" {
|
||||
s = s.Where("photos.photo_type IN (?)", strings.Split(strings.ToLower(f.Type), ","))
|
||||
s = s.Where("photos.photo_type IN (?)", strings.Split(strings.ToLower(f.Type), OrSep))
|
||||
}
|
||||
|
||||
if f.Video {
|
||||
|
@ -129,8 +129,8 @@ func Geo(f form.GeoSearch) (results GeoResults, err error) {
|
|||
|
||||
if strings.HasSuffix(p, "/") {
|
||||
s = s.Where("photos.photo_path = ?", p[:len(p)-1])
|
||||
} else if strings.Contains(p, ",") {
|
||||
s = s.Where("photos.photo_path IN (?)", strings.Split(p, ","))
|
||||
} else if strings.Contains(p, OrSep) {
|
||||
s = s.Where("photos.photo_path IN (?)", strings.Split(p, OrSep))
|
||||
} else {
|
||||
s = s.Where("photos.photo_path LIKE ?", strings.ReplaceAll(p, "*", "%"))
|
||||
}
|
||||
|
|
|
@ -56,7 +56,7 @@ func PhotoSearch(f form.PhotoSearch) (results PhotoResults, count int, err error
|
|||
|
||||
// Shortcut for known photo ids.
|
||||
if f.ID != "" {
|
||||
s = s.Where("photos.photo_uid IN (?)", strings.Split(f.ID, ","))
|
||||
s = s.Where("photos.photo_uid IN (?)", strings.Split(f.ID, OrSep))
|
||||
s = s.Order("files.file_primary DESC")
|
||||
|
||||
if result := s.Scan(&results); result.Error != nil {
|
||||
|
@ -78,7 +78,7 @@ func PhotoSearch(f form.PhotoSearch) (results PhotoResults, count int, err error
|
|||
var labelIds []uint
|
||||
|
||||
if f.Label != "" {
|
||||
if err := Db().Where(AnySlug("label_slug", f.Label, ",")).Or(AnySlug("custom_slug", f.Label, ",")).Find(&labels).Error; len(labels) == 0 || err != nil {
|
||||
if err := Db().Where(AnySlug("label_slug", f.Label, OrSep)).Or(AnySlug("custom_slug", f.Label, OrSep)).Find(&labels).Error; len(labels) == 0 || err != nil {
|
||||
log.Errorf("search: labels %s not found", txt.Quote(f.Label))
|
||||
return results, 0, fmt.Errorf("%s not found", txt.Quote(f.Label))
|
||||
} else {
|
||||
|
@ -183,7 +183,7 @@ func PhotoSearch(f form.PhotoSearch) (results PhotoResults, count int, err error
|
|||
}
|
||||
|
||||
if f.Color != "" {
|
||||
s = s.Where("files.file_main_color IN (?)", strings.Split(strings.ToLower(f.Color), ","))
|
||||
s = s.Where("files.file_main_color IN (?)", strings.Split(strings.ToLower(f.Color), OrSep))
|
||||
}
|
||||
|
||||
if f.Favorite {
|
||||
|
@ -203,21 +203,21 @@ func PhotoSearch(f form.PhotoSearch) (results PhotoResults, count int, err error
|
|||
}
|
||||
|
||||
if f.Country != "" {
|
||||
s = s.Where("photos.photo_country IN (?)", strings.Split(strings.ToLower(f.Country), ","))
|
||||
s = s.Where("photos.photo_country IN (?)", strings.Split(strings.ToLower(f.Country), OrSep))
|
||||
}
|
||||
|
||||
if f.State != "" {
|
||||
s = s.Where("places.place_state IN (?)", strings.Split(f.State, ","))
|
||||
s = s.Where("places.place_state IN (?)", strings.Split(f.State, OrSep))
|
||||
}
|
||||
|
||||
if f.Category != "" {
|
||||
s = s.Joins("JOIN cells ON photos.cell_id = cells.id").
|
||||
Where("cells.cell_category IN (?)", strings.Split(strings.ToLower(f.Category), ","))
|
||||
Where("cells.cell_category IN (?)", strings.Split(strings.ToLower(f.Category), OrSep))
|
||||
}
|
||||
|
||||
// Filter by media type.
|
||||
if f.Type != "" {
|
||||
s = s.Where("photos.photo_type IN (?)", strings.Split(strings.ToLower(f.Type), ","))
|
||||
s = s.Where("photos.photo_type IN (?)", strings.Split(strings.ToLower(f.Type), OrSep))
|
||||
}
|
||||
|
||||
if f.Video {
|
||||
|
@ -257,7 +257,7 @@ func PhotoSearch(f form.PhotoSearch) (results PhotoResults, count int, err error
|
|||
}
|
||||
|
||||
if f.Hash != "" {
|
||||
s = s.Where("files.file_hash IN (?)", strings.Split(strings.ToLower(f.Hash), ","))
|
||||
s = s.Where("files.file_hash IN (?)", strings.Split(strings.ToLower(f.Hash), OrSep))
|
||||
}
|
||||
|
||||
if f.Portrait {
|
||||
|
|
|
@ -546,7 +546,7 @@ func TestPhotoSearch(t *testing.T) {
|
|||
|
||||
t.Run("search for labels", func(t *testing.T) {
|
||||
var f form.PhotoSearch
|
||||
f.Label = "botanical-garden,nature,landscape,park"
|
||||
f.Label = "botanical-garden|nature|landscape|park"
|
||||
|
||||
photos, _, err := PhotoSearch(f)
|
||||
|
||||
|
@ -620,7 +620,7 @@ func TestPhotoSearch(t *testing.T) {
|
|||
f.Video = true
|
||||
f.Name = "xxx"
|
||||
f.Original = "xxyy"
|
||||
f.Path = "/xxx,xxx"
|
||||
f.Path = "/xxx|xxx"
|
||||
f.Type = "mp4"
|
||||
f.Stack = true
|
||||
f.Unsorted = true
|
||||
|
|
|
@ -49,6 +49,7 @@ var log = event.Log
|
|||
const (
|
||||
MySQL = "mysql"
|
||||
SQLite = "sqlite3"
|
||||
OrSep = "|"
|
||||
)
|
||||
|
||||
// Max result limit for queries.
|
||||
|
|
|
@ -89,7 +89,7 @@ func TestAnySlug(t *testing.T) {
|
|||
})
|
||||
|
||||
t.Run("comma separated", func(t *testing.T) {
|
||||
where := AnySlug("custom_slug", "botanical-garden,landscape,bay", ",")
|
||||
where := AnySlug("custom_slug", "botanical-garden|landscape|bay", OrSep)
|
||||
assert.Equal(t, "custom_slug = 'botanical-garden' OR custom_slug = 'landscape' OR custom_slug = 'bay'", where)
|
||||
})
|
||||
|
||||
|
|
Loading…
Reference in a new issue