@@ -90,6 +90,11 @@ type ElasticHits struct {
90
90
} `json:"inner_hits"`
91
91
} `json:"hits"`
92
92
} `json:"hits"`
93
+ Aggregations struct {
94
+ Distinct struct {
95
+ Total int `json:"value"`
96
+ } `json:"distinct_mapset_ids"`
97
+ } `json:"aggregations"`
93
98
}
94
99
95
100
// IndexElasticSearchMapset Indexes an individual mapset in elastic
@@ -256,7 +261,7 @@ func IndexAllElasticSearchMapsets(deletePrevious bool) error {
256
261
}
257
262
258
263
// SearchElasticMapsets Searches ElasticSearch for mapsets
259
- func SearchElasticMapsets (options * ElasticMapsetSearchOptions ) ([]* Mapset , error ) {
264
+ func SearchElasticMapsets (options * ElasticMapsetSearchOptions ) ([]* Mapset , int , error ) {
260
265
boolQuery := BoolQuery {}
261
266
262
267
if options .Search != "" {
@@ -345,12 +350,19 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, error
345
350
{"_score" : {Order : "desc" }},
346
351
{"date_last_updated" : {Order : "desc" }},
347
352
},
353
+ Aggs : map [string ]interface {}{
354
+ "distinct_mapset_ids" : map [string ]interface {}{
355
+ "cardinality" : map [string ]interface {}{
356
+ "field" : "mapset_id" ,
357
+ },
358
+ },
359
+ },
348
360
}
349
361
350
362
queryJSON , err := json .Marshal (query )
351
363
352
364
if err != nil {
353
- return nil , errors .New (fmt .Sprintf ("Error marshaling the query: %s" , err ))
365
+ return nil , 0 , errors .New (fmt .Sprintf ("Error marshaling the query: %s" , err ))
354
366
}
355
367
356
368
resp , err := ElasticSearch .Search (
@@ -359,21 +371,21 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, error
359
371
)
360
372
361
373
if err != nil {
362
- return nil , err
374
+ return nil , 0 , err
363
375
}
364
376
365
377
defer resp .Body .Close ()
366
378
367
379
body , err := io .ReadAll (resp .Body )
368
380
369
381
if err != nil {
370
- return nil , err
382
+ return nil , 0 , err
371
383
}
372
384
373
385
var hits ElasticHits
374
386
375
387
if err := json .Unmarshal (body , & hits ); err != nil {
376
- return nil , err
388
+ return nil , 0 , err
377
389
}
378
390
379
391
var mapsets = make ([]* Mapset , 0 )
@@ -406,5 +418,5 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, error
406
418
}
407
419
}
408
420
409
- return mapsets , nil
421
+ return mapsets , hits . Aggregations . Distinct . Total , nil
410
422
}
0 commit comments