forked from ewilde/go-kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
186 lines (155 loc) · 4.96 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
package kibana
import (
"encoding/json"
)
const (
Ascending SortOrder = iota
Descending
)
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)
Delete(id string) error
NewSearchSource() SearchSourceBuilder
Version() string
}
type CreateSearchRequest struct {
Attributes *SearchAttributes `json:"attributes"`
}
type UpdateSearchRequest struct {
Attributes *SearchAttributes `json:"attributes"`
}
type Search struct {
Id string `json:"id"`
Type string `json:"type"`
Version version `json:"version"`
Attributes *SearchAttributes `json:"attributes"`
}
type SearchAttributes struct {
Title string `json:"title"`
Description string `json:"description"`
Hits int `json:"hits"`
Columns []string `json:"columns"`
Sort []string `json:"sort"`
Version int `json:"version"`
KibanaSavedObjectMeta *SearchKibanaSavedObjectMeta `json:"kibanaSavedObjectMeta"`
}
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"`
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"`
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
}
type SearchSourceBuilder interface {
WithIndexId(indexId 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) 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),
},
},
}
return request, nil
}