Add parameter to control the amount of highlighted output.

This commit is contained in:
Sergio Martínez Portela 2025-02-23 23:33:38 +01:00
parent 842a5e712b
commit e35fafb18e
3 changed files with 49 additions and 15 deletions

View File

@ -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

View File

@ -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, '<span class=\"match\">', '</span>'), top_level_title, is_done, is_todo, snippet(note_search, 2, '<span class=\"match\">', '</span>', '<span class=\"search-result-break\"></span>', ?) FROM note_search(?)")
} else if (body_type == "all") {
stm, err = db.Prepare("SELECT note_id, highlight(note_search, 1, '<span class=\"match\">', '</span>'), top_level_title, is_done, is_todo, highlight(note_search, 2, '<span class=\"match\">', '</span>') FROM note_search(?)")
} else if (body_type == "none") {
stm, err = db.Prepare("SELECT note_id, highlight(note_search, 1, '<span class=\"match\">', '</span>'), top_level_title, is_done, is_todo FROM note_search(?)")
}
stm, err := db.Prepare("SELECT note_id, highlight(note_search, 1, '<span class=\"match\">', '</span>'), top_level_title, is_done, is_todo, snippet(note_search, 2, '<span class=\"match\">', '</span>', '<span class=\"search-result-break\"></span>', ?) 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,6 +129,10 @@ func main() {
var note_top_level_title string
var note_is_done string
var note_is_todo string
item := make(map[string]string)
if (body_type != "none") {
var note_highlight string
err = rows.Scan(
@ -119,6 +143,18 @@ func main() {
&note_is_todo,
&note_highlight,
)
if (body_type != "none") {
item["highlight"] = note_highlight
}
} else {
err = rows.Scan(
&note_id,
&note_title,
&note_top_level_title,
&note_is_done,
&note_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)
}

View File

@ -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