-
Notifications
You must be signed in to change notification settings - Fork 104
/
cluster_searchindexes.go
459 lines (376 loc) · 13.6 KB
/
cluster_searchindexes.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package gocb
import (
"context"
"encoding/json"
"time"
)
// SearchIndex is used to define a search index.
type SearchIndex struct {
// UUID is required for updates. It provides a means of ensuring consistency, the UUID must match the UUID value
// for the index on the server.
UUID string
// Name represents the name of this index.
Name string
// SourceName is the name of the source of the data for the index e.g. bucket name.
SourceName string
// Type is the type of index, e.g. fulltext-index or fulltext-alias.
Type string
// IndexParams are index properties such as store type and mappings.
Params map[string]interface{}
// SourceUUID is the UUID of the data source, this can be used to more tightly tie the index to a source.
SourceUUID string
// SourceParams are extra parameters to be defined. These are usually things like advanced connection and tuning
// parameters.
SourceParams map[string]interface{}
// SourceType is the type of the data source, e.g. couchbase or nil depending on the Type field.
SourceType string
// PlanParams are plan properties such as number of replicas and number of partitions.
PlanParams map[string]interface{}
}
func (si *SearchIndex) UnmarshalJSON(bytes []byte) error {
var index jsonSearchIndex
err := json.Unmarshal(bytes, &index)
if err != nil {
return err
}
return si.fromData(index)
}
func (si *SearchIndex) MarshalJSON() ([]byte, error) {
index, err := si.toData()
if err != nil {
return nil, err
}
return json.Marshal(index)
}
func (si *SearchIndex) fromData(data jsonSearchIndex) error {
si.UUID = data.UUID
si.Name = data.Name
si.SourceName = data.SourceName
si.Type = data.Type
si.Params = data.Params
si.SourceUUID = data.SourceUUID
si.SourceParams = data.SourceParams
si.SourceType = data.SourceType
si.PlanParams = data.PlanParams
return nil
}
func (si *SearchIndex) toData() (jsonSearchIndex, error) {
var data jsonSearchIndex
data.UUID = si.UUID
data.Name = si.Name
data.SourceName = si.SourceName
data.Type = si.Type
data.Params = si.Params
data.SourceUUID = si.SourceUUID
data.SourceParams = si.SourceParams
data.SourceType = si.SourceType
data.PlanParams = si.PlanParams
return data, nil
}
// SearchIndexManager provides methods for performing Couchbase search index management.
type SearchIndexManager struct {
getProvider func() (searchIndexProvider, error)
}
// GetAllSearchIndexOptions is the set of options available to the search indexes GetAllIndexes operation.
type GetAllSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// GetAllIndexes retrieves all of the search indexes for the cluster.
func (sm *SearchIndexManager) GetAllIndexes(opts *GetAllSearchIndexOptions) ([]SearchIndex, error) {
if opts == nil {
opts = &GetAllSearchIndexOptions{}
}
provider, err := sm.getProvider()
if err != nil {
return nil, err
}
return provider.GetAllIndexes(nil, opts)
}
// GetSearchIndexOptions is the set of options available to the search indexes GetIndex operation.
type GetSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// GetIndex retrieves a specific search index by name.
func (sm *SearchIndexManager) GetIndex(indexName string, opts *GetSearchIndexOptions) (*SearchIndex, error) {
if opts == nil {
opts = &GetSearchIndexOptions{}
}
if indexName == "" {
return nil, invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return nil, err
}
return provider.GetIndex(nil, indexName, opts)
}
// UpsertSearchIndexOptions is the set of options available to the search index manager UpsertIndex operation.
type UpsertSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// UpsertIndex creates or updates a search index.
func (sm *SearchIndexManager) UpsertIndex(indexDefinition SearchIndex, opts *UpsertSearchIndexOptions) error {
if opts == nil {
opts = &UpsertSearchIndexOptions{}
}
if indexDefinition.Name == "" {
return invalidArgumentsError{"index name cannot be empty"}
}
if indexDefinition.Type == "" {
return invalidArgumentsError{"index type cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.UpsertIndex(nil, indexDefinition, opts)
}
// DropSearchIndexOptions is the set of options available to the search index DropIndex operation.
type DropSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// DropIndex removes the search index with the specific name.
func (sm *SearchIndexManager) DropIndex(indexName string, opts *DropSearchIndexOptions) error {
if opts == nil {
opts = &DropSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.DropIndex(nil, indexName, opts)
}
// AnalyzeDocumentOptions is the set of options available to the search index AnalyzeDocument operation.
type AnalyzeDocumentOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// AnalyzeDocument returns how a doc is analyzed against a specific index.
func (sm *SearchIndexManager) AnalyzeDocument(indexName string, doc interface{}, opts *AnalyzeDocumentOptions) ([]interface{}, error) {
if opts == nil {
opts = &AnalyzeDocumentOptions{}
}
if indexName == "" {
return nil, invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return nil, err
}
return provider.AnalyzeDocument(nil, indexName, doc, opts)
}
// GetIndexedDocumentsCountOptions is the set of options available to the search index GetIndexedDocumentsCount operation.
type GetIndexedDocumentsCountOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// GetIndexedDocumentsCount retrieves the document count for a search index.
func (sm *SearchIndexManager) GetIndexedDocumentsCount(indexName string, opts *GetIndexedDocumentsCountOptions) (uint64, error) {
if opts == nil {
opts = &GetIndexedDocumentsCountOptions{}
}
if indexName == "" {
return 0, invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return 0, err
}
return provider.GetIndexedDocumentsCount(nil, indexName, opts)
}
// PauseIngestSearchIndexOptions is the set of options available to the search index PauseIngest operation.
type PauseIngestSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// PauseIngest pauses updates and maintenance for an index.
func (sm *SearchIndexManager) PauseIngest(indexName string, opts *PauseIngestSearchIndexOptions) error {
if opts == nil {
opts = &PauseIngestSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.PauseIngest(nil, indexName, opts)
}
// ResumeIngestSearchIndexOptions is the set of options available to the search index ResumeIngest operation.
type ResumeIngestSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// ResumeIngest resumes updates and maintenance for an index.
func (sm *SearchIndexManager) ResumeIngest(indexName string, opts *ResumeIngestSearchIndexOptions) error {
if opts == nil {
opts = &ResumeIngestSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.ResumeIngest(nil, indexName, opts)
}
// AllowQueryingSearchIndexOptions is the set of options available to the search index AllowQuerying operation.
type AllowQueryingSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// AllowQuerying allows querying against an index.
func (sm *SearchIndexManager) AllowQuerying(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.AllowQuerying(nil, indexName, opts)
}
// DisallowQueryingSearchIndexOptions is the set of options available to the search index DisallowQuerying operation.
type DisallowQueryingSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// DisallowQuerying disallows querying against an index.
func (sm *SearchIndexManager) DisallowQuerying(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.DisallowQuerying(nil, indexName, &DisallowQueryingSearchIndexOptions{
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
ParentSpan: opts.ParentSpan,
Context: opts.Context,
})
}
// FreezePlanSearchIndexOptions is the set of options available to the search index FreezePlan operation.
type FreezePlanSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// FreezePlan freezes the assignment of index partitions to nodes.
func (sm *SearchIndexManager) FreezePlan(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.FreezePlan(nil, indexName, &FreezePlanSearchIndexOptions{
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
ParentSpan: opts.ParentSpan,
Context: opts.Context,
})
}
// UnfreezePlanSearchIndexOptions is the set of options available to the search index UnfreezePlan operation.
type UnfreezePlanSearchIndexOptions struct {
Timeout time.Duration
RetryStrategy RetryStrategy
ParentSpan RequestSpan
// Using a deadlined Context alongside a Timeout will cause the shorter of the two to cause cancellation, this
// also applies to global level timeouts.
// UNCOMMITTED: This API may change in the future.
Context context.Context
}
// UnfreezePlan unfreezes the assignment of index partitions to nodes.
func (sm *SearchIndexManager) UnfreezePlan(indexName string, opts *AllowQueryingSearchIndexOptions) error {
if opts == nil {
opts = &AllowQueryingSearchIndexOptions{}
}
if indexName == "" {
return invalidArgumentsError{"indexName cannot be empty"}
}
provider, err := sm.getProvider()
if err != nil {
return err
}
return provider.UnfreezePlan(nil, indexName, &UnfreezePlanSearchIndexOptions{
Timeout: opts.Timeout,
RetryStrategy: opts.RetryStrategy,
ParentSpan: opts.ParentSpan,
Context: opts.Context,
})
}