forked from gobuffalo/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
117 lines (100 loc) · 2.28 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package actions
import (
"fmt"
"log"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"github.com/blevesearch/bleve"
"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/packr"
strip "github.com/grokify/html-strip-tags-go"
"github.com/pkg/errors"
)
const indexName = "gobuffalo.search"
var index bleve.Index
func init() {
os.RemoveAll(indexName)
var err error
index, err = bleve.Open(indexName)
if err == bleve.ErrorIndexPathDoesNotExist {
mapping := bleve.NewIndexMapping()
index, err = bleve.New(indexName, mapping)
if err != nil {
log.Fatalf("could not create bleve index: %s\n", err)
}
}
}
func indexDocs(app *buffalo.App) {
hl := r.HTMLLayout
// set a blank layout until we finish indexing
r.HTMLLayout = ""
defer func() { r.HTMLLayout = hl }()
box := r.TemplatesBox
err := box.Walk(func(path string, file packr.File) error {
fi, err := file.FileInfo()
if err != nil {
return errors.WithStack(err)
}
if fi.IsDir() {
return nil
}
if !strings.HasPrefix(path, "docs/") {
return nil
}
n := filepath.Base(path)
if strings.HasPrefix(n, "_") {
return nil
}
u := "/en/" + path
ext := filepath.Ext(u)
for ext != "" {
u = strings.TrimSuffix(u, ext)
ext = filepath.Ext(u)
}
req := httptest.NewRequest("GET", u, nil)
req.Header.Set("X-Forwarded-Proto", "https")
res := httptest.NewRecorder()
app.ServeHTTP(res, req)
if res.Code != 200 {
fmt.Printf("could not index %s\n", u)
fmt.Println(res.Body.String())
return nil
}
body := strip.StripTags(res.Body.String())
for strings.Index(body, " ") > 0 || strings.Index(body, "\n\n") > 0 {
r := strings.NewReplacer(" ", " ", "\n", " ", "\t", " ")
body = r.Replace(body)
}
doc := struct {
URL string
Body string
}{
URL: u,
Body: body,
}
err = index.Index(doc.URL, doc)
if err != nil {
fmt.Println(err)
}
return nil
})
if err != nil {
fmt.Println(err)
}
}
func Search(c buffalo.Context) error {
if c.Param("query") != "" {
query := bleve.NewQueryStringQuery(c.Param("query"))
req := bleve.NewSearchRequest(query)
req.Size = 100
req.Highlight = bleve.NewHighlight()
res, err := index.Search(req)
if err != nil {
return errors.WithStack(err)
}
c.Set("results", res)
}
return c.Render(200, r.HTML("search.html"))
}