Skip to content

Commit a98b984

Browse files
committed
Return total mapsets found
1 parent b98640b commit a98b984

File tree

3 files changed

+21
-8
lines changed

3 files changed

+21
-8
lines changed

db/elasticsearch_mapsets.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ type ElasticHits struct {
9090
} `json:"inner_hits"`
9191
} `json:"hits"`
9292
} `json:"hits"`
93+
Aggregations struct {
94+
Distinct struct {
95+
Total int `json:"value"`
96+
} `json:"distinct_mapset_ids"`
97+
} `json:"aggregations"`
9398
}
9499

95100
// IndexElasticSearchMapset Indexes an individual mapset in elastic
@@ -256,7 +261,7 @@ func IndexAllElasticSearchMapsets(deletePrevious bool) error {
256261
}
257262

258263
// SearchElasticMapsets Searches ElasticSearch for mapsets
259-
func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, error) {
264+
func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, int, error) {
260265
boolQuery := BoolQuery{}
261266

262267
if options.Search != "" {
@@ -345,12 +350,19 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, error
345350
{"_score": {Order: "desc"}},
346351
{"date_last_updated": {Order: "desc"}},
347352
},
353+
Aggs: map[string]interface{}{
354+
"distinct_mapset_ids": map[string]interface{}{
355+
"cardinality": map[string]interface{}{
356+
"field": "mapset_id",
357+
},
358+
},
359+
},
348360
}
349361

350362
queryJSON, err := json.Marshal(query)
351363

352364
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))
354366
}
355367

356368
resp, err := ElasticSearch.Search(
@@ -359,21 +371,21 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, error
359371
)
360372

361373
if err != nil {
362-
return nil, err
374+
return nil, 0, err
363375
}
364376

365377
defer resp.Body.Close()
366378

367379
body, err := io.ReadAll(resp.Body)
368380

369381
if err != nil {
370-
return nil, err
382+
return nil, 0, err
371383
}
372384

373385
var hits ElasticHits
374386

375387
if err := json.Unmarshal(body, &hits); err != nil {
376-
return nil, err
388+
return nil, 0, err
377389
}
378390

379391
var mapsets = make([]*Mapset, 0)
@@ -406,5 +418,5 @@ func SearchElasticMapsets(options *ElasticMapsetSearchOptions) ([]*Mapset, error
406418
}
407419
}
408420

409-
return mapsets, nil
421+
return mapsets, hits.Aggregations.Distinct.Total, nil
410422
}

db/elasticsearch_objects.go

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type Query struct {
66
Collapse Collapse `json:"collapse,omitempty"`
77
Query BoolQuery `json:"query,omitempty"`
88
Sort []map[string]SortOrder `json:"sort,omitempty"`
9+
Aggs interface{} `json:"aggs"`
910
}
1011

1112
type Collapse struct {

handlers/mapsets.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -352,12 +352,12 @@ func GetMapsetsSearch(c *gin.Context) *APIError {
352352
body.Limit = 50
353353
}
354354

355-
mapsets, err := db.SearchElasticMapsets(body)
355+
mapsets, total, err := db.SearchElasticMapsets(body)
356356

357357
if err != nil {
358358
return APIErrorServerError("Error retrieving mapsets from elastic search", err)
359359
}
360360

361-
c.JSON(http.StatusOK, gin.H{"mapsets": mapsets})
361+
c.JSON(http.StatusOK, gin.H{"total": total, "mapsets": mapsets})
362362
return nil
363363
}

0 commit comments

Comments
 (0)