diff --git a/database/database.go b/database/database.go index f75346b..04f9755 100644 --- a/database/database.go +++ b/database/database.go @@ -95,6 +95,11 @@ func SearchWordsByScore(db *sql.DB, words []string) []types.PageData { return searchWords(db, words, true) } +func SearchWordsBySite(db *sql.DB, words []string, domain string) []types.PageData { + // search words by site is same as search words by score, but adds a domain condition + return searchWords(db, words, true, domain) +} + func SearchWordsByCount(db *sql.DB, words []string) []types.PageData { return searchWords(db, words, false) } @@ -190,7 +195,7 @@ func countQuery(db *sql.DB, table string) int { return count } -func searchWords(db *sql.DB, words []string, searchByScore bool) []types.PageData { +func searchWords(db *sql.DB, words []string, searchByScore bool, domain ...string) []types.PageData { var wordlist []string var args []interface{} for _, word := range words { @@ -198,6 +203,16 @@ func searchWords(db *sql.DB, words []string, searchByScore bool) []types.PageDat args = append(args, strings.ToLower(word)) } + // the domains conditional defaults to just 'true' i.e. no domain condition + domains := []string{"1"} + if len(domain) > 0 && domain[0] != "" { + domains = make([]string, 0) // we've got at least one domain! clear domains default + for _, d := range domain { + domains = append(domains, "domain = ?") + args = append(args, d) + } + } + orderType := "SUM(score)" if !searchByScore { orderType = "COUNT(*)" @@ -206,12 +221,13 @@ func searchWords(db *sql.DB, words []string, searchByScore bool) []types.PageDat query := fmt.Sprintf(` SELECT p.url, p.about, p.title FROM inv_index inv INNER JOIN pages p ON inv.url = p.url - WHERE %s + WHERE (%s) + AND (%s) GROUP BY inv.url ORDER BY %s DESC LIMIT 15 - `, strings.Join(wordlist, " OR "), orderType) + `, strings.Join(wordlist, " OR "), strings.Join(domains, " OR "), orderType) stmt, err := db.Prepare(query) util.Check(err) diff --git a/html/search.html b/html/search.html index bc1f594..71268fd 100644 --- a/html/search.html +++ b/html/search.html @@ -1,11 +1,14 @@ {{ template "head" . }} {{ template "nav" . }}
-

{{ .Data.Title }}

+

{{ .Data.Title }} {{ if ne .Data.Site "" }} for {{ .Data.Site }} {{ end }}