-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
386 lines (329 loc) · 11.8 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
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
package peerdb
import (
"context"
"fmt"
"net/http"
"time"
"github.com/olivere/elastic/v7"
"gitlab.com/tozd/go/errors"
"gitlab.com/tozd/go/x"
"gitlab.com/tozd/identifier"
"gitlab.com/tozd/waf"
internal "gitlab.com/peerdb/peerdb/internal/store"
"gitlab.com/peerdb/peerdb/search"
)
// TODO: Limit properties only to those really used in filters ("rel", "amount", "amountRange")?
func (s *Service) populatePropertiesTotal(ctx context.Context) errors.E {
boolQuery := elastic.NewBoolQuery().Must(
elastic.NewTermQuery("claims.rel.prop.id", "CAfaL1ZZs6L4uyFdrJZ2wN"), // TYPE.
elastic.NewTermQuery("claims.rel.to.id", "HohteEmv2o7gPRnJ5wukVe"), // PROPERTY.
)
query := elastic.NewNestedQuery("claims.rel", boolQuery)
for _, site := range s.Sites {
total, err := s.esClient.Count(site.Index).Query(query).Do(ctx)
if err != nil {
return errors.Errorf(`site "%s": %w`, site.Index, err)
}
site.propertiesTotal = total
}
return nil
}
func (s *Service) getSearchService(req *http.Request) (*elastic.SearchService, int64) {
ctx := req.Context()
site := waf.MustGetSite[*Site](ctx)
// The fact that TrackTotalHits is set to true is important because the count is used as the
// number of documents of the filter on the _index field.
return s.esClient.Search(site.Index).FetchSource(false).Preference(getHost(req.RemoteAddr)).
Header("X-Opaque-ID", waf.MustRequestID(ctx).String()).TrackTotalHits(true).AllowPartialSearchResults(false), site.propertiesTotal
}
func (s *Service) getSearchServiceClosure(req *http.Request) func() (*elastic.SearchService, int64) {
return func() (*elastic.SearchService, int64) {
return s.getSearchService(req)
}
}
func (s *Service) SearchAmountFilterGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.FromString(params["s"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"s" is not a valid identifier`))
return
}
prop, errE := identifier.FromString(params["prop"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"prop" is not a valid identifier`))
return
}
data, metadata, errE := search.AmountFilterGet(req.Context(), s.getSearchServiceClosure(req), id, prop, params["unit"])
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrInvalidArgument) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
func (s *Service) SearchFiltersGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.FromString(params["s"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"s" is not a valid identifier`))
return
}
data, metadata, errE := search.FiltersGet(req.Context(), s.getSearchServiceClosure(req), id)
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrInvalidArgument) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
func (s *Service) SearchIndexFilterGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.FromString(params["s"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"s" is not a valid identifier`))
return
}
data, metadata, errE := search.IndexFilterGet(req.Context(), s.getSearchServiceClosure(req), id)
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrInvalidArgument) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
//nolint:dupl
func (s *Service) SearchRelFilterGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.FromString(params["s"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"s" is not a valid identifier`))
return
}
prop, errE := identifier.FromString(params["prop"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"prop" is not a valid identifier`))
return
}
data, metadata, errE := search.RelFilterGet(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.ErrInvalidArgument) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
func (s *Service) SearchSizeFilterGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.FromString(params["s"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"s" is not a valid identifier`))
return
}
data, metadata, errE := search.SizeFilterGet(req.Context(), s.getSearchServiceClosure(req), id)
if errors.Is(errE, search.ErrNotFound) {
s.NotFoundWithError(w, req, errE)
return
} else if errors.Is(errE, search.ErrInvalidArgument) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
//nolint:dupl
func (s *Service) SearchStringFilterGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.FromString(params["s"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"s" is not a valid identifier`))
return
}
prop, errE := identifier.FromString(params["prop"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"prop" is not a valid identifier`))
return
}
data, metadata, errE := search.StringFilterGet(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.ErrInvalidArgument) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
//nolint:dupl
func (s *Service) SearchTimeFilterGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
id, errE := identifier.FromString(params["s"])
if errE != nil {
s.BadRequestWithError(w, req, errors.WithMessage(errE, `"s" is not a valid identifier`))
return
}
prop, errE := identifier.FromString(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.ErrInvalidArgument) {
s.BadRequestWithError(w, req, errE)
return
} else if errE != nil {
s.InternalServerErrorWithError(w, req, errE)
return
}
s.WriteJSON(w, req, data, metadata)
}
// SearchGet is a GET/HEAD HTTP request handler which returns HTML frontend for searching documents.
// If search state is invalid, it redirects to a valid one.
func (s *Service) SearchGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
var q *string
if req.Form.Has("q") {
qq := req.Form.Get("q")
q = &qq
}
var filters *string
if req.Form.Has("filters") {
f := req.Form.Get("filters")
filters = &f
}
m := metrics.Duration(internal.MetricSearchState).Start()
sh, ok := search.GetOrCreateState(params["s"], q, filters)
m.Stop()
if !ok {
// Something was not OK, so we redirect to the correct URL.
path, err := s.Reverse("SearchGet", waf.Params{"s": sh.ID.String()}, sh.Values())
if err != nil {
s.InternalServerErrorWithError(w, req, err)
return
}
// TODO: Should we already do the query, to warm up ES cache?
// Maybe we should cache response ourselves so that we do not hit store twice?
w.Header().Set("Location", path)
w.WriteHeader(http.StatusSeeOther)
return
} else if !req.Form.Has("q") {
// "q" is missing, so we redirect to the correct URL.
path, err := s.Reverse("SearchGet", waf.Params{"s": sh.ID.String()}, sh.ValuesWithAt(req.Form.Get("at")))
if err != nil {
s.InternalServerErrorWithError(w, req, err)
return
}
// TODO: Should we already do the query, to warm up ES cache?
// Maybe we should cache response ourselves so that we do not hit store twice?
w.Header().Set("Location", path)
w.WriteHeader(http.StatusSeeOther)
return
}
s.Home(w, req, nil)
}
type searchResult struct {
ID string `json:"id"`
}
// SearchGetGet is a GET/HEAD HTTP request handler and it searches ElasticSearch index using provided
// search state and returns to the client a JSON with an array of IDs of found documents. If search state is
// invalid, it returns correct query parameters as JSON. It supports compression based on accepted content
// encoding and range requests. It returns search metadata (e.g., total results) as PeerDB HTTP response headers.
func (s *Service) SearchGetGet(w http.ResponseWriter, req *http.Request, params waf.Params) {
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
var q *string
if req.Form.Has("q") {
qq := req.Form.Get("q")
q = &qq
}
var filters *string
if req.Form.Has("filters") {
f := req.Form.Get("filters")
filters = &f
}
m := metrics.Duration(internal.MetricSearchState).Start()
sh, ok := search.GetOrCreateState(params["s"], q, filters)
m.Stop()
if !ok {
// Something was not OK, so we return new query parameters.
// TODO: Should we already do the query, to warm up ES cache?
// Maybe we should cache response ourselves so that we do not hit store twice?
s.WriteJSON(w, req, sh, nil)
return
}
searchService, _ := s.getSearchService(req)
searchService = searchService.From(0).Size(search.MaxResultsCount).Query(sh.Query())
m = metrics.Duration(internal.MetricElasticSearch).Start()
res, err := searchService.Do(ctx)
m.Stop()
if err != nil {
s.InternalServerErrorWithError(w, req, errors.WithStack(err))
return
}
metrics.Duration(internal.MetricElasticSearchInternal).Duration = time.Duration(res.TookInMillis) * time.Millisecond
results := make([]searchResult, len(res.Hits.Hits))
for i, hit := range res.Hits.Hits {
results[i] = searchResult{ID: hit.Id}
}
// Total is a string or a number.
var total interface{}
if res.Hits.TotalHits.Relation == "gte" {
total = fmt.Sprintf("+%d", res.Hits.TotalHits.Value)
} else {
total = res.Hits.TotalHits.Value
}
// TODO: Move this to a separate API endpoint.
filtersJSON, err := x.MarshalWithoutEscapeHTML(sh.Filters)
if err != nil {
s.InternalServerErrorWithError(w, req, errors.WithStack(err))
return
}
s.WriteJSON(w, req, results, map[string]interface{}{
"total": total,
"query": sh.Text,
"filters": string(filtersJSON),
})
}
// SearchCreatePost is a POST HTTP request handler which stores the search state and returns
// query parameters for the GET endpoint as JSON or redirects to the GET endpoint based on search ID.
func (s *Service) SearchCreatePost(w http.ResponseWriter, req *http.Request, _ waf.Params) {
ctx := req.Context()
metrics := waf.MustGetMetrics(ctx)
var q *string
if req.Form.Has("q") {
qq := req.Form.Get("q")
q = &qq
}
var filters *string
if req.Form.Has("filters") {
f := req.Form.Get("filters")
filters = &f
}
m := metrics.Duration(internal.MetricSearchState).Start()
sh := search.CreateState(req.Form.Get("s"), q, filters)
m.Stop()
// TODO: Should we already do the query, to warm up ES cache?
// Maybe we should cache response ourselves so that we do not hit store twice?
s.WriteJSON(w, req, sh, nil)
}