-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
60 lines (56 loc) · 1.46 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
package handlers
import (
"fmt"
"html/template"
"log"
"net/http"
"github.com/martini-contrib/render"
"github.com/warren-community/warren/models"
)
func (h *Handlers) Search(w http.ResponseWriter, r *http.Request, render render.Render, l *log.Logger) {
q := r.FormValue("q")
if q == "" {
render.HTML(http.StatusOK, "search/displaySearch", map[string]interface{}{
"CSRF": h.session.Values["_csrf_token"],
"Title": "Search",
"User": h.user,
"Flashes": h.flashes(r, w),
})
return
}
searchJson := fmt.Sprintf(`{
"query": {
"simple_query_string": {
"query": "%s",
"analyzer": "snowball",
"fields": ["IndexedContent", "Title", "Tags"],
"default_operator": "and"
}
}
}`, template.JSEscapeString(q))
res, err := h.esConn.Search("warren", "entity", nil, searchJson)
if err != nil {
l.Printf("Error conducting search: %+v\n", err)
h.InternalServerError(w, r, render)
return
}
var hits []models.Entity
for _, hit := range res.Hits.Hits {
entity, err := models.GetEntity(hit.Id, h.db)
if err != nil {
l.Printf("Error fetching entity: %+v\n", err)
h.InternalServerError(w, r, render)
return
}
hits = append(hits, entity)
}
render.HTML(http.StatusOK, "search/searchResults", map[string]interface{}{
"CSRF": h.session.Values["_csrf_token"],
"Title": fmt.Sprintf("Search results for: %s", q),
"User": h.user,
"Flashes": h.flashes(r, w),
"Res": res,
"Hits": hits,
"Q": q,
})
}