-
Notifications
You must be signed in to change notification settings - Fork 1
/
search.go
120 lines (100 loc) · 2.94 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
118
119
120
// Copyright 2019
// The Honest Scoop and P.J. Siripala
// All rights reserved
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/labstack/echo"
"github.com/patrickmn/go-cache"
)
type searchRef struct {
Name *string `json:"name"`
ID string `json:"id"`
Data string `json:"data"`
SearchTitle *string `json:"search_title"` // add omitempty
SearchSynopsis *string `json:"search_synopsis"` // add omitempty
CreatedAt time.Time `json:"created_at"`
}
func (s *searchRef) MarshalJSON() ([]byte, error) {
out := map[string]interface{}{
"data": s.Data,
"created_at": s.CreatedAt,
"search_title": s.SearchTitle,
"search_synopsis": s.SearchSynopsis,
}
if s.Name == nil {
out["id"] = s.ID
} else {
out["id"] = "@" + *s.Name + "/" + s.ID
}
return json.Marshal(out)
}
// searchHandler provides search functionality. It returns all refs that may contain
// the search terms be in the title or synopsis. It only returns refs that have "searchable"
// set to true.
func searchHandler(c echo.Context) error {
ctx := c.Request().Context()
terms := strings.TrimSpace(c.Param("terms"))
if terms == "" {
return c.JSON(http.StatusOK, map[string]interface{}{"results": []int{}})
}
// Check cache
key := fmt.Sprintf("search-%s", terms)
cachedData, found := memoryCache.Get(key)
if found {
// log.Println("Using cache:" + key)
return c.JSONPretty(http.StatusOK, cachedData, " ")
}
txn := dg.NewReadOnlyTxn()
vars := map[string]string{
"$terms": terms,
}
q := `
query withvar($terms: string) {
results(func: eq(node.searchable, true), orderdesc: node.created_at) @normalize @filter(allofterms(node.search_title, $terms) OR alloftext(node.search_synopsis, $terms)) {
node.owner {
name: user.name
}
id: node.hashid
data: node.xdata
search_title: node.search_title
search_synopsis: node.search_synopsis
created_at: node.created_at
}
}
`
if stdQueryTimeout != 0 {
// Create a max query timeout
_ctx, cancel := context.WithTimeout(ctx, time.Duration(stdQueryTimeout)*time.Millisecond)
defer cancel()
ctx = _ctx
}
resp, err := txn.QueryWithVars(ctx, q, vars)
if err != nil {
if strings.Contains(err.Error(), "context canceled") {
return c.NoContent(http.StatusNoContent)
} else if strings.Contains(err.Error(), "context deadline exceeded") {
return c.NoContent(http.StatusRequestTimeout)
}
log.Println(err)
return c.JSON(http.StatusInternalServerError, ErrorFmt("something went wrong. Try again"))
}
type Root struct {
Results []searchRef `json:"results"`
}
var root Root
err = json.Unmarshal(resp.Json, &root)
if err != nil {
log.Println(err)
return c.JSON(http.StatusInternalServerError, ErrorFmt("something went wrong. Try again"))
}
// Store data in cache
memoryCache.Set(key, root, cache.DefaultExpiration)
return c.JSONPretty(http.StatusOK, root, " ")
}