/
spelunker.go
445 lines (306 loc) · 9.62 KB
/
spelunker.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package opensearch
import (
"context"
"fmt"
"io"
"log/slog"
"math"
_ "net/http"
"net/url"
"strings"
"time"
"github.com/aaronland/go-pagination"
"github.com/aaronland/go-pagination/countable"
"github.com/aaronland/go-pagination/cursor"
opensearch "github.com/opensearch-project/opensearch-go/v2"
opensearchapi "github.com/opensearch-project/opensearch-go/v2/opensearchapi"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-reader"
_ "github.com/whosonfirst/go-reader-http"
"github.com/whosonfirst/go-whosonfirst-opensearch/client"
"github.com/whosonfirst/go-whosonfirst-spelunker"
wof_spr "github.com/whosonfirst/go-whosonfirst-spr/v2"
"github.com/whosonfirst/go-whosonfirst-uri"
)
const scroll_duration time.Duration = 5 * time.Minute
const scroll_trigger int64 = 10000
type OpenSearchSpelunker struct {
spelunker.Spelunker
client *opensearch.Client
index string
reader reader.Reader
}
func init() {
ctx := context.Background()
spelunker.RegisterSpelunker(ctx, "opensearch", NewOpenSearchSpelunker)
}
func NewOpenSearchSpelunker(ctx context.Context, uri string) (spelunker.Spelunker, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}
q := u.Query()
dsn := q.Get("dsn")
if dsn == "" {
return nil, fmt.Errorf("Missing ?dsn= parameter")
}
cl, err := client.NewClient(ctx, dsn)
if err != nil {
return nil, fmt.Errorf("Failed to create opensearch client, %w", err)
}
s := &OpenSearchSpelunker{
client: cl,
index: "spelunker",
}
if q.Has("reader-uri") {
reader_uri := q.Get("reader_uri")
r, err := reader.NewReader(ctx, reader_uri)
if err != nil {
return nil, fmt.Errorf("Failed to create reader, %w", err)
}
s.reader = r
}
return s, nil
}
func (s *OpenSearchSpelunker) GetRecordForId(ctx context.Context, id int64) ([]byte, error) {
q := fmt.Sprintf(`{"query": { "ids": { "values": [ %d ] } } }`, id)
req := &opensearchapi.SearchRequest{
Body: strings.NewReader(q),
}
body, err := s.searchWithIndex(ctx, req)
if err != nil {
slog.Error("Get by ID query failed", "q", q)
return nil, fmt.Errorf("Failed to retrieve %d, %w", id, err)
}
r := gjson.GetBytes(body, "hits.hits.0._source")
if !r.Exists() {
return nil, fmt.Errorf("First hit missing")
}
return []byte(r.String()), nil
}
func (s *OpenSearchSpelunker) GetFeatureForId(ctx context.Context, id int64, uri_args *uri.URIArgs) ([]byte, error) {
rel_path, err := uri.Id2RelPath(id, uri_args)
if err != nil {
return nil, err
}
f_reader := s.reader
if f_reader == nil {
record, err := s.GetRecordForId(ctx, id)
if err != nil {
return nil, err
}
repo_name := gjson.GetBytes(record, "wof:repo")
reader_uri := fmt.Sprintf("https://raw.githubusercontent.com/whosonfirst-data/%s/master/data", repo_name)
r, err := reader.NewReader(ctx, reader_uri)
if err != nil {
return nil, err
}
f_reader = r
}
r, err := f_reader.Read(ctx, rel_path)
if err != nil {
return nil, err
}
defer r.Close()
return io.ReadAll(r)
}
func (s *OpenSearchSpelunker) facet(ctx context.Context, req *opensearchapi.SearchRequest, facets []*spelunker.Facet) ([]*spelunker.Faceting, error) {
body, err := s.searchWithIndex(ctx, req)
if err != nil {
return nil, fmt.Errorf("Failed to query facets, %w", err)
}
aggs_rsp := gjson.GetBytes(body, "aggregations")
if !aggs_rsp.Exists() {
return nil, fmt.Errorf("Failed to derive facets, missing")
}
facetings := make([]*spelunker.Faceting, 0)
for k, rsp := range aggs_rsp.Map() {
facet_results := make([]*spelunker.FacetCount, 0)
buckets_rsp := rsp.Get("buckets")
for _, b := range buckets_rsp.Array() {
k_rsp := b.Get("key")
v_rsp := b.Get("doc_count")
fc := &spelunker.FacetCount{
Key: k_rsp.String(),
Count: v_rsp.Int(),
}
facet_results = append(facet_results, fc)
}
var bucket_facet *spelunker.Facet
for _, f := range facets {
if f.String() == k {
bucket_facet = f
break
}
}
if bucket_facet == nil {
return nil, fmt.Errorf("Failed to determine facet for bucket key '%s'", k)
}
faceting := &spelunker.Faceting{
Facet: bucket_facet,
Results: facet_results,
}
facetings = append(facetings, faceting)
}
return facetings, nil
}
// searchPaginated wraps all the logic for determining whether to do a cursor-based or plain-vanilla-paginated query
func (s *OpenSearchSpelunker) searchPaginated(ctx context.Context, pg_opts pagination.Options, q string) (wof_spr.StandardPlacesResults, pagination.Results, error) {
scroll_id := ""
pre_count := false
use_scroll := false
if pg_opts.Method() == pagination.Cursor {
scroll_id = pg_opts.Pointer().(string)
}
if scroll_id == "" {
pre_count = true
}
if pre_count {
count, err := s.countForQuery(ctx, q)
if err != nil {
return nil, nil, err
}
if count >= scroll_trigger {
use_scroll = true
}
}
var body []byte
var err error
if scroll_id != "" {
// See this? Neither of these things are documented anywhere.
// Good times...
scroll_id = strings.TrimLeft(scroll_id, "after-")
q = fmt.Sprintf(`{"scroll_id": "%s"}`, scroll_id)
req := &opensearchapi.ScrollRequest{
Body: strings.NewReader(q),
ScrollID: scroll_id,
Scroll: scroll_duration,
}
body, err = s.searchWithScroll(ctx, req)
} else {
sz := int(pg_opts.PerPage())
from := int(pg_opts.PerPage() * (pg_opts.Pointer().(int64) - 1))
req := &opensearchapi.SearchRequest{
Body: strings.NewReader(q),
Size: &sz,
From: &from,
}
if use_scroll {
req.Scroll = scroll_duration
}
body, err = s.searchWithIndex(ctx, req)
}
// To do: Check for expired scroll
if err != nil {
return nil, nil, fmt.Errorf("Failed to execute search, %w", err)
}
return s.searchResultsToSPR(ctx, pg_opts, body)
}
// https://pkg.go.dev/github.com/opensearch-project/opensearch-go/v2@v2.3.0/opensearchapi#SearchRequest
func (s *OpenSearchSpelunker) searchWithIndex(ctx context.Context, req *opensearchapi.SearchRequest) ([]byte, error) {
if len(req.Index) == 0 {
req.Index = []string{
s.index,
}
}
rsp, err := req.Do(ctx, s.client)
if err != nil {
return nil, fmt.Errorf("Failed to execute search, %w", err)
}
defer rsp.Body.Close()
if rsp.StatusCode != 200 {
body, _ := io.ReadAll(rsp.Body)
slog.Error(string(body))
slog.Error("Query failed", "status", rsp.StatusCode)
return nil, fmt.Errorf("Invalid status")
}
body, err := io.ReadAll(rsp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read response, %w", err)
}
return body, nil
}
// https://pkg.go.dev/github.com/opensearch-project/opensearch-go/v2/opensearchapi#ScrollRequest
func (s *OpenSearchSpelunker) searchWithScroll(ctx context.Context, req *opensearchapi.ScrollRequest) ([]byte, error) {
rsp, err := req.Do(ctx, s.client)
if err != nil {
return nil, fmt.Errorf("Failed to execute search, %w", err)
}
defer rsp.Body.Close()
// To do: Check for expired cursor...
if rsp.StatusCode != 200 {
// body, _ := io.ReadAll(rsp.Body)
// slog.Error(string(body))
slog.Error("Query failed", "status", rsp.StatusCode)
return nil, fmt.Errorf("Invalid status")
}
body, err := io.ReadAll(rsp.Body)
if err != nil {
return nil, fmt.Errorf("Failed to read response, %w", err)
}
return body, nil
}
func (s *OpenSearchSpelunker) propsToGeoJSON(props []byte) []byte {
// See this? It's a derived geometry. Still working through
// how to signal and how to fetch full geometry...
lat_rsp := gjson.GetBytes(props, "geom:latitude")
lon_rsp := gjson.GetBytes(props, "geom:longitude")
lat := lat_rsp.String()
lon := lon_rsp.String()
return []byte(`{"type": "Feature", "properties": ` + string(props) + `, "geometry": { "type": "Point", "coordinates": [` + lon + `,` + lat + `] } }`)
}
func (s *OpenSearchSpelunker) countForQuery(ctx context.Context, q string) (int64, error) {
sz := 0
req := &opensearchapi.SearchRequest{
Body: strings.NewReader(q),
Size: &sz,
}
body, err := s.searchWithIndex(ctx, req)
if err != nil {
return 0, fmt.Errorf("Failed to determine count for query, %w", err)
}
r := gjson.GetBytes(body, "hits.total.value")
count := r.Int()
return count, nil
}
func (s *OpenSearchSpelunker) searchResultsToSPR(ctx context.Context, pg_opts pagination.Options, body []byte) (wof_spr.StandardPlacesResults, pagination.Results, error) {
scroll_rsp := gjson.GetBytes(body, "_scroll_id")
scroll_id := scroll_rsp.String()
total_rsp := gjson.GetBytes(body, "hits.total.value")
count := total_rsp.Int()
var pg_results pagination.Results
var pg_err error
if scroll_id != "" {
page_count := math.Ceil(float64(count) / float64(pg_opts.PerPage()))
c_results := new(cursor.CursorResults)
c_results.TotalCount = count
c_results.PerPageCount = pg_opts.PerPage()
c_results.CursorNext = scroll_id
c_results.PageCount = int64(page_count)
pg_results = c_results
} else {
if pg_opts != nil {
pg_results, pg_err = countable.NewResultsFromCountWithOptions(pg_opts, count)
} else {
pg_results, pg_err = countable.NewResultsFromCount(count)
}
}
if pg_err != nil {
return nil, nil, pg_err
}
// START OF put me in a(nother) function
hits_r := gjson.GetBytes(body, "hits.hits")
count_hits := len(hits_r.Array())
results := make([]wof_spr.StandardPlacesResult, count_hits)
for idx, r := range hits_r.Array() {
src := r.Get("_source")
sp_spr, err := NewSpelunkerRecordSPR([]byte(src.String()))
if err != nil {
slog.Error("Failed to derive SPR from result", "index", idx, "error", err)
return nil, nil, fmt.Errorf("Failed to derive SPR from result, %w", err)
}
results[idx] = sp_spr
}
spr_results := NewSpelunkerStandardPlacesResults(results)
return spr_results, pg_results, nil
}