This repository has been archived by the owner on May 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fts_index.go
356 lines (313 loc) · 9.37 KB
/
fts_index.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package bucket
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"github.com/volatiletech/null"
)
const (
ftsEndpoint = "/_p/fts/api/index"
statAddress = "http://localhost:8094"
)
type apiResponse struct {
Status string `json:"status"`
IndexDefs IndexDefs `json:"indexDefs"`
Error string `json:"error"`
}
// IndexDefs ...
type IndexDefs struct {
UUID string `json:"uuid"`
IndexDefs map[string]IndexDefinition `json:"indexDefs"`
}
// IndexDefinition ...
type IndexDefinition struct {
Type string `json:"type"`
Name string `json:"name"`
UUID string `json:"uuid"`
SourceType string `json:"sourceType"`
SourceName string `json:"sourceName"`
SourceUUID string `json:"sourceUUID"`
SourceParams interface{} `json:"sourceParams"` // TODO
PlanParams IndexPlanParams `json:"planParams"`
Params IndexParams `json:"params"`
}
// IndexPlanParams ...
type IndexPlanParams struct {
MaxPartitionsPerPIndex int64 `json:"maxPartitionsPerPIndex"`
NumReplicas int64 `json:"numReplicas"`
}
// IndexParams ...
type IndexParams struct {
DocConfig IndexDocConfig `json:"doc_config"`
Mapping IndexMapping `json:"mapping"`
Store IndexStore `json:"store"`
}
// IndexDocConfig ...
type IndexDocConfig struct {
DocIDPrefixDelimiter string `json:"docid_prefix_delim"`
DocIDRegexp string `json:"docid_regexp"`
Mode string `json:"mode"`
TypeField string `json:"type_field"`
}
// IndexMapping ...
type IndexMapping struct {
DefaultAnalyzer string `json:"default_analyzer"`
DefaultDatetimeParser string `json:"default_datetime_parser"`
DefaultField string `json:"default_field"`
DefaultMapping IndexDefaultMapping `json:"default_mapping"`
DefaultType string `json:"default_type"`
DocvaluesDynamic bool `json:"docvalues_dynamic"`
IndexDynamic bool `json:"index_dynamic"`
StoreDynamic bool `json:"store_dynamic"`
TypeField string `json:"type_field"`
Types map[string]IndexType `json:"types"`
}
// IndexDefaultMapping ...
type IndexDefaultMapping struct {
Dynamic bool `json:"dynamic"`
Enabled bool `json:"enabled"`
}
// IndexType ...
type IndexType struct {
Dynamic bool `json:"dynamic"`
Enabled bool `json:"enabled"`
DefaultAnalyzer string `json:"default_analyzer,omitempty"`
Properties map[string]IndexProperties `json:"properties"`
}
// IndexProperties ...
type IndexProperties struct {
Dynamic bool `json:"dynamic"`
Enabled bool `json:"enabled"`
Fields []IndexField `json:"fields"`
}
// IndexField ...
type IndexField struct {
Analyzer string `json:"analyzer"`
IncludeInAll bool `json:"include_in_all"`
IncludeTermVectors bool `json:"include_term_vectors"`
Index bool `json:"index"`
Name string `json:"name"`
Store bool `json:"store"`
Type string `json:"type"`
}
// IndexStore ...
type IndexStore struct {
IndexType string `json:"indexType"`
KVStoreName string `json:"kvStoreName"`
}
// IndexMeta ...
type IndexMeta struct {
Name string
SourceType string
SourceName string
DocIDPrefixDelimiter string
DocIDRegexp string
TypeField string
}
// IndexCount represents index count response
type IndexCount struct {
Status string `json:"status"`
Count null.Uint `json:"count,omitempty"`
Error null.String `json:"error,omitempty"`
Request null.String `json:"request,omitempty"`
}
// IndexStat represents the statistics of the search index
type IndexStat struct {
Status null.String `json:"status,omitempty"`
Error null.String `json:"error,omitempty"`
Request null.String `json:"request,omitempty"`
AggStats null.JSON `json:"aggStats,omitempty"`
DocCount null.Uint `json:"docCount,omitempty"`
NodesStats null.JSON `json:"nodesStats"`
}
// DefaultFullTextSearchIndexDefinition creates a default index def
// for full-text search and return it in purpose to change default
// values manually
func DefaultFullTextSearchIndexDefinition(meta IndexMeta) (*IndexDefinition, error) {
if meta.Name == "" {
return nil, ErrEmptyIndex
}
if meta.SourceType == "" {
return nil, ErrEmptyType
}
if meta.SourceName == "" {
return nil, ErrEmptySource
}
var ftsDef = &IndexDefinition{
Type: "fulltext-index",
Name: meta.Name,
SourceType: meta.SourceType,
SourceName: meta.SourceName,
PlanParams: IndexPlanParams{
MaxPartitionsPerPIndex: 171,
},
Params: IndexParams{
Mapping: IndexMapping{
DefaultAnalyzer: "standard",
DefaultDatetimeParser: "dateTimeOptional",
DefaultField: "_all",
DefaultMapping: IndexDefaultMapping{
Dynamic: false,
Enabled: false,
},
DefaultType: "_default",
DocvaluesDynamic: true,
IndexDynamic: true,
StoreDynamic: true,
TypeField: "_type",
},
Store: IndexStore{
IndexType: "scorch",
KVStoreName: "",
},
},
}
switch {
case meta.DocIDPrefixDelimiter != "":
ftsDef.Params.DocConfig = IndexDocConfig{
DocIDPrefixDelimiter: meta.DocIDPrefixDelimiter,
Mode: "docid_prefix",
DocIDRegexp: "",
TypeField: "type",
}
case meta.DocIDRegexp != "":
ftsDef.Params.DocConfig = IndexDocConfig{
DocIDPrefixDelimiter: "",
Mode: "docid_regexp",
DocIDRegexp: meta.DocIDRegexp,
TypeField: "type",
}
case meta.TypeField != "":
ftsDef.Params.DocConfig = IndexDocConfig{
DocIDPrefixDelimiter: "",
Mode: "type_field",
DocIDRegexp: "",
TypeField: meta.TypeField,
}
}
return ftsDef, nil
}
// CreateFullTextSearchIndex ...
func (h *Handler) CreateFullTextSearchIndex(ctx context.Context, def *IndexDefinition) error {
body, err := json.Marshal(def)
if err != nil {
return err
}
req, _ := http.NewRequest("PUT", h.fullTextSearchURL(ctx, def.Name), bytes.NewBuffer(body))
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")
resp, err := h.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var ar apiResponse
if err := json.Unmarshal(respbody, &ar); err != nil {
return err
}
if ar.Status == "fail" {
return errors.New(ar.Error)
}
return nil
}
// DeleteFullTextSearchIndex ...
func (h *Handler) DeleteFullTextSearchIndex(ctx context.Context, indexName string) error {
req, _ := http.NewRequest("DELETE", h.fullTextSearchURL(ctx, indexName), nil)
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")
resp, err := h.http.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
var ar apiResponse
err = json.Unmarshal(respbody, &ar)
if err != nil {
return err
}
if ar.Status == "fail" {
return errors.New(ar.Error)
}
return nil
}
// InspectFullTextSearchIndex checks the availability of the index
// and returns it if exists
func (h *Handler) InspectFullTextSearchIndex(ctx context.Context, indexName string) (bool, *IndexDefinition, error) {
req, _ := http.NewRequest("GET", h.fullTextSearchURL(ctx, ""), nil)
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")
resp, err := h.http.Do(req)
if err != nil {
return false, nil, err
}
defer resp.Body.Close()
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return false, nil, err
}
var ar apiResponse
err = json.Unmarshal(respbody, &ar)
if err != nil {
return false, nil, err
}
if v, ok := ar.IndexDefs.IndexDefs[indexName]; ok {
return true, &v, nil
}
return false, nil, nil
}
func (h *Handler) fullTextSearchURL(ctx context.Context, indexName string) string {
if indexName == "" {
return fmt.Sprintf("%s%s", h.httpAddress, ftsEndpoint)
}
return fmt.Sprintf("%s%s/%s", h.httpAddress, ftsEndpoint, indexName)
}
func (h *Handler) CountIndex(ctx context.Context, indexName string) (*IndexCount, error) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/index/%s/count", statAddress, indexName), nil)
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")
resp, err := h.http.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var indexCount IndexCount
if err := json.Unmarshal(respbody, &indexCount); err != nil {
return nil, err
}
return &indexCount, nil
}
func (h *Handler) IndexStat(ctx context.Context, indexName string) (*IndexStat, error) {
req, _ := http.NewRequest("GET", fmt.Sprintf("%s/api/stats/sourceStats/%s", statAddress, indexName), nil)
setupBasicAuth(req)
req.Header.Add("Content-Type", "application/json")
resp, err := h.http.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respbody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var indexStat IndexStat
if err := json.Unmarshal(respbody, &indexStat); err != nil {
return nil, err
}
return &indexStat, nil
}