From eca8b9f99d948bc180517b2ebc7fdac481bb8ab3 Mon Sep 17 00:00:00 2001
From: vednoc
Date: Mon, 21 Aug 2023 05:43:24 +0200
Subject: [PATCH 04/14] fix(search): remove substring functionality
As it turns out, it caused more weirdness than I imagined. Therefore,
let's see where things go without substring search. If that doesn't
improve the status quo either, we'll go back to the drawing board.
To simulate substring search, you can use a prefix with an asterisk.
For example, looking up all Git forges can be done with `git*`, which
will return all userstyles that contain a word starting with "git".
However, using `-` still has the same behavior. To work around it, wrap
it with quotes, like `"dark-"`, which will return all userstyles that
contain "dark-" literally.
Both of these can be used to target specific columns: name, description,
notes, categories (added in a commit from the future), as well as other
columns we'll add later on (if we're happy with this approach).
Fix #233
---
handlers/core/search.go | 4 ----
models/search.go | 2 +-
2 files changed, 1 insertion(+), 5 deletions(-)
diff --git a/handlers/core/search.go b/handlers/core/search.go
index e7a458e4..b51fe5c8 100644
--- a/handlers/core/search.go
+++ b/handlers/core/search.go
@@ -19,10 +19,6 @@ func Search(c *fiber.Ctx) error {
c.Locals("Canonical", "search")
keyword := strings.TrimSpace(c.Query("q"))
- if len(keyword) < 3 {
- c.Locals("Error", "Keywords need to be in a group of three or more characters.")
- return c.Render("core/search", fiber.Map{})
- }
c.Locals("Keyword", keyword)
page, err := models.IsValidPage(c.Query("page"))
diff --git a/models/search.go b/models/search.go
index 1392b8a8..57989e1f 100644
--- a/models/search.go
+++ b/models/search.go
@@ -5,7 +5,7 @@ import "userstyles.world/modules/database"
func InitStyleSearch() error {
init := `
DROP TABLE IF EXISTS fts_styles;
-CREATE VIRTUAL TABLE fts_styles USING FTS5(id, name, description, notes, tokenize="trigram");
+CREATE VIRTUAL TABLE fts_styles USING FTS5(id, name, description, notes);
INSERT INTO fts_styles(id, name, description, notes) SELECT id, name, description, notes FROM styles;
DROP TRIGGER IF EXISTS fts_styles_insert;
From f9792732dbc53fe51e7354e63645bf46a9c3b09b Mon Sep 17 00:00:00 2001
From: vednoc
Date: Wed, 20 Sep 2023 20:49:41 +0200
Subject: [PATCH 05/14] feat(search): add a new "category" filter
Ref #238
Fix #87, #127, #128, #230
---
handlers/core/search.go | 13 +++++++++++--
models/search.go | 17 ++++++++++++-----
web/views/core/search.tmpl | 11 +++++++++--
3 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/handlers/core/search.go b/handlers/core/search.go
index b51fe5c8..4d3c05c0 100644
--- a/handlers/core/search.go
+++ b/handlers/core/search.go
@@ -1,6 +1,7 @@
package core
import (
+ "fmt"
"strings"
"time"
@@ -21,6 +22,14 @@ func Search(c *fiber.Ctx) error {
keyword := strings.TrimSpace(c.Query("q"))
c.Locals("Keyword", keyword)
+ category := strings.TrimSpace(c.Query("category"))
+ c.Locals("Category", category)
+
+ query := keyword
+ if category != "" {
+ query += fmt.Sprintf(" category:%s", category)
+ }
+
page, err := models.IsValidPage(c.Query("page"))
if err != nil || page < 1 {
c.Locals("Title", "Invalid page size")
@@ -32,7 +41,7 @@ func Search(c *fiber.Ctx) error {
t := time.Now()
- total, err := storage.TotalSearchStyles(keyword, sort)
+ total, err := storage.TotalSearchStyles(query, sort)
if err != nil {
log.Database.Println(err)
c.Locals("Title", "Failed to count userstyles")
@@ -46,7 +55,7 @@ func Search(c *fiber.Ctx) error {
}
c.Locals("Pagination", p)
- s, err := storage.FindSearchStyles(keyword, p.SortStyles(), page)
+ s, err := storage.FindSearchStyles(query, p.SortStyles(), page)
if err != nil {
log.Database.Println(err)
c.Locals("Title", "Failed to search for userstyles")
diff --git a/models/search.go b/models/search.go
index 57989e1f..8f51182a 100644
--- a/models/search.go
+++ b/models/search.go
@@ -5,21 +5,28 @@ import "userstyles.world/modules/database"
func InitStyleSearch() error {
init := `
DROP TABLE IF EXISTS fts_styles;
-CREATE VIRTUAL TABLE fts_styles USING FTS5(id, name, description, notes);
-INSERT INTO fts_styles(id, name, description, notes) SELECT id, name, description, notes FROM styles;
+CREATE VIRTUAL TABLE fts_styles USING FTS5(id, name, description, notes, category);
+
+INSERT INTO fts_styles(id, name, description, notes, category)
+SELECT id, name, description, notes, category
+FROM styles;
DROP TRIGGER IF EXISTS fts_styles_insert;
CREATE TRIGGER fts_styles_insert AFTER INSERT ON styles
BEGIN
- INSERT INTO fts_styles(id, name, description, notes)
- VALUES (new.id, new.name, new.description, new.notes);
+ INSERT INTO fts_styles(id, name, description, notes, category)
+ VALUES (new.id, new.name, new.description, new.notes, new.category);
END;
DROP TRIGGER IF EXISTS fts_styles_update;
CREATE TRIGGER fts_styles_update AFTER UPDATE ON styles
BEGIN
UPDATE fts_styles
- SET name = new.name, description = new.description, notes = new.notes
+ SET
+ name = new.name,
+ description = new.description,
+ notes = new.notes,
+ category = new.category
WHERE id = old.id;
END;
diff --git a/web/views/core/search.tmpl b/web/views/core/search.tmpl
index 262125d8..36c884ea 100644
--- a/web/views/core/search.tmpl
+++ b/web/views/core/search.tmpl
@@ -7,13 +7,20 @@