-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.go
More file actions
406 lines (347 loc) · 12.8 KB
/
search.go
File metadata and controls
406 lines (347 loc) · 12.8 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
package peerdb
import (
"io"
"net/http"
essearch "github.com/elastic/go-elasticsearch/v9/typedapi/core/search"
"github.com/elastic/go-elasticsearch/v9/typedapi/esdsl"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/go/x"
"gitlab.com/tozd/identifier"
"gitlab.com/tozd/waf"
internalStore "gitlab.com/peerdb/peerdb/internal/store"
"gitlab.com/peerdb/peerdb/search"
)
func (s *Service) getSearchService(req *http.Request) (*essearch.Search, int64, int64) {
ctx := req.Context()
site := waf.MustGetSite[*Site](ctx)
// We set TrackTotalHits to true to always get exact number of results. For now we didn't notice any performance
// issues at data scale PeerDB is currently being used with, but in the future we might want to make this configurable.
return site.ESClient.Search().Index(site.Index).
Source_(esdsl.NewSourceConfig().Bool(false)).
Preference(getHost(req.RemoteAddr)).
Header("X-Opaque-ID", waf.MustRequestID(ctx).String()).
TrackTotalHits(esdsl.NewTrackHits().Bool(true)).
AllowPartialSearchResults(false), site.propertiesTotal, site.unitsTotal
}
func (s *Service) getSearchServiceClosure(req *http.Request) func() (*essearch.Search, int64, int64) {
return func() (*essearch.Search, int64, int64) {
return s.getSearchService(req)
}
}
// SearchAmountFilterGetAPI handles GET requests for amount filter search endpoints.
func (s *Service) SearchAmountFilterGetAPI(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.MaybeString(params["id"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"id" is not a valid identifier`))
return
}
prop, errE := identifier.MaybeString(params["prop"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"prop" is not a valid identifier`))
return
}
var unit *identifier.Identifier
if unitStr, ok := params["unit"]; ok {
u, errE := identifier.MaybeString(unitStr)
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"unit" is not a valid identifier`))
return
}
unit = &u
}
data, metadata, errE := search.AmountFilterGet(req.Context(), s.getSearchServiceClosure(req), id, prop, unit)
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
// SearchRefFilterGetAPI handles GET requests for reference filter search endpoints.
//
//nolint:dupl
func (s *Service) SearchRefFilterGetAPI(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.MaybeString(params["id"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"id" is not a valid identifier`))
return
}
prop, errE := identifier.MaybeString(params["prop"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"prop" is not a valid identifier`))
return
}
data, metadata, errE := search.RefFilterGet(req.Context(), s.getSearchServiceClosure(req), id, prop)
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
// SearchTimeFilterGetAPI handles GET requests for time filter search endpoints.
//
//nolint:dupl
func (s *Service) SearchTimeFilterGetAPI(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.MaybeString(params["id"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"id" is not a valid identifier`))
return
}
prop, errE := identifier.MaybeString(params["prop"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"prop" is not a valid identifier`))
return
}
data, metadata, errE := search.TimeFilterGet(req.Context(), s.getSearchServiceClosure(req), id, prop)
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
// SearchGetGet is a GET/HEAD HTTP request handler which returns HTML frontend for searching documents.
func (s *Service) SearchGetGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
m := metrics.Duration(internalStore.MetricSearchSession).Start()
_, errE := search.GetSessionFromID(ctx, params["id"])
m.Stop()
if errors.Is(errE, search.ErrNotFound) {
// TODO: We should show some nice 404 error page here.
s.NotFoundWithError(w, req, errE)
return
} else if errE != nil {
// TODO: We should show some nice 500 error page here.
s.InternalServerErrorWithError(w, req, errE)
return
}
s.HomeGet(w, req, nil)
}
// SearchGetGetAPI is a GET/HEAD HTTP request API handler which returns a search session.
func (s *Service) SearchGetGetAPI(w http.ResponseWriter, req *http.Request, params waf.Params) {
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
m := metrics.Duration(internalStore.MetricSearchSession).Start()
searchSession, errE := search.GetSessionFromID(ctx, params["id"])
m.Stop()
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, searchSession, nil)
}
// SearchFiltersGetAPI is a GET/HEAD HTTP request API handler which returns filters available for the search session.
func (s *Service) SearchFiltersGetAPI(w http.ResponseWriter, req *http.Request, params waf.Params) { //nolint:dupl
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
m := metrics.Duration(internalStore.MetricSearchSession).Start()
searchSession, errE := search.GetSessionFromID(ctx, params["id"])
m.Stop()
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
data, metadata, errE := search.FiltersGet(ctx, s.getSearchServiceClosure(req), searchSession)
if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
// SearchResultsGetAPI is a GET/HEAD HTTP request API handler and it searches ElasticSearch index for the provided
// search session and returns to the client a JSON with an array of IDs of found documents.
// It returns search metadata (e.g., total results) as waf HTTP response header.
func (s *Service) SearchResultsGetAPI(w http.ResponseWriter, req *http.Request, params waf.Params) { //nolint:dupl
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
m := metrics.Duration(internalStore.MetricSearchSession).Start()
searchSession, errE := search.GetSessionFromID(ctx, params["id"])
m.Stop()
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
data, metadata, errE := search.ResultsGet(ctx, s.getSearchServiceClosure(req), searchSession)
if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
// SearchJustResultsPostAPI is a POST HTTP request API handler and it searches ElasticSearch index without
// creating a search session and returns to the client a JSON with an array of IDs of found documents.
// It returns search metadata (e.g., total results) as waf HTTP response header.
func (s *Service) SearchJustResultsPostAPI(w http.ResponseWriter, req *http.Request, _ waf.Params) {
defer req.Body.Close() //nolint:errcheck
defer io.Copy(io.Discard, req.Body) //nolint:errcheck
ctx := req.Context()
var searchSession search.Session
errE := x.DecodeJSONWithoutUnknownFields(req.Body, &searchSession)
if errE != nil {
s.BadRequestWithError(w, req, errE)
return
}
if searchSession.ID != nil {
s.BadRequestWithError(w, req, errors.New("payload contains ID"))
return
}
errE = searchSession.Validate(ctx, nil)
if errE != nil {
errE = errors.WrapWith(errE, search.ErrValidationFailed)
s.BadRequestWithError(w, req, errE)
return
}
data, metadata, errE := search.ResultsGet(ctx, s.getSearchServiceClosure(req), &searchSession)
if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
// SearchCreatePostAPI is a POST HTTP API request handler which creates a new search session.
func (s *Service) SearchCreatePostAPI(w http.ResponseWriter, req *http.Request, _ waf.Params) {
defer req.Body.Close() //nolint:errcheck
defer io.Copy(io.Discard, req.Body) //nolint:errcheck
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
var searchSession search.Session
errE := x.DecodeJSONWithoutUnknownFields(req.Body, &searchSession)
if errE != nil {
s.BadRequestWithError(w, req, errE)
return
}
if searchSession.ID != nil {
s.BadRequestWithError(w, req, errors.New("payload contains ID"))
return
}
m := metrics.Duration(internalStore.MetricSearchSession).Start()
errE = search.CreateSession(ctx, &searchSession)
m.Stop()
if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, searchSession.Ref(), nil)
}
// SearchUpdatePostAPI is a POST HTTP API request handler which updates the search session.
func (s *Service) SearchUpdatePostAPI(w http.ResponseWriter, req *http.Request, params waf.Params) {
defer req.Body.Close() //nolint:errcheck
defer io.Copy(io.Discard, req.Body) //nolint:errcheck
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
var searchSession search.Session
errE := x.DecodeJSONWithoutUnknownFields(req.Body, &searchSession)
if errE != nil {
s.BadRequestWithError(w, req, errE)
return
}
// If searchSession.ID == nil, UpdateSession returns an error.
if searchSession.ID != nil && params["id"] != searchSession.ID.String() {
errE = errors.New("params ID does not match payload ID")
errors.Details(errE)["params"] = params["id"]
errors.Details(errE)["payload"] = *searchSession.ID
s.BadRequestWithError(w, req, errE)
return
}
m := metrics.Duration(internalStore.MetricSearchSession).Start()
errE = search.UpdateSession(ctx, &searchSession)
m.Stop()
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, searchSession.Ref(), nil)
}
// SearchShortcutGet is a GET/HEAD HTTP request handler which creates a new search session
// from query parameters and redirects to the search page. Query parameters are interpreted
// as ref filters where key is the property ID and value is the value ID.
func (s *Service) SearchShortcutGet(w http.ResponseWriter, req *http.Request, _ waf.Params) {
ctx := req.Context()
var filters []search.Filters
for prop, values := range req.URL.Query() {
propID, errE := identifier.MaybeString(prop)
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, "query parameter key is not a valid identifier"))
return
}
for _, value := range values {
valueID, errE := identifier.MaybeString(value)
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, "query parameter value is not a valid identifier"))
return
}
filters = append(filters, search.Filters{ //nolint:exhaustruct
Ref: &search.RefFilter{ //nolint:exhaustruct
Prop: propID,
Value: &valueID,
},
})
}
}
searchSession := search.Session{}
if len(filters) == 1 {
searchSession.Filters = &filters[0]
} else if len(filters) > 1 {
searchSession.Filters = &search.Filters{ //nolint:exhaustruct
And: filters,
}
}
errE := search.CreateSession(ctx, &searchSession)
if errors.Is(errE, search.ErrValidationFailed) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
path, errE := s.Reverse("SearchGet", waf.Params{"id": searchSession.ID.String()}, nil)
if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.TemporaryRedirectGetMethod(w, req, path)
}