-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathdatabase.js
More file actions
1188 lines (1059 loc) · 29.7 KB
/
database.js
File metadata and controls
1188 lines (1059 loc) · 29.7 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
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { isEmpty } from 'lodash-es'
import { dbClient as _client, createIndex } from './database-client.js'
import logger from './logger.js'
import { NotFoundError, ValidationError } from './errors.js'
import { bboxToPolygon } from './geo-utils.js'
const COLLECTIONS_INDEX = process.env['COLLECTIONS_INDEX'] || 'collections'
const DEFAULT_INDICES = ['*', '-.*', '-collections']
const OP = {
AND: 'and',
OR: 'or',
NOT: 'not',
EQ: '=',
NEQ: '<>',
LT: '<',
LTE: '<=',
GT: '>',
GTE: '>=',
IS_NULL: 'isNull',
IN: 'in',
BETWEEN: 'between',
LIKE: 'like',
S_INTERSECTS: 's_intersects',
}
const RANGE_TRANSLATION = {
'<': 'lt',
'<=': 'lte',
'>': 'gt',
'>=': 'gte'
}
const UNPREFIXED_FIELDS = [
'id',
'collection',
'geometry',
'bbox'
]
const GEOMETRY_TYPES = ['Point', 'LineString', 'Polygon', 'MultiPoint',
'MultiLineString', 'MultiPolygon', 'GeometryCollection']
export const DEFAULT_FIELDS = [
'id',
'type',
'stac_version',
'geometry',
'bbox',
'links',
'assets',
'collection',
'properties.datetime'
]
const MAX_COLLECTIONS_IN_QUERY_PATH = 10
let collectionToIndexMapping = null
let unrestrictedIndices = null
export const isIndexNotFoundError = (e) => (
e instanceof Error
&& e.name === 'ResponseError'
&& e.message.includes('index_not_found_exception'))
/*
This module is used for connecting to a search database instance, writing records,
searching records, and managing the indexes. It looks for the OPENSEARCH_HOST environment
variable which is the URL to the search database host line
*/
function buildRangeQuery(property, operators, operatorsObject) {
const gt = 'gt'
const lt = 'lt'
const gte = 'gte'
const lte = 'lte'
const comparisons = [gt, lt, gte, lte]
let rangeQuery
if (operators.includes(gt) || operators.includes(lt)
|| operators.includes(gte) || operators.includes(lte)) {
const propertyKey = `properties.${property}`
rangeQuery = {
range: {
[propertyKey]: {
}
}
}
// All operators for a property go in a single range query.
comparisons.forEach((comparison) => {
if (operators.includes(comparison)) {
const existing = rangeQuery.range[propertyKey]
rangeQuery.range[propertyKey] = { ...existing, [comparison]: operatorsObject[comparison] }
}
})
}
return rangeQuery
}
// assumes a valid RFC3339 datetime or interval
// validation was previously done by api.extractDatetime
export function buildDatetimeQuery(parameters) {
let dateQuery
const { datetime } = parameters
if (datetime) {
if (datetime.includes('/')) {
const [start, end] = datetime.split('/')
const datetimeRange = {}
if (start && start !== '..') datetimeRange.gte = start
if (end && end !== '..') datetimeRange.lte = end
dateQuery = {
range: {
'properties.datetime': datetimeRange
}
}
} else {
dateQuery = {
term: {
'properties.datetime': datetime
}
}
}
}
return dateQuery
}
function IN(cql2Field, cql2Value) {
if (!Array.isArray(cql2Value) || cql2Value.length === 0) {
throw new ValidationError("Operand for 'in' must be a non-empty array")
}
if (!cql2Value.every((x) => x !== Object(x))) {
throw new ValidationError(
"Operand for 'in' must contain only string, number, or boolean types"
)
}
return {
terms: {
[cql2Field]: cql2Value
}
}
}
function between(cql2Field, filterArgs) {
if (filterArgs.length < 3) {
throw new ValidationError("Two operands must be provided for the 'between' operator")
}
const cql2Value1 = filterArgs[1]
const cql2Value2 = filterArgs[2]
if (!(typeof cql2Value1 === 'number' && typeof cql2Value2 === 'number')) {
throw new ValidationError("Operands for 'between' must be numbers")
}
if (cql2Value1 > cql2Value2) {
throw new ValidationError(
"For the 'between' operator, the first operand must be less than or equal "
+ 'to the second operand'
)
}
return {
range: {
[cql2Field]: {
gte: cql2Value1,
lte: cql2Value2
}
}
}
}
function sIntersects(cql2Field, cql2Value) {
// cql2Value can be either:
// 1) { "bbox": [swLon, swLat, neLon, neLat] }
// 2) geojson geometry
let geom = null
if (cql2Value.bbox) {
geom = bboxToPolygon(cql2Value.bbox, true)
}
if (cql2Value.type && cql2Value.coordinates) {
if (!GEOMETRY_TYPES.includes(cql2Value.type)) {
throw new ValidationError(
`Operand for 's_intersects' must be a GeoJSON geometry: type was '${cql2Value.type}'`
)
}
geom = cql2Value
}
if (!geom) {
throw new ValidationError(
"Operand for 's_intersects' must be a bbox literal or GeoJSON geometry"
)
}
return {
geo_shape: {
[cql2Field]: { shape: geom }
}
}
}
function buildQueryExtQuery(query) {
const eq = 'eq'
const inop = 'in'
const startsWith = 'startsWith'
const endsWith = 'endsWith'
const contains = 'contains'
let filterQueries = []
// Using reduce rather than map as we don't currently support all
// stac query operators.
filterQueries = Object.keys(query).reduce((accumulator, property) => {
const operatorsObject = query[property]
const operators = Object.keys(operatorsObject)
// eq
if (operators.includes(eq)) {
accumulator.push({
term: {
[`properties.${property}`]: operatorsObject.eq
}
})
}
// in
if (operators.includes(inop)) {
accumulator.push({
terms: {
[`properties.${property}`]: operatorsObject.in
}
})
}
// startsWith
if (operators.includes(startsWith)) {
accumulator.push({
prefix: {
[`properties.${property}`]: {
value: operatorsObject.startsWith
}
}
})
}
// endsWith
if (operators.includes(endsWith)) {
accumulator.push({
wildcard: {
[`properties.${property}`]: {
value: `*${operatorsObject.endsWith}`
}
}
})
}
// contains
if (operators.includes(contains)) {
accumulator.push({
wildcard: {
[`properties.${property}`]: {
value: `*${operatorsObject.contains}*`
}
}
})
}
// lt, lte, gt, gte
const rangeQuery = buildRangeQuery(property, operators, operatorsObject)
if (rangeQuery) {
accumulator.push(rangeQuery)
}
return accumulator
}, filterQueries)
const neq = 'neq'
let mustNotQueries = []
mustNotQueries = Object.keys(query).reduce((accumulator, property) => {
const operatorsObject = query[property]
const operators = Object.keys(operatorsObject)
// neq
if (operators.includes(neq)) {
accumulator.push({
term: {
[`properties.${property}`]: operatorsObject.neq
}
})
}
return accumulator
}, mustNotQueries)
return {
bool: {
filter: filterQueries,
must_not: mustNotQueries
}
}
}
function buildFilterExtQuery(filter) {
let cql2Field = filter.args[0].property
if (!UNPREFIXED_FIELDS.includes(cql2Field)) {
cql2Field = `properties.${cql2Field}`
}
let cql2Value = filter.args[1]
if (typeof cql2Value === 'object' && cql2Value.timestamp) {
cql2Value = cql2Value.timestamp
}
switch (filter.op) {
// recursive cases
case OP.AND:
return {
bool: {
filter: filter.args.map(buildFilterExtQuery)
}
}
case OP.OR:
return {
bool: {
should: filter.args.map(buildFilterExtQuery),
minimum_should_match: 1
}
}
case OP.NOT:
return {
bool: {
must_not: filter.args.map(buildFilterExtQuery)
}
}
// direct cases
case OP.EQ:
return {
term: {
[cql2Field]: cql2Value
}
}
case OP.NEQ:
return {
bool: {
must_not: [
{
term: {
[cql2Field]: cql2Value
}
}
]
}
}
case OP.IS_NULL:
return {
bool: {
must_not: [
{
exists: {
field: cql2Field
}
}
]
}
}
// range cases
case OP.LT:
case OP.LTE:
case OP.GT:
case OP.GTE:
return {
range: {
[cql2Field]: {
[RANGE_TRANSLATION[filter.op]]: cql2Value
}
}
}
case OP.IN:
return IN(cql2Field, cql2Value)
case OP.BETWEEN:
return between(cql2Field, filter.args)
case OP.LIKE:
throw new ValidationError("The 'like' operator is not currently supported")
case OP.S_INTERSECTS:
return sIntersects(cql2Field, cql2Value)
// should not get here
default:
throw new Error(`Unknown filter operation: ${filter.op}`)
}
}
/**
* Hash function that converts a string into a hexadecimal string.
* A variant of the well-known "djb2" algorithm.
*
* @param {string} collection
* @returns {string} An 8-character hexadecimal hash string.
*/
function collectionHash(collection) {
let hash = 0
if (collection.length === 0) {
return '00000000'
}
for (let i = 0; i < collection.length; i += 1) {
const charCode = collection.charCodeAt(i)
hash = (hash << 5) - hash + charCode //eslint-disable-line no-bitwise
// converts to a 32-bit integer.
hash |= 0 //eslint-disable-line no-bitwise
}
// coonvert to hex string
return (hash >>> 0).toString(16).padStart(8, '0') //eslint-disable-line no-bitwise
}
/**
* Translates any collection ID into a fully unique ID to be
* used as index by OpenSearch
* Necessary because OpenSearch does not allow upper case letters in indicies.
* Using a short hash facilitates generating unique lower case IDs
* regardless of case
* @param {string} collection
* @returns {string} unique OpenSearch compatible string
*/
export function collectionUniqueIndexID(collection) {
return `${collection.toLowerCase()}-${collectionHash(collection)}`
}
function buildItemSearchQuery(parameters) {
const { intersects, collections, ids } = parameters
const filterQueries = []
if (ids) {
filterQueries.push({
terms: {
id: ids
}
})
}
if (collections) {
filterQueries.push({
terms: {
collection: collections
}
})
}
if (intersects) {
filterQueries.push({
geo_shape: {
geometry: { shape: intersects }
}
})
}
const datetimeQuery = buildDatetimeQuery(parameters)
if (datetimeQuery instanceof Error) {
throw datetimeQuery
} else if (datetimeQuery) {
filterQueries.push(datetimeQuery)
}
return {
bool: {
filter: filterQueries,
}
}
}
function buildOpenSearchQuery(parameters) {
const { query, filter, intersects, collections, ids } = parameters
let cql2Query = {}
let stacqlQuery = {}
let itemSearchQuery = {}
const osQuery = { bool: {} }
if (query) {
stacqlQuery = buildQueryExtQuery(query)
}
if (filter) {
cql2Query = buildFilterExtQuery(filter)
// non-recursive results can be bare
if (!cql2Query.bool) {
cql2Query = { bool: { filter: [cql2Query] } }
}
}
if (intersects || collections || ids) {
itemSearchQuery = buildItemSearchQuery(parameters)
}
const combinedFilter = [
...(cql2Query.bool?.filter || []),
...(stacqlQuery.bool?.filter || []),
...(itemSearchQuery.bool?.filter || [])
]
const combinedShould = [
...(cql2Query.bool?.should || []),
]
const combinedMustNot = [
...(cql2Query.bool?.must_not || []),
...(stacqlQuery.bool?.must_not || []),
]
if (!isEmpty(combinedFilter)) {
osQuery.bool.filter = combinedFilter
}
if (!isEmpty(combinedShould)) {
osQuery.bool.should = combinedShould
osQuery.bool.minimum_should_match = 1
}
if (!isEmpty(combinedMustNot)) {
osQuery.bool.must_not = combinedMustNot
}
if (isEmpty(osQuery.bool)) {
return { query: { match_all: {} } }
}
return { query: osQuery }
}
function buildIdQuery(id) {
return {
query: {
bool: {
filter: {
term: {
id: id
}
}
}
}
}
}
const DEFAULT_SORTING = [
{ 'properties.datetime': { order: 'desc' } },
{ id: { order: 'desc' } },
{ collection: { order: 'desc' } }
]
function buildSort(parameters) {
const { sortby } = parameters
if (sortby && sortby.length) {
return sortby.map((sortRule) => {
const { field, direction } = sortRule
return {
[field]: {
order: direction
}
}
})
}
return DEFAULT_SORTING
}
function buildSearchAfter(parameters) {
const { next } = parameters
if (next) {
return next.split(',')
}
return undefined
}
function fieldsParamIsEmpty(fieldsSpec, paramName) {
// Check if include or exclude is either:
// a. null
// b. an empty array
return fieldsSpec.hasOwnProperty(paramName)
&& (fieldsSpec[paramName] === null
|| (Array.isArray(fieldsSpec[paramName]) && !fieldsSpec[paramName].length))
}
export function buildFieldsFilter(parameters) {
const { fields } = parameters
let _sourceIncludes = []
let _sourceExcludes = []
if (parameters.hasOwnProperty('fields')) {
if (fieldsParamIsEmpty(fields, 'include')
&& fieldsParamIsEmpty(fields, 'exclude')) {
// if fields parameters are supplied but empty,
// start with this initial set, otherwise return all
_sourceIncludes = DEFAULT_FIELDS
} else if (fieldsParamIsEmpty(fields, 'include')) {
_sourceExcludes = fields.exclude
_sourceIncludes = DEFAULT_FIELDS.filter((item) => !_sourceExcludes.includes(item))
} else if (fieldsParamIsEmpty(fields, 'exclude')) {
_sourceIncludes = fields.include
} else {
const { include, exclude } = fields
// Add include fields to the source include list if they're not already in it
if (include && include.length > 0) {
include.forEach((field) => {
if (_sourceIncludes.indexOf(field) < 0) {
_sourceIncludes.push(field)
}
})
}
// Remove exclude fields from the default include list and add them to the source exclude list
if (exclude && exclude.length > 0) {
//_sourceIncludes = _sourceIncludes.filter((field) => !exclude.includes(field))
_sourceExcludes = exclude.filter((excludeField) =>
!_sourceIncludes.includes(excludeField)
&& !_sourceIncludes.some((includeField) => includeField.startsWith(`${excludeField}.`)))
}
}
}
return { _sourceIncludes, _sourceExcludes }
}
/*
* Create a new Collection
*
*/
async function indexCollection(collection) {
const client = await _client()
const exists = await client.indices.exists({ index: COLLECTIONS_INDEX })
if (!exists.body) {
await createIndex(COLLECTIONS_INDEX)
}
const idHash = collectionUniqueIndexID(collection.id)
// call the hash function
const collectionDocResponse = await client.index({
index: COLLECTIONS_INDEX,
id: collection.id,
body: collection,
opType: 'create'
})
const indexCreateResponse = await createIndex(idHash)
return [collectionDocResponse, indexCreateResponse]
}
/*
* Create a new Item in an index corresponding to the Collection
*
*/
async function indexItem(item) {
const client = await _client()
const hashedIndex = collectionUniqueIndexID(item.collection)
const exists = await client.indices.exists({ index: hashedIndex })
if (!exists.body) {
return new Error(`Index ${hashedIndex} does not exist, add before creating items`)
}
const now = new Date().toISOString()
Object.assign(item.properties, {
created: now,
updated: now
})
const response = await client.index({
index: hashedIndex,
id: item.id,
body: item,
opType: 'create'
})
return response
}
/*
*
* This conforms to a PATCH request and updates an existing item by ID
* using a partial item description, compliant with RFC 7386.
*
*/
async function partialUpdateItem(collectionId, itemId, updateFields) {
const client = await _client()
// Handle inserting required default properties to `updateFields`
const requiredProperties = {
updated: new Date().toISOString()
}
if (updateFields.properties) {
// If there are properties incoming, merge and overwrite our required ones.
Object.assign(updateFields.properties, requiredProperties)
} else {
updateFields.properties = requiredProperties
}
const response = await client.update({
index: collectionUniqueIndexID(collectionId),
id: itemId,
_source: true,
body: {
doc: updateFields
}
})
return response
}
async function deleteItem(collectionId, itemId) {
const client = await _client()
if (client === undefined) throw new Error('Client is undefined')
return await client.delete_by_query({
index: collectionUniqueIndexID(collectionId),
body: buildIdQuery(itemId),
waitForCompletion: true
})
}
async function dbQuery(parameters) {
logger.debug('Search query: %j', parameters)
const client = await _client()
if (client === undefined) throw new Error('Client is undefined')
const response = await client.search(parameters)
logger.debug('Response: %j', response)
return response
}
// get single collection
async function getCollection(collectionId) {
const response = await dbQuery({
index: COLLECTIONS_INDEX,
body: buildIdQuery(collectionId)
})
if (Array.isArray(response.body.hits.hits) && response.body.hits.hits.length) {
return response.body.hits.hits[0]._source
}
return new NotFoundError()
}
// get all collections
async function getCollections(page = 1, limit = 100) {
try {
const response = await dbQuery({
index: COLLECTIONS_INDEX,
size: limit,
from: (page - 1) * limit
})
return response.body.hits.hits.map((r) => (r._source))
} catch (e) {
logger.error('Failure getting collections, maybe none exist?', e)
return new Error('Collections not found. This is likely '
+ 'because the server has not been initialized with create_indices, '
+ 'cannot connect to the database, or cannot authenticate to the database.')
}
}
async function populateCollectionToIndexMapping() {
if (process.env['COLLECTION_TO_INDEX_MAPPINGS']) {
try {
collectionToIndexMapping = JSON.parse(process.env['COLLECTION_TO_INDEX_MAPPINGS'])
} catch (_) {
logger.error('COLLECTION_TO_INDEX_MAPPINGS is not a valid JSON object.')
collectionToIndexMapping = {}
}
} else {
collectionToIndexMapping = {}
}
}
async function indexForCollection(collectionId) {
return collectionToIndexMapping[collectionId] || collectionId
}
async function populateUnrestrictedIndices() {
if (!unrestrictedIndices) {
if (process.env['COLLECTION_TO_INDEX_MAPPINGS']) {
if (!collectionToIndexMapping) {
await populateCollectionToIndexMapping()
}
// When no collections are specified, the default index restriction
// is for all local indices (*, which excludes remote indices), excludes any
// system indices that start with a ".", the collections index, and then
// explicitly adds each of the remote indicies that have a mapping defined to them
unrestrictedIndices = DEFAULT_INDICES.concat(
Object.values(collectionToIndexMapping)
)
} else {
unrestrictedIndices = DEFAULT_INDICES
}
}
}
export async function constructSearchParams(parameters, page, limit) {
// console.log('DEBUG - parameters %j', parameters)
const { id, collections, filter } = parameters
let body
if (id) {
const params = { ids: [id] }
if (filter) {
params.filter = filter
}
body = buildOpenSearchQuery(params)
} else {
body = buildOpenSearchQuery(parameters)
body.sort = buildSort(parameters) // sort applied to the id query causes hang???
body.search_after = buildSearchAfter(parameters)
}
let indices
if (Array.isArray(collections) && collections.length) {
if (collections.length > MAX_COLLECTIONS_IN_QUERY_PATH) {
indices = ['_all']
} else if (process.env['COLLECTION_TO_INDEX_MAPPINGS']) {
if (!collectionToIndexMapping) await populateCollectionToIndexMapping()
indices = await Promise.all(collections.map(async (x) => await indexForCollection(x)))
} else {
indices = collections
}
} else {
if (!unrestrictedIndices) {
await populateUnrestrictedIndices()
}
indices = unrestrictedIndices
}
// hash indices
indices = indices.map((index) => {
if (DEFAULT_INDICES.includes(index) || index === '_all') {
return index
}
return collectionUniqueIndexID(index)
})
const searchParams = {
index: indices,
body,
size: limit,
track_total_hits: true
}
if (page !== undefined) {
searchParams.from = (page - 1) * limit
}
// disable fields filter for now
const { _sourceIncludes, _sourceExcludes } = buildFieldsFilter(parameters)
if (_sourceExcludes.length) {
searchParams._sourceExcludes = _sourceExcludes
}
if (_sourceIncludes.length) {
searchParams._sourceIncludes = _sourceIncludes
}
logger.debug('Search Params: %j', searchParams)
return searchParams
}
async function search(parameters, limit, page) {
const searchParams = await constructSearchParams(parameters, page, limit)
const dbResponse = await dbQuery({
ignore_unavailable: true,
allow_no_indices: true,
...searchParams
})
const hits = dbResponse.body.hits.hits
const results = hits.map((r) => (r._source))
const lastItem = hits.at(-1)
let lastItemSort = null
if (lastItem && lastItem.sort) {
lastItemSort = lastItem.sort.join(',')
}
const response = {
results,
numberMatched: dbResponse.body.hits.total.value,
numberReturned: results.length,
lastItemSort
}
return response
}
const ALL_AGGREGATIONS = {
total_count: { value_count: { field: 'id' } },
collection_frequency: { terms: { field: 'collection', size: 100 } },
platform_frequency: { terms: { field: 'properties.platform', size: 100 } },
cloud_cover_frequency: {
range: {
field: 'properties.eo:cloud_cover',
ranges: [
{ to: 5 },
{ from: 5, to: 15 },
{ from: 15, to: 40 },
{ from: 40 },
],
}
},
datetime_frequency: {
date_histogram: {
field: 'properties.datetime',
calendar_interval: 'month',
}
},
datetime_min: { min: { field: 'properties.datetime' } },
datetime_max: { max: { field: 'properties.datetime' } },
grid_code_frequency: {
terms: {
field: 'properties.grid:code',
missing: 'none',
size: 10000,
}
},
sun_elevation_frequency: {
histogram: {
field: 'properties.view:sun_elevation',
interval: 5
}
},
sun_azimuth_frequency: {
histogram: {
field: 'properties.view:sun_azimuth',
interval: 5
}
},
off_nadir_frequency: {
histogram: {
field: 'properties.view:off_nadir',
interval: 5
}
}
}
/**
* @param {Array} aggregations
* @param {Object} parameters
* @param {number} geohashPrecision
* @param {number} geohexPrecision
* @param {number} geotilePrecision
* @param {any} centroidGeohashGridPrecision
* @param {any} centroidGeohexGridPrecision
* @param {any} centroidGeotileGridPrecision
* @param {any} geometryGeohashGridPrecision
* @param {any} geometryGeotileGridPrecision
* @returns {Promise<Object>}
*/
async function aggregate(
aggregations, parameters,
geohashPrecision, geohexPrecision, geotilePrecision,
centroidGeohashGridPrecision,
centroidGeohexGridPrecision,
centroidGeotileGridPrecision,
geometryGeohashGridPrecision,
// geometryGeohexGridPrecision,
geometryGeotileGridPrecision,
) {
const searchParams = await constructSearchParams(parameters)
searchParams.body.size = 0
logger.debug('Aggregations: %j', aggregations)
// include all aggregations specified
// this will ignore aggregations with the wrong names
searchParams.body.aggs = Object.keys(ALL_AGGREGATIONS).reduce((o, k) => {
if (aggregations.includes(k)) o[k] = ALL_AGGREGATIONS[k]
return o
}, {})
// deprecated centroid
if (aggregations.includes('grid_geohash_frequency')) {
searchParams.body.aggs.grid_geohash_frequency = {
geohash_grid: {
field: 'properties.proj:centroid',
precision: geohashPrecision
}
}
}
if (aggregations.includes('grid_geohex_frequency')) {
searchParams.body.aggs.grid_geohex_frequency = {
geohex_grid: {
field: 'properties.proj:centroid',
precision: geohexPrecision
}
}
}
if (aggregations.includes('grid_geotile_frequency')) {
searchParams.body.aggs.grid_geotile_frequency = {
geotile_grid: {
field: 'properties.proj:centroid',
precision: geotilePrecision
}
}
}
// centroid
if (aggregations.includes('centroid_geohash_grid_frequency')) {
searchParams.body.aggs.centroid_geohash_grid_frequency = {
geohash_grid: {
field: 'properties.proj:centroid',
precision: centroidGeohashGridPrecision
}
}
}
if (aggregations.includes('centroid_geohex_grid_frequency')) {
searchParams.body.aggs.centroid_geohex_grid_frequency = {
geohex_grid: {