Skip to content

Commit d6b6e3b

Browse files
CCET-768: add track total hits to os-query lib (#15)
1 parent 1783ebb commit d6b6e3b

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

search.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ import (
1515
// Not all features of the search API are currently supported, but a request can
1616
// currently include a query, aggregations, and more.
1717
type SearchRequest struct {
18-
aggs []Aggregation
19-
explain *bool
20-
from *uint64
21-
highlight Mappable
22-
searchAfter []interface{}
23-
postFilter Mappable
24-
query Mappable
25-
size *uint64
26-
sorts Sorts
27-
source Source
28-
timeout *time.Duration
29-
collapse *CollapseRequest
18+
aggs []Aggregation
19+
explain *bool
20+
from *uint64
21+
highlight Mappable
22+
searchAfter []interface{}
23+
postFilter Mappable
24+
query Mappable
25+
size *uint64
26+
sorts Sorts
27+
source Source
28+
timeout *time.Duration
29+
collapse *CollapseRequest
30+
trackTotalHits any
3031
}
3132

3233
// Search creates a new SearchRequest object, to be filled via method chaining.
@@ -84,6 +85,12 @@ func (req *SearchRequest) Explain(b bool) *SearchRequest {
8485
return req
8586
}
8687

88+
// TrackTotalHits sets whether the search should count the total number of hits past the limit. Supported values is a number [range] or a bool
89+
func (req *SearchRequest) TrackTotalHits(b any) *SearchRequest {
90+
req.trackTotalHits = b
91+
return req
92+
}
93+
8794
// Timeout sets a timeout for the request.
8895
func (req *SearchRequest) Timeout(dur time.Duration) *SearchRequest {
8996
req.timeout = &dur
@@ -163,6 +170,9 @@ func (req *SearchRequest) Map() map[string]interface{} {
163170
if req.collapse != nil {
164171
m["collapse"] = req.collapse.Map()
165172
}
173+
if req.trackTotalHits != nil {
174+
m["track_total_hits"] = req.trackTotalHits
175+
}
166176

167177
if req.source.disabled {
168178
m["_source"] = false

search_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ func TestSearchMaps(t *testing.T) {
7777
).
7878
SourceIncludes("field_1", "field_2").
7979
SourceExcludes("field_3").
80-
Timeout(time.Duration(20000000000)),
80+
Timeout(time.Duration(20000000000)).
81+
TrackTotalHits(false),
8182
map[string]interface{}{
8283
"query": map[string]interface{}{
8384
"bool": map[string]interface{}{
@@ -138,6 +139,7 @@ func TestSearchMaps(t *testing.T) {
138139
"includes": []string{"field_1", "field_2"},
139140
"excludes": []string{"field_3"},
140141
},
142+
"track_total_hits": false,
141143
},
142144
},
143145
{

0 commit comments

Comments
 (0)