// Search over a help center's articles, with highlighted snippets.
//
//   quillo run search.qwe      then open http://localhost:8080/search?q=refund

use "search.qwer"

let INDEX = "data/search-index"

let articles = [
    { id: "returns", title: "Returns and refunds", body: "You can return any item within 30 days. Refunds go back to the card you paid with, in 5 to 10 days." },
    { id: "shipping", title: "Shipping times", body: "Orders ship in 2 days. Express shipping arrives the next day in most cities." },
    { id: "account", title: "Changing your password", body: "Open Settings, then Security, and choose a new password. A refund of a gift card is not possible." },
]

route GET /reindex = { added: search_add(INDEX, articles)?, count: search_count(INDEX)? }

route GET /search ? { q: str } {
    if search_count(INDEX)? == 0 { search_add(INDEX, articles)? }
    search_query(INDEX, q, 10)?
}

route GET /delete ? { id: str } {
    search_delete(INDEX, id)?
    { count: search_count(INDEX)? }
}
