From e35fafb18e2894a97489e73f28cafcf2729661cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Sun, 23 Feb 2025 23:33:38 +0100 Subject: [PATCH] Add parameter to control the amount of highlighted output. --- scripts/search-server.sh | 2 +- scripts/search-server/server.go | 60 ++++++++++++++++++++++++++------- static/search-box.js | 2 +- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/scripts/search-server.sh b/scripts/search-server.sh index 90ff02c..62abda0 100644 --- a/scripts/search-server.sh +++ b/scripts/search-server.sh @@ -12,4 +12,4 @@ cd ../../_gen/notes/ set -x -exec docker run -it --rm -p $PORT:80 -e SNIPPET_SIZE=128 -e PORT=80 -e DB_PATH=/db.sqlite3 -v `pwd`/db.sqlite3:/db.sqlite3:ro search-server +exec docker run -it --rm -p $PORT:80 -e SNIPPET_SIZE=256 -e PORT=80 -e DB_PATH=/db.sqlite3 -v `pwd`/db.sqlite3:/db.sqlite3:ro search-server diff --git a/scripts/search-server/server.go b/scripts/search-server/server.go index a2275d4..be6ab0d 100644 --- a/scripts/search-server/server.go +++ b/scripts/search-server/server.go @@ -79,8 +79,23 @@ func main() { c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, OPTIONS") query := c.Query("q") + body_type := c.Query("body") + + if ((body_type != "all") && (body_type != "none") && (body_type != "snippet")) { + body_type = "none" + } + + var stm *sql.Stmt + var err error + + if (body_type == "snippet") { + stm, err = db.Prepare("SELECT note_id, highlight(note_search, 1, '', ''), top_level_title, is_done, is_todo, snippet(note_search, 2, '', '', '', ?) FROM note_search(?)") + } else if (body_type == "all") { + stm, err = db.Prepare("SELECT note_id, highlight(note_search, 1, '', ''), top_level_title, is_done, is_todo, highlight(note_search, 2, '', '') FROM note_search(?)") + } else if (body_type == "none") { + stm, err = db.Prepare("SELECT note_id, highlight(note_search, 1, '', ''), top_level_title, is_done, is_todo FROM note_search(?)") + } - stm, err := db.Prepare("SELECT note_id, highlight(note_search, 1, '', ''), top_level_title, is_done, is_todo, snippet(note_search, 2, '', '', '', ?) FROM note_search(?)") if err != nil { log.Fatal(err) @@ -92,8 +107,13 @@ func main() { } results := make([]map[string]string, 0) + var rows *sql.Rows - rows, err := stm.Query(snippet_size, query) + if (body_type == "snippet") { + rows, err = stm.Query(snippet_size, query) + } else { + rows, err = stm.Query(query) + } if err != nil { log.Fatal(err) c.JSON(500, gin.H{ @@ -109,16 +129,32 @@ func main() { var note_top_level_title string var note_is_done string var note_is_todo string - var note_highlight string - err = rows.Scan( - ¬e_id, - ¬e_title, - ¬e_top_level_title, - ¬e_is_done, - ¬e_is_todo, - ¬e_highlight, - ) + item := make(map[string]string) + + if (body_type != "none") { + var note_highlight string + + err = rows.Scan( + ¬e_id, + ¬e_title, + ¬e_top_level_title, + ¬e_is_done, + ¬e_is_todo, + ¬e_highlight, + ) + if (body_type != "none") { + item["highlight"] = note_highlight + } + } else { + err = rows.Scan( + ¬e_id, + ¬e_title, + ¬e_top_level_title, + ¬e_is_done, + ¬e_is_todo, + ) + } if err != nil { log.Fatal(err) c.JSON(500, gin.H{ @@ -128,13 +164,11 @@ func main() { return } - item := make(map[string]string) item["id"] = note_id item["title"] = note_title item["top_level_title"] = note_top_level_title item["is_done"] = note_is_done item["is_todo"] = note_is_todo - item["highlight"] = note_highlight results = append(results, item) } diff --git a/static/search-box.js b/static/search-box.js index 6fa89da..08b81f0 100644 --- a/static/search-box.js +++ b/static/search-box.js @@ -74,7 +74,7 @@ function _codigoparallevar_enable_search_box(selector, options) { lastVal = val; resultsBox.classList.add('loading'); - const uri = SEARCH_ENDPOINT + '?q=' + encodeURIComponent(val); + const uri = SEARCH_ENDPOINT + '?q=' + encodeURIComponent(val) + '&body=snippet'; let query = fetch(uri); currentQuery = query; query