-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
84 lines (66 loc) · 2.06 KB
/
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
package zinc
import (
"context"
"github.com/fabmation-gmbh/zinc-go/pkg/meta"
)
// IndexCreateService provides methods to manage indicies.
type IndexCreateService struct {
cli *Client
req struct {
Name string `json:"name"`
StorageType meta.IndexStorageType `json:"storage_type"`
Mappings struct {
Props map[string]meta.IndexMappingProperty `json:"properties"`
} `json:"mappings,omitempty"`
// TODO: Support settings
// Settings *IndexSettings `json:"settings,omitempty"`
}
}
// NewIndexCreateService returns a fully initialized IndexCreateService.
func NewIndexCreateService(cli *Client) *IndexCreateService {
svc := &IndexCreateService{
cli: cli,
}
svc.req.Mappings.Props = make(map[string]meta.IndexMappingProperty)
return svc
}
// Name sets the name of the index.
//
// The IndexService object is returned to allow stacking method calls.
func (i *IndexCreateService) Name(name string) *IndexCreateService {
i.req.Name = name
return i
}
// IndexStorageType sets the storage type of the index.
//
// The IndexService object is returned to allow stacking method calls.
func (i *IndexCreateService) IndexStorageType(storage meta.IndexStorageType) *IndexCreateService {
i.req.StorageType = storage
return i
}
// AddMappingProperty adds the given index property to the index.
//
// The IndexService object is returned to allow stacking method calls.
func (i *IndexCreateService) AddMappingProperty(prop meta.IndexMappingProperty) *IndexCreateService {
i.req.Mappings.Props[prop.Name] = prop
return i
}
// Create creates the index.
func (i *IndexCreateService) Create(ctx context.Context) (IndexCreateResponse, error) {
var resp IndexCreateResponse
_, err := i.cli.c.R().
SetResult(&resp).
SetBody(i.req).
Post("/index")
if err != nil {
return IndexCreateResponse{}, err
}
return resp, nil
}
// IndexGetMappingResponse holds the index mapping.
type IndexGetMappingResponse struct {
// Name is the index name.
Name string `json:"name"`
// Mappings holds the index mappings.
Mappings meta.Mappings `json:"mappings"`
}