Improved search functionality.
h3rald h3rald@h3rald.com
Sat, 11 Apr 2015 15:24:29 +0200
6 files changed,
24 insertions(+),
16 deletions(-)
M
admin/js/app.js
→
admin/js/app.js
@@ -4,17 +4,19 @@ var app = window.LS || (window.LS = {});
app.flash = m.prop(); app.system = {}; + m.route.mode = "hash"; + app.init = function(info){ app.system = info; - m.route.mode = "hash"; - m.route(document.body, "/info", { - '/info': app.info, + "/info": app.info, "/tags/:id": app.tags, "/document/:action/:id...": app.document, "/guide/:id": app.guide, "/new": app.create, - "/search": app.search + "/search/:q": app.search, + "/search/:q/:page": app.search, + "/search/:q/:page/:limit": app.search }); }; Info.get().then(app.init);
M
admin/js/modules/search.js
→
admin/js/modules/search.js
@@ -8,8 +8,10 @@ app.search = {vm: {}};
app.search.vm.init = function(){ var vm = this; vm.query = m.route.param("q"); - vm.offset = m.route.param("offset") || 0; vm.limit = m.route.param("limit") || 10; + vm.page = m.route.param("page") || 1; + vm.page -= 1; // pages are 0-based + vm.offset = vm.page * vm.limit; vm.result = m.prop({total: 0, results: []}); vm.total = 0; Doc.search(vm.query, vm.offset, vm.limit).then(function(result){@@ -47,4 +49,4 @@ };
u.layout(app.search); -}()) +}())
M
admin/js/utils.js
→
admin/js/utils.js
@@ -116,7 +116,7 @@ var offset = obj.limit * n;
sign = sign || n+1; return m("li", {class: klass}, [m("a", { - href: "/search?q="+obj.query+"&offset="+offset+"&limit="+obj.limit, + href: "/search/"+obj.query+"/"+(n+1), // assuming 10 elements per page //+"/"+obj.limit, config: m.route }, [m.trust(sign)] )]
M
lib/utils.nim
→
lib/utils.nim
@@ -1,6 +1,9 @@
import json, db_sqlite, strutils, pegs, asyncdispatch, asynchttpserver2, times, logging, math, sqlite3 import types, queries, contenttypes +proc dbg*(args: varargs[string, `$`]) = + echo "DEBUG - "&args.join(" ") + proc dbQuote*(s: string): string = result = "'" for c in items(s):@@ -12,12 +15,12 @@ proc currentTime*(): string =
return getTime().getGMTime().format("yyyy-MM-dd'T'hh:mm:ss'Z'") proc selectDocumentsByTags(tags: string): string = - var select_tagged = "SELECT document_id FROM tags WHERE tag_id = \"" + var select_tagged = "SELECT document_id FROM tags WHERE tag_id = '" result = "" for tag in tags.split(','): if not tag.match(PEG_TAG): raise newException(EInvalidTag, "Invalid tag '$1'" % tag) - result = result & "AND id IN (" & select_tagged & tag & "\") " + result = result & "AND id IN (" & select_tagged & tag & "') " proc prepareSelectDocumentsQuery*(options: var QueryOptions): string = result = "SELECT "@@ -37,14 +40,14 @@ result = result & "AND id = ?"
if options.tags.len > 0: result = result & options.tags.selectDocumentsByTags() if options.search.len > 0: - result = result & "AND searchcontents MATCH \"" & options.search & "\" " + result = result & "AND searchcontents MATCH '" & options.search.replace("'", "''") & "' " if options.orderby.len > 0 and options.select[0] != "COUNT(id)": result = result & "ORDER BY " & options.orderby & " " if options.limit > 0: result = result & "LIMIT " & $options.limit & " " if options.offset > 0: result = result & "OFFSET " & $options.offset & " " - debug(result) + dbg(result) proc prepareSelectTagsQuery*(options: QueryOptions): string = result = "SELECT tag_id, COUNT(document_ID) "@@ -56,7 +59,7 @@ if options.orderby.len > 0:
result = result & "ORDER BY " & options.orderby&" " if options.limit > 0: result = result & "LIMIT " & $options.limit & " " - debug(result) + dbg(result) proc prepareJsonDocument*(store:Datastore, doc: TRow, cols:seq[string]): JsonNode = var raw_tags = store.db.getAllRows(SQL_SELECT_DOCUMENT_TAGS, doc[0])@@ -111,7 +114,7 @@
proc resError*(code: HttpCode, message: string, trace = ""): Response = warn(message) if trace.len > 0: - debug(trace) + dbg(trace) result.code = code result.content = """{"error":"$1"}""" % message result.headers = ctJsonHeader()