forked from meilisearch/meilisearch-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.go
219 lines (198 loc) · 7.57 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
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
package meilisearch
import (
"encoding/json"
"net/http"
"strconv"
"strings"
)
// IndexConfig configure the Index
type IndexConfig struct {
// Uid is the unique identifier of a given index.
Uid string
// PrimaryKey is optional
PrimaryKey string
client *Client //nolint:golint,unused,structcheck
}
type IndexInterface interface {
FetchInfo() (resp *Index, err error)
FetchPrimaryKey() (resp *string, err error)
UpdateIndex(primaryKey string) (resp *TaskInfo, err error)
Delete(uid string) (ok bool, err error)
GetStats() (resp *StatsIndex, err error)
AddDocuments(documentsPtr interface{}, primaryKey ...string) (resp *TaskInfo, err error)
AddDocumentsInBatches(documentsPtr interface{}, batchSize int, primaryKey ...string) (resp []TaskInfo, err error)
AddDocumentsCsv(documents []byte, options *CsvDocumentsQuery) (resp *TaskInfo, err error)
AddDocumentsCsvInBatches(documents []byte, batchSize int, options *CsvDocumentsQuery) (resp []TaskInfo, err error)
AddDocumentsNdjson(documents []byte, primaryKey ...string) (resp *TaskInfo, err error)
AddDocumentsNdjsonInBatches(documents []byte, batchSize int, primaryKey ...string) (resp []TaskInfo, err error)
UpdateDocuments(documentsPtr interface{}, primaryKey ...string) (resp *TaskInfo, err error)
UpdateDocumentsInBatches(documentsPtr interface{}, batchSize int, primaryKey ...string) (resp []TaskInfo, err error)
UpdateDocumentsCsv(documents []byte, options *CsvDocumentsQuery) (resp *TaskInfo, err error)
UpdateDocumentsCsvInBatches(documents []byte, batchsize int, options *CsvDocumentsQuery) (resp []TaskInfo, err error)
UpdateDocumentsNdjson(documents []byte, primaryKey ...string) (resp *TaskInfo, err error)
UpdateDocumentsNdjsonInBatches(documents []byte, batchsize int, primaryKey ...string) (resp []TaskInfo, err error)
GetDocument(uid string, request *DocumentQuery, documentPtr interface{}) error
GetDocuments(param *DocumentsQuery, resp *DocumentsResult) error
DeleteDocument(uid string) (resp *TaskInfo, err error)
DeleteDocuments(uid []string) (resp *TaskInfo, err error)
DeleteAllDocuments() (resp *TaskInfo, err error)
Search(query string, request *SearchRequest) (*SearchResponse, error)
SearchRaw(query string, request *SearchRequest) (*json.RawMessage, error)
GetTask(taskUID int64) (resp *Task, err error)
GetTasks(param *TasksQuery) (resp *TaskResult, err error)
GetSettings() (resp *Settings, err error)
UpdateSettings(request *Settings) (resp *TaskInfo, err error)
ResetSettings() (resp *TaskInfo, err error)
GetRankingRules() (resp *[]string, err error)
UpdateRankingRules(request *[]string) (resp *TaskInfo, err error)
ResetRankingRules() (resp *TaskInfo, err error)
GetDistinctAttribute() (resp *string, err error)
UpdateDistinctAttribute(request string) (resp *TaskInfo, err error)
ResetDistinctAttribute() (resp *TaskInfo, err error)
GetSearchableAttributes() (resp *[]string, err error)
UpdateSearchableAttributes(request *[]string) (resp *TaskInfo, err error)
ResetSearchableAttributes() (resp *TaskInfo, err error)
GetDisplayedAttributes() (resp *[]string, err error)
UpdateDisplayedAttributes(request *[]string) (resp *TaskInfo, err error)
ResetDisplayedAttributes() (resp *TaskInfo, err error)
GetStopWords() (resp *[]string, err error)
UpdateStopWords(request *[]string) (resp *TaskInfo, err error)
ResetStopWords() (resp *TaskInfo, err error)
GetSynonyms() (resp *map[string][]string, err error)
UpdateSynonyms(request *map[string][]string) (resp *TaskInfo, err error)
ResetSynonyms() (resp *TaskInfo, err error)
GetFilterableAttributes() (resp *[]string, err error)
UpdateFilterableAttributes(request *[]string) (resp *TaskInfo, err error)
ResetFilterableAttributes() (resp *TaskInfo, err error)
WaitForTask(taskUID int64, options ...WaitParams) (*Task, error)
}
var _ IndexInterface = &Index{}
func newIndex(client *Client, uid string) *Index {
return &Index{
UID: uid,
client: client,
}
}
func (i *Index) FetchInfo() (resp *Index, err error) {
resp = newIndex(i.client, i.UID)
req := internalRequest{
endpoint: "/indexes/" + i.UID,
method: http.MethodGet,
withRequest: nil,
withResponse: resp,
acceptedStatusCodes: []int{http.StatusOK},
functionName: "FetchInfo",
}
if err := i.client.executeRequest(req); err != nil {
return nil, err
}
i.PrimaryKey = resp.PrimaryKey
i.CreatedAt = resp.CreatedAt
i.UpdatedAt = resp.UpdatedAt
return resp, nil
}
func (i Index) FetchPrimaryKey() (resp *string, err error) {
index, err := i.FetchInfo()
if err != nil {
return nil, err
}
return &index.PrimaryKey, nil
}
func (i *Index) UpdateIndex(primaryKey string) (resp *TaskInfo, err error) {
request := &UpdateIndexRequest{
PrimaryKey: primaryKey,
}
i.PrimaryKey = primaryKey
resp = &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(req); err != nil {
return nil, err
}
return resp, nil
}
func (i Index) Delete(uid string) (ok bool, err error) {
resp := &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(req); err != nil {
return false, err
}
return true, nil
}
func (i Index) GetStats() (resp *StatsIndex, err error) {
resp = &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(req); err != nil {
return nil, err
}
return resp, nil
}
func (i Index) GetTask(taskUID int64) (resp *Task, err error) {
return i.client.GetTask(taskUID)
}
func (i Index) GetTasks(param *TasksQuery) (resp *TaskResult, err error) {
resp = &TaskResult{}
req := internalRequest{
endpoint: "/tasks",
method: http.MethodGet,
withRequest: nil,
withResponse: &resp,
withQueryParams: map[string]string{},
acceptedStatusCodes: []int{http.StatusOK},
functionName: "GetTasks",
}
if param != nil {
if param.Limit != 0 {
req.withQueryParams["limit"] = strconv.FormatInt(param.Limit, 10)
}
if param.From != 0 {
req.withQueryParams["from"] = strconv.FormatInt(param.From, 10)
}
if len(param.Statuses) != 0 {
req.withQueryParams["statuses"] = strings.Join(param.Statuses, ",")
}
if len(param.Types) != 0 {
req.withQueryParams["types"] = strings.Join(param.Types, ",")
}
if len(param.IndexUIDS) != 0 {
param.IndexUIDS = append(param.IndexUIDS, i.UID)
req.withQueryParams["indexUids"] = strings.Join(param.IndexUIDS, ",")
} else {
req.withQueryParams["indexUids"] = i.UID
}
}
if err := i.client.executeRequest(req); err != nil {
return nil, err
}
return resp, nil
}
// WaitForTask waits for a task to be processed.
// The function will check by regular interval provided in parameter interval
// the TaskStatus.
// If no ctx and interval are provided WaitForTask will check each 50ms the
// status of a task.
func (i Index) WaitForTask(taskUID int64, options ...WaitParams) (*Task, error) {
return i.client.WaitForTask(taskUID, options...)
}