-
Notifications
You must be signed in to change notification settings - Fork 13
/
search.go
253 lines (211 loc) · 7.01 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package kibana
import (
"encoding/json"
)
const (
Ascending SortOrder = iota
Descending
)
// Enums for searchReferencesType
const (
SearchReferencesTypeIndexPattern searchReferencesType = "index-pattern"
)
type searchReferencesType string
func (r searchReferencesType) String() string {
return string(r)
}
type SortOrder int
type SearchSourceBuilderFactory interface {
NewSearchSource() SearchSourceBuilder
}
type SearchClient interface {
Create(request *CreateSearchRequest) (*Search, error)
Update(id string, request *UpdateSearchRequest) (*Search, error)
GetById(id string) (*Search, error)
List() ([]*Search, error)
Delete(id string) error
NewSearchSource() SearchSourceBuilder
Version() string
}
type CreateSearchRequest struct {
Attributes *SearchAttributes `json:"attributes"`
References []*SearchReferences `json:"references,omitempty"`
}
type UpdateSearchRequest struct {
Attributes *SearchAttributes `json:"attributes"`
References []*SearchReferences `json:"references,omitempty"`
}
type Search struct {
Id string `json:"id"`
Type string `json:"type"`
Version version `json:"version"`
Attributes *SearchAttributes `json:"attributes"`
References []*SearchReferences `json:"references,omitempty"`
}
type SearchReferences struct {
Name string `json:"name"`
Type searchReferencesType `json:"type"`
Id string `json:"id"`
}
type SearchAttributes struct {
Title string `json:"title"`
Description string `json:"description"`
Hits int `json:"hits"`
Columns []string `json:"columns"`
Sort Sort `json:"sort"`
Version int `json:"version"`
KibanaSavedObjectMeta *SearchKibanaSavedObjectMeta `json:"kibanaSavedObjectMeta"`
}
// Sort allows unmarshalling different json structure for the sort field. In
// newer version of Kibana this can be a nested JSON array
// (https://github.com/elastic/kibana/pull/41918/), while in the older versions
// it is a flat JSON array.
type Sort []string
// UnmarshalJSON tries to unmarshal the json data first into a nested array and if that fails it will try to unmarshal into a slice of string
func (s *Sort) UnmarshalJSON(data []byte) error {
var nestedSlice [][]string
err := json.Unmarshal(data, &nestedSlice)
if err != nil {
var slice []string
err = json.Unmarshal(data, &slice)
if err != nil {
return err
}
*s = slice
}
// Successfully unmarshalled into a nested slice, which will now need to be
// flatten into a slice of string
if len(nestedSlice) > 0 && len(nestedSlice[0]) == 2 {
// The sort order for the old format will be
// taken from the first element of the new format
sortOrder := nestedSlice[0][1]
for _, sort := range nestedSlice {
*s = append(*s, sort[0])
}
*s = append(*s, sortOrder)
}
return nil
}
type searchReadResult553 struct {
Id string `json:"_id"`
Type string `json:"_type"`
Version version `json:"_version"`
Source *SearchAttributes `json:"_source"`
}
type SearchKibanaSavedObjectMeta struct {
SearchSourceJSON string `json:"searchSourceJSON"`
}
type SearchSource struct {
IndexId string `json:"index"`
IndexRefName string `json:"indexRefName"`
HighlightAll bool `json:"highlightAll"`
Version bool `json:"version"`
Query interface{} `json:"query,omitempty"`
Filter []*SearchFilter `json:"filter"`
}
type SearchQuery600 struct {
Query string `json:"query"`
Language string `json:"language"`
}
type SearchQuery553 struct {
QueryString *searchQueryString `json:"query_string"`
}
type searchQueryString struct {
Query string `json:"query"`
Analyze bool `json:"analyze_wildcard"`
}
type SearchFilter struct {
Query *SearchFilterQuery `json:"query"`
Exists *SearchFilterExists `json:"exists"`
Meta *SearchFilterMetaData `json:"meta,omitempty"`
}
type SearchFilterQuery struct {
Match map[string]*SearchFilterQueryAttributes `json:"match"`
}
type SearchFilterExists struct {
Field string `json:"field"`
}
type SearchFilterMetaData struct {
Index string `json:"index"`
IndexRefName string `json:"indexRefName"`
Negate bool `json:"negate"`
Disabled bool `json:"disabled"`
Alias string `json:"alias"`
Type string `json:"type"`
Key string `json:"key"`
Value string `json:"value"`
Params *SearchFilterQueryAttributes `json:"params"`
}
type SearchFilterQueryAttributes struct {
Query string `json:"query"`
Type string `json:"type"`
}
type SearchRequestBuilder struct {
title string
description string
displayColumns []string
sortColumns []string
searchSource *SearchSource
references []*SearchReferences
}
type SearchSourceBuilder interface {
WithIndexId(indexId string) SearchSourceBuilder
WithIndexRefName(indexRefName string) SearchSourceBuilder
WithQuery(query string) SearchSourceBuilder
WithFilter(filter *SearchFilter) SearchSourceBuilder
Build() (*SearchSource, error)
}
func NewSearchRequestBuilder() *SearchRequestBuilder {
return &SearchRequestBuilder{}
}
func (builder *SearchRequestBuilder) WithTitle(title string) *SearchRequestBuilder {
builder.title = title
return builder
}
func (builder *SearchRequestBuilder) WithDescription(description string) *SearchRequestBuilder {
builder.description = description
return builder
}
func (builder *SearchRequestBuilder) WithDisplayColumns(columns []string) *SearchRequestBuilder {
builder.displayColumns = columns
return builder
}
func (builder *SearchRequestBuilder) WithSortColumns(columns []string, order SortOrder) *SearchRequestBuilder {
var sortOrder = ""
if order == Descending {
sortOrder = "desc"
} else {
sortOrder = "asc"
}
builder.sortColumns = append(columns, sortOrder)
return builder
}
func (builder *SearchRequestBuilder) WithSearchSource(searchSource *SearchSource) *SearchRequestBuilder {
builder.searchSource = searchSource
return builder
}
func (builder *SearchRequestBuilder) WithReferences(refs []*SearchReferences) *SearchRequestBuilder {
builder.references = refs
return builder
}
func (builder *SearchRequestBuilder) Build() (*CreateSearchRequest, error) {
searchSourceBytes, err := json.Marshal(builder.searchSource)
if err != nil {
return nil, err
}
request := &CreateSearchRequest{
Attributes: &SearchAttributes{
Title: builder.title,
Description: builder.description,
Hits: 0,
Columns: builder.displayColumns,
Sort: builder.sortColumns,
Version: 1,
KibanaSavedObjectMeta: &SearchKibanaSavedObjectMeta{
SearchSourceJSON: string(searchSourceBytes),
},
},
References: builder.references,
}
return request, nil
}