-
Notifications
You must be signed in to change notification settings - Fork 86
/
index.go
154 lines (133 loc) · 3.66 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
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
package meilisearch
import (
"context"
"net/http"
)
// index is the type that represent an index in meilisearch
type index struct {
uid string
primaryKey string
client *client
}
func newIndex(cli *client, uid string) IndexManager {
return &index{
client: cli,
uid: uid,
}
}
func (i *index) GetTaskReader() TaskReader {
return i
}
func (i *index) GetDocumentManager() DocumentManager {
return i
}
func (i *index) GetDocumentReader() DocumentReader {
return i
}
func (i *index) GetSettingsManager() SettingsManager {
return i
}
func (i *index) GetSettingsReader() SettingsReader {
return i
}
func (i *index) GetSearch() SearchReader {
return i
}
func (i *index) GetIndexReader() IndexReader {
return i
}
func (i *index) FetchInfo() (*IndexResult, error) {
return i.FetchInfoWithContext(context.Background())
}
func (i *index) FetchInfoWithContext(ctx context.Context) (*IndexResult, error) {
resp := new(IndexResult)
req := &internalRequest{
endpoint: "/indexes/" + i.uid,
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "FetchInfo",
}
if err := i.client.executeRequest(ctx, req); err != nil {
return nil, err
}
if resp.PrimaryKey != "" {
i.primaryKey = resp.PrimaryKey
}
resp.IndexManager = i
return resp, nil
}
func (i *index) FetchPrimaryKey() (*string, error) {
return i.FetchPrimaryKeyWithContext(context.Background())
}
func (i *index) FetchPrimaryKeyWithContext(ctx context.Context) (*string, error) {
idx, err := i.FetchInfoWithContext(ctx)
if err != nil {
return nil, err
}
i.primaryKey = idx.PrimaryKey
return &idx.PrimaryKey, nil
}
func (i *index) UpdateIndex(primaryKey string) (*TaskInfo, error) {
return i.UpdateIndexWithContext(context.Background(), primaryKey)
}
func (i *index) UpdateIndexWithContext(ctx context.Context, primaryKey string) (*TaskInfo, error) {
request := &UpdateIndexRequest{
PrimaryKey: primaryKey,
}
i.primaryKey = primaryKey
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/indexes/" + i.uid,
method: http.MethodPatch,
contentType: contentTypeJSON,
withRequest: request,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "UpdateIndex",
}
if err := i.client.executeRequest(ctx, req); err != nil {
return nil, err
}
i.primaryKey = primaryKey
return resp, nil
}
func (i *index) Delete(uid string) (bool, error) {
return i.DeleteWithContext(context.Background(), uid)
}
func (i *index) DeleteWithContext(ctx context.Context, uid string) (bool, error) {
resp := new(TaskInfo)
req := &internalRequest{
endpoint: "/indexes/" + uid,
method: http.MethodDelete,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "Delete",
}
// err is not nil if status code is not 204 StatusNoContent
if err := i.client.executeRequest(ctx, req); err != nil {
return false, err
}
i.primaryKey = ""
return true, nil
}
func (i *index) GetStats() (*StatsIndex, error) {
return i.GetStatsWithContext(context.Background())
}
func (i *index) GetStatsWithContext(ctx context.Context) (*StatsIndex, error) {
resp := new(StatsIndex)
req := &internalRequest{
endpoint: "/indexes/" + i.uid + "/stats",
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetStats",
}
if err := i.client.executeRequest(ctx, req); err != nil {
return nil, err
}
return resp, nil
}