Skip to content

Commit

Permalink
CBB-1079: add source disable
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmitharoo committed Aug 27, 2024
1 parent b248120 commit 4c55d76
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 5 deletions.
16 changes: 14 additions & 2 deletions aggs_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ func (agg *TopHitsAgg) Sorts(sorts ...map[string]Sort) *TopHitsAgg {
return agg
}

// SourceDisabled when set will prevent source fields returning
func (agg *TopHitsAgg) SourceDisabled(b bool) *TopHitsAgg {
agg.source.disabled = b
return agg
}

// SourceIncludes sets the keys to return from the top matching documents.
func (agg *TopHitsAgg) SourceIncludes(keys ...string) *TopHitsAgg {
agg.source.includes = keys
Expand All @@ -472,8 +478,14 @@ func (agg *TopHitsAgg) Map() map[string]interface{} {
if len(agg.sorts) > 0 {
innerMap["sort"] = agg.sorts
}
if len(agg.source.includes) > 0 {
innerMap["_source"] = agg.source.Map()

if agg.source.disabled {
innerMap["_source"] = false
} else {
source := agg.source.Map()
if len(source) > 0 {
innerMap["_source"] = source
}
}

return map[string]interface{}{
Expand Down
36 changes: 36 additions & 0 deletions aggs_metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,41 @@ func TestMetricAggs(t *testing.T) {
},
},
},
{
"top_hits agg with no source",
TopHits("top_hits").
Sorts(
Sorts{
{
"field_1": {
Order: OrderDesc,
},
},
{
"field_2": {
Order: OrderAsc,
},
},
}...,
).
SourceDisabled(true),
map[string]interface{}{
"top_hits": map[string]interface{}{
"sort": []map[string]interface{}{
{
"field_1": map[string]interface{}{
"order": OrderDesc,
},
},
{
"field_2": map[string]interface{}{
"order": OrderAsc,
},
},
},
"_source": false,
},
},
},
})
}
1 change: 1 addition & 0 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package query
type Source struct {
includes []string
excludes []string
disabled bool
}

// Map returns a map representation of the Source object.
Expand Down
16 changes: 13 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ func (req *SearchRequest) Timeout(dur time.Duration) *SearchRequest {
return req
}

// SourceDisabled when set will prevent source fields returning
func (req *SearchRequest) SourceDisabled(b bool) *SearchRequest {
req.source.disabled = b
return req
}

// SourceIncludes sets the keys to return from the matching documents.
func (req *SearchRequest) SourceIncludes(keys ...string) *SearchRequest {
req.source.includes = keys
Expand Down Expand Up @@ -158,9 +164,13 @@ func (req *SearchRequest) Map() map[string]interface{} {
m["collapse"] = req.collapse.Map()
}

source := req.source.Map()
if len(source) > 0 {
m["_source"] = source
if req.source.disabled {
m["_source"] = false
} else {
source := req.source.Map()
if len(source) > 0 {
m["_source"] = source
}
}

return m
Expand Down
8 changes: 8 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func TestSearchMaps(t *testing.T) {
"size": 20,
},
},
{
"a simple query with no source",
Search().SearchAfter("_id", "name").SourceDisabled(true),
map[string]interface{}{
"search_after": []string{"_id", "name"},
"_source": false,
},
},
{
"a complex query with an aggregation and various other options",
Search().
Expand Down

0 comments on commit 4c55d76

Please sign in to comment.