File tree 4 files changed +65
-0
lines changed
4 files changed +65
-0
lines changed Original file line number Diff line number Diff line change
1
+ package query
2
+
3
+ // CollapseRequest represents the "collapse" param that can be applied to a request
4
+ // see: https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html
5
+ type CollapseRequest struct {
6
+ field string
7
+ }
8
+
9
+ // Collapse creates a new collapse request
10
+ func Collapse (field string ) * CollapseRequest {
11
+ return & CollapseRequest {
12
+ field : field ,
13
+ }
14
+ }
15
+
16
+ // Map returns a map representation of the Source object.
17
+ func (source CollapseRequest ) Map () map [string ]interface {} {
18
+ return map [string ]interface {}{
19
+ "field" : source .field ,
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package query
2
+
3
+ import (
4
+ "testing"
5
+ )
6
+
7
+ func TestCollapse_Map (t * testing.T ) {
8
+ runMapTests (
9
+ t ,
10
+ []mapTest {
11
+ {
12
+ name : "collapse" ,
13
+ q : Collapse ("collapse_field" ),
14
+ exp : map [string ]interface {}{
15
+ "field" : "collapse_field" ,
16
+ },
17
+ },
18
+ },
19
+ )
20
+ }
Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ type SearchRequest struct {
26
26
sorts Sorts
27
27
source Source
28
28
timeout * time.Duration
29
+ collapse * CollapseRequest
29
30
}
30
31
31
32
// Search creates a new SearchRequest object, to be filled via method chaining.
@@ -107,6 +108,13 @@ func (req *SearchRequest) Highlight(highlight Mappable) *SearchRequest {
107
108
return req
108
109
}
109
110
111
+ // Collapse sets the collapse param for the request.
112
+ // See:https://www.elastic.co/guide/en/elasticsearch/reference/current/collapse-search-results.html
113
+ func (req * SearchRequest ) Collapse (collapse * CollapseRequest ) * SearchRequest {
114
+ req .collapse = collapse
115
+ return req
116
+ }
117
+
110
118
// Map implements the Mappable interface. It converts the request to into a
111
119
// nested map[string]interface{}, as expected by the go-elasticsearch library.
112
120
func (req * SearchRequest ) Map () map [string ]interface {} {
@@ -146,6 +154,9 @@ func (req *SearchRequest) Map() map[string]interface{} {
146
154
if req .searchAfter != nil {
147
155
m ["search_after" ] = req .searchAfter
148
156
}
157
+ if req .collapse != nil {
158
+ m ["collapse" ] = req .collapse .Map ()
159
+ }
149
160
150
161
source := req .source .Map ()
151
162
if len (source ) > 0 {
Original file line number Diff line number Diff line change @@ -132,5 +132,18 @@ func TestSearchMaps(t *testing.T) {
132
132
},
133
133
},
134
134
},
135
+ {
136
+ "a simple match_all query with a size and collapse" ,
137
+ Search ().Query (MatchAll ()).Size (20 ).Collapse (& CollapseRequest {field : "collapse_field" }),
138
+ map [string ]interface {}{
139
+ "query" : map [string ]interface {}{
140
+ "match_all" : map [string ]interface {}{},
141
+ },
142
+ "collapse" : map [string ]interface {}{
143
+ "field" : "collapse_field" ,
144
+ },
145
+ "size" : 20 ,
146
+ },
147
+ },
135
148
})
136
149
}
You can’t perform that action at this time.
0 commit comments