-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
77 lines (61 loc) · 1.84 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package miru
import (
"fmt"
"html/template"
"strings"
"time"
rdb "github.com/dancannon/gorethink"
)
// Result holds data for a result's document and index
type Result struct {
Document
Index
}
// Results holds all of the results, the time taken to perform the query and the
// number of results.
type Results struct {
Speed float64 `json:"speed"`
Count int64 `json:"count"`
Results []Result `json:"results"`
}
// RenderSpeed formats the speed of the query
func (rxs *Results) RenderSpeed() string {
return fmt.Sprintf("%.4f seconds", rxs.Speed)
}
// RenderSpeedHTML formats the speed of the query and escapes for use in templates.
func (rxs *Results) RenderSpeedHTML() template.HTML {
return template.HTML(rxs.RenderSpeed())
}
// RenderCount formats the number of results
func (rxs *Results) RenderCount() string {
return fmt.Sprintf("Showing %d of %d results.", len(rxs.Results), rxs.Count)
}
// RenderCountHTML formats the number of results and escapes for use in templates.
func (rxs *Results) RenderCountHTML() template.HTML {
return template.HTML(rxs.RenderCount())
}
// Search returns a list of Results for a given query.
func (rxs *Results) Search(query string, c *Context) error {
start := time.Now()
query = Normalise(query)
keywords := rxs.ParseQuery(query)
results, err := rdb.Db(
c.Config.Database.Name).Table(
c.Config.Tables.Index).GetAllByIndex(
"word", rdb.Args(keywords)).EqJoin(
"doc_id", rdb.Db(c.Config.Database.Name).Table(
c.Config.Tables.Document)).Zip().OrderBy(
rdb.Desc("count")).Run(c.Db)
if err != nil {
return err
}
results.All(&rxs.Results)
t := time.Since(start).Seconds()
rxs.Speed = t
rxs.Count = int64(len(rxs.Results))
return nil
}
// ParseQuery splits words into a list of individual words.
func (rxs *Results) ParseQuery(query string) []string {
return strings.Split(query, " ")
}