Skip to content

Commit

Permalink
CBB-1170: add constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesmitharoo committed Aug 27, 2024
1 parent 2490b6b commit f9f5056
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
13 changes: 10 additions & 3 deletions collapse.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package query

// Collapse represents the "collapse" param that can be applied to a request
// CollapseRequest represents the "collapse" param that can be applied to a request
// see: https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html
type Collapse struct {
type CollapseRequest struct {
field string
}

// Collapse creates a new collapse request
func Collapse(field string) *CollapseRequest {
return &CollapseRequest{
field: field,
}
}

// Map returns a map representation of the Source object.
func (source Collapse) Map() map[string]interface{} {
func (source CollapseRequest) Map() map[string]interface{} {
return map[string]interface{}{
"field": source.field,
}
Expand Down
4 changes: 1 addition & 3 deletions collapse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ func TestCollapse_Map(t *testing.T) {
[]mapTest{
{
name: "collapse",
q: Collapse{
field: "collapse_field",
},
q: Collapse("collapse_field"),
exp: map[string]interface{}{
"field": "collapse_field",
},
Expand Down
4 changes: 2 additions & 2 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type SearchRequest struct {
sorts Sorts
source Source
timeout *time.Duration
collapse *Collapse
collapse *CollapseRequest
}

// Search creates a new SearchRequest object, to be filled via method chaining.
Expand Down Expand Up @@ -110,7 +110,7 @@ func (req *SearchRequest) Highlight(highlight Mappable) *SearchRequest {

// Collapse sets the collapse param for the request.
// See:https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html
func (req *SearchRequest) Collapse(collapse *Collapse) *SearchRequest {
func (req *SearchRequest) Collapse(collapse *CollapseRequest) *SearchRequest {
req.collapse = collapse
return req
}
Expand Down
2 changes: 1 addition & 1 deletion search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func TestSearchMaps(t *testing.T) {
},
{
"a simple match_all query with a size and collapse",
Search().Query(MatchAll()).Size(20).Collapse(&Collapse{field: "collapse_field"}),
Search().Query(MatchAll()).Size(20).Collapse(&CollapseRequest{field: "collapse_field"}),
map[string]interface{}{
"query": map[string]interface{}{
"match_all": map[string]interface{}{},
Expand Down

0 comments on commit f9f5056

Please sign in to comment.