-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_logger.go
239 lines (214 loc) · 6.24 KB
/
search_logger.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package search
import (
"context"
"encoding/json"
"io"
"strings"
"time"
"github.com/pkg/errors"
"gopkg.in/olivere/elastic.v5"
)
type SearchLog struct {
Created time.Time `json:"created",omitempty`
Query Query `json:"query"`
QueryResult interface{} `json:"query_result,omitempty"`
Error interface{} `json:"error,omitempty"`
SortBy string `json:"sort_by,omitempty"`
From uint64 `json:"from"`
Size uint64 `json:"size,omitempty"`
SearchId string `json:"search_id"`
// Deprecated field.
Results interface{} `json:"results,omitempty"`
}
type SearchClick struct {
Created time.Time `json:"click_created,omitempty"`
MdbUid string `json:"mdb_uid",omitempty`
Index string `json:"index",omitempty`
Type string `json:"type",omitempty`
Rank uint32 `json:"rank",omitempty`
SearchId string `json:"search_id",omitempty`
}
type CreatedSearchLogs []SearchLog
func (csl CreatedSearchLogs) Len() int {
return len(csl)
}
func (csl CreatedSearchLogs) Less(i, j int) bool {
return csl[i].Created.Before(csl[j].Created)
}
func (csl CreatedSearchLogs) Swap(i, j int) {
csl[i], csl[j] = csl[j], csl[i]
}
type CreatedSearchClicks []SearchClick
func (csc CreatedSearchClicks) Len() int {
return len(csc)
}
func (csc CreatedSearchClicks) Less(i, j int) bool {
return csc[i].Created.Before(csc[j].Created)
}
func (csc CreatedSearchClicks) Swap(i, j int) {
csc[i], csc[j] = csc[j], csc[i]
}
type SearchLogger struct {
esc *elastic.Client
}
func MakeSearchLogger(esc *elastic.Client) *SearchLogger {
return &SearchLogger{esc: esc}
}
func (searchLogger *SearchLogger) LogClick(mdbUid string, index string, indexType string, rank int, searchId string) error {
sc := SearchClick{
Created: time.Now(),
MdbUid: mdbUid,
Index: index,
Type: indexType,
Rank: uint32(rank),
SearchId: searchId,
}
sr, err := elastic.NewSearchService(searchLogger.esc).
Index("search_logs").
Type("search_logs").
Query(elastic.NewMatchQuery("search_id", searchId)).
Do(context.TODO())
if sr.Hits == nil || sr.Hits.Hits == nil || len(sr.Hits.Hits) == 0 {
return errors.Errorf("Did not find appropriate search id %s", searchId)
}
if len(sr.Hits.Hits) > 1 {
return errors.Errorf("Found more then one search id %s", searchId)
}
parentId := sr.Hits.Hits[0].Id
resp, err := searchLogger.esc.Index().
Index("search_logs").
Type("search_clicks").
BodyJson(sc).
Parent(parentId).
Do(context.TODO())
if err != nil {
return errors.Wrap(err, "Log Click")
}
if !resp.Created {
return errors.Errorf("Click log not created.")
}
return nil
}
func (searchLogger *SearchLogger) LogSearch(query Query, sortBy string, from int, size int, searchId string, res *QueryResult) error {
return searchLogger.logSearch(query, sortBy, from, size, searchId, res, nil)
}
func (searchLogger *SearchLogger) LogSearchError(query Query, sortBy string, from int, size int, searchId string, searchErr interface{}) error {
return searchLogger.logSearch(query, sortBy, from, size, searchId, nil, searchErr)
}
func (searchLogger *SearchLogger) fixHighlight(h *elastic.SearchHitHighlight) *elastic.SearchHitHighlight {
if h == nil {
return nil
}
hRet := make(elastic.SearchHitHighlight, 0)
for fieldName, highlights := range *h {
fixedFieldName := strings.Replace(fieldName, ".", "_", -1)
hRet[fixedFieldName] = highlights
}
return &hRet
}
// Should not change the input, should copy hit and fix highlight fields.
func (searchLogger *SearchLogger) fixResults(res *QueryResult) *QueryResult {
if res == nil {
return nil
}
if res.SearchResult.Hits != nil && res.SearchResult.Hits.Hits != nil && len(res.SearchResult.Hits.Hits) > 0 {
hitsCopy := make([]*elastic.SearchHit, len(res.SearchResult.Hits.Hits))
for i, h := range res.SearchResult.Hits.Hits {
hCopy := *h
hCopy.Highlight = *searchLogger.fixHighlight(&hCopy.Highlight)
hitsCopy[i] = &hCopy
}
searchResultCopy := *res.SearchResult
searchResultCopy.Hits.Hits = hitsCopy
resCopy := *res
resCopy.SearchResult = &searchResultCopy
return &resCopy
}
return res
}
func (searchLogger *SearchLogger) logSearch(query Query, sortBy string, from int, size int, searchId string, res *QueryResult, searchErr interface{}) error {
sl := SearchLog{
Created: time.Now(),
Query: query,
QueryResult: searchLogger.fixResults(res),
Error: searchErr,
SortBy: sortBy,
From: uint64(from),
Size: uint64(size),
SearchId: searchId,
}
resp, err := searchLogger.esc.Index().
Index("search_logs").
Type("search_logs").
BodyJson(sl).
Do(context.TODO())
if err != nil {
return errors.Wrap(err, "Log Search")
}
if !resp.Created {
return errors.Errorf("Search log not created.")
}
return nil
}
func (searchLogger *SearchLogger) GetAllQueries() ([]SearchLog, error) {
var ret []SearchLog
var searchResult *elastic.SearchResult
for true {
if searchResult != nil && searchResult.Hits != nil {
for _, h := range searchResult.Hits.Hits {
sl := SearchLog{}
json.Unmarshal(*h.Source, &sl)
ret = append(ret, sl)
}
}
var err error
scrollClient := searchLogger.esc.Scroll().
Index("search_logs").
Type("search_logs").
Query(elastic.NewMatchAllQuery()).
Scroll("1m").
Size(100)
if searchResult != nil {
scrollClient = scrollClient.ScrollId(searchResult.ScrollId)
}
searchResult, err = scrollClient.Do(context.TODO())
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
}
return ret, nil
}
func (searchLogger *SearchLogger) GetAllClicks() ([]SearchClick, error) {
var ret []SearchClick
var searchResult *elastic.SearchResult
for true {
if searchResult != nil && searchResult.Hits != nil {
for _, h := range searchResult.Hits.Hits {
sl := SearchClick{}
json.Unmarshal(*h.Source, &sl)
ret = append(ret, sl)
}
}
var err error
scrollClient := searchLogger.esc.Scroll().
Index("search_logs").
Type("search_clicks").
Query(elastic.NewMatchAllQuery()).
Scroll("1m").
Size(100)
if searchResult != nil {
scrollClient = scrollClient.ScrollId(searchResult.ScrollId)
}
searchResult, err = scrollClient.Do(context.TODO())
if err != nil {
if err == io.EOF {
break
}
return nil, err
}
}
return ret, nil
}