@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"encoding/json"
22
22
"fmt"
23
+ "strings"
23
24
"time"
24
25
25
26
"github.com/meilisearch/meilisearch-go"
@@ -66,95 +67,88 @@ func NewMeiliClient(conf config.Config) (store.DocStoreInterface, error) {
66
67
}
67
68
68
69
func (c * Client ) init () error {
69
- attrs , err := c .docIndex .GetFilterableAttributes ()
70
- if err != nil {
70
+ testDoc := (& doc.Document {}).NewTest ()
71
+ // doc index
72
+ if err := c .initIndex (c .docIndex , DocFilterableAttrs , DocSortAttrs , func () error {
73
+ return c .CreateDocument (context .TODO (), testDoc )
74
+ }); err != nil {
71
75
return err
72
76
}
73
- if ! utils .Equal (DocFilterableAttrs , attrs ) {
74
- t , err := c .docIndex .UpdateFilterableAttributes (& DocFilterableAttrs )
75
- if err != nil {
76
- return err
77
- }
78
- if err = c .wait (context .TODO (), "document" , t .TaskUID ); err != nil {
79
- return err
80
- }
81
- }
82
77
83
- sortAttrs := DocSortAttrs
84
- crtSortAttrs , err := c .docIndex .GetSortableAttributes ()
85
- if err != nil {
78
+ // attr index
79
+ if err := c .initIndex (c .attrIndex , DocAttrFilterableAttrs , DocAttrSortAttrs , func () error {
80
+ return c .CreateDocument (context .TODO (), testDoc )
81
+ }); err != nil {
86
82
return err
87
83
}
88
- if ! utils .Equal (sortAttrs , crtSortAttrs ) {
89
- t , err := c .docIndex .UpdateSortableAttributes (& sortAttrs )
90
- if err != nil {
84
+
85
+ d , err := c .GetDocument (context .TODO (), testDoc .EntryId )
86
+ if (err != nil && strings .Contains (err .Error (), "not found" )) || (err == nil && d == nil ) {
87
+ return nil
88
+ } else if d != nil {
89
+ return c .DeleteDocument (context .TODO (), testDoc .EntryId )
90
+ }
91
+ return err
92
+ }
93
+
94
+ func (c * Client ) initIndex (index meilisearch.IndexManager , filterableAttrs , sortableAttrs []string , noIndexFn func () error ) error {
95
+ attrs , err := index .GetFilterableAttributes ()
96
+ if err != nil {
97
+ if ! strings .Contains (err .Error (), "not found" ) {
91
98
return err
92
99
}
93
- if err = c . wait ( context . TODO (), "document" , t . TaskUID ); err != nil {
100
+ if err := noIndexFn ( ); err != nil {
94
101
return err
95
102
}
96
103
}
97
-
98
- // attr index
99
- attrAttrs , err := c .attrIndex .GetFilterableAttributes ()
100
- if err != nil {
101
- return err
102
- }
103
- if ! utils .Equal (DocAttrFilterableAttrs , attrAttrs ) {
104
- t , err := c .docIndex .UpdateFilterableAttributes (& DocAttrFilterableAttrs )
104
+ if ! utils .Equal (filterableAttrs , attrs ) {
105
+ t , err := index .UpdateFilterableAttributes (& filterableAttrs )
105
106
if err != nil {
106
107
return err
107
108
}
108
- if err = c .wait (context .TODO (), "attr" , t .TaskUID ); err != nil {
109
+ if err = c .wait (context .TODO (), index , t .TaskUID ); err != nil {
109
110
return err
110
111
}
111
112
}
112
- attrSortAttrs := DocAttrSortAttrs
113
- crtAttrSortAttrs , err := c . docIndex .GetSortableAttributes ()
113
+
114
+ crtSortAttrs , err := index .GetSortableAttributes ()
114
115
if err != nil {
115
116
return err
116
117
}
117
- if ! utils .Equal (attrSortAttrs , crtAttrSortAttrs ) {
118
- t , err := c . docIndex . UpdateSortableAttributes (& attrSortAttrs )
118
+ if ! utils .Equal (sortableAttrs , crtSortAttrs ) {
119
+ t , err := index . UpdateSortableAttributes (& sortableAttrs )
119
120
if err != nil {
120
121
return err
121
122
}
122
- if err = c .wait (context .TODO (), "attr" , t .TaskUID ); err != nil {
123
+ if err = c .wait (context .TODO (), index , t .TaskUID ); err != nil {
123
124
return err
124
125
}
125
126
}
126
127
return nil
127
128
}
128
129
129
- func (c * Client ) index (kind string ) meilisearch.IndexManager {
130
- if kind == "attr" {
131
- return c .attrIndex
132
- }
133
- return c .docIndex
134
- }
135
-
136
130
func (c * Client ) CreateDocument (ctx context.Context , doc * doc.Document ) error {
137
131
newDoc := (& Document {}).FromModel (doc )
138
132
c .log .Debugf ("store entryId %s" , newDoc .EntryId )
139
- task , err := c .index ( newDoc . Kind ) .AddDocuments (newDoc , "id" )
133
+ task , err := c .docIndex .AddDocuments (newDoc , "id" )
140
134
if err != nil {
141
135
c .log .Error (err )
142
136
return err
143
137
}
144
- if err := c .wait (ctx , newDoc . Kind , task .TaskUID ); err != nil {
138
+ if err := c .wait (ctx , c . docIndex , task .TaskUID ); err != nil {
145
139
c .log .Errorf ("store document with entryId %s error: %s" , newDoc .EntryId , err )
146
140
return err
147
141
}
148
142
149
143
// store document attr
150
144
newAttrs := (& DocumentAttrList {}).FromModel (doc )
151
145
c .log .Debugf ("store doc of entryId %d attrs: %s" , doc .EntryId , newAttrs .String ())
152
- t , err := c .index ( "attr" ) .AddDocuments (newAttrs , "id" )
146
+ t , err := c .attrIndex .AddDocuments (newAttrs , "id" )
153
147
if err != nil {
154
148
c .log .Error (err )
155
149
return err
156
150
}
157
- if err := c .wait (ctx , "attr" , t .TaskUID ); err != nil {
151
+ if err := c .wait (ctx , c . attrIndex , t .TaskUID ); err != nil {
158
152
c .log .Errorf ("store document attr of entryId %d error: %s" , doc .EntryId , err )
159
153
return err
160
154
}
@@ -170,24 +164,24 @@ func (c *Client) UpdateDocument(ctx context.Context, doc *doc.Document) error {
170
164
for _ , aq := range newAttrsQuery .AttrQueries {
171
165
filter = append (filter , aq .ToFilter ())
172
166
}
173
- t , err := c .index ( "attr" ) .DeleteDocumentsByFilter (filter )
167
+ t , err := c .attrIndex .DeleteDocumentsByFilter (filter )
174
168
if err != nil {
175
169
c .log .Error (err )
176
170
return err
177
171
}
178
- if err = c .wait (ctx , "attr" , t .TaskUID ); err != nil {
172
+ if err = c .wait (ctx , c . attrIndex , t .TaskUID ); err != nil {
179
173
c .log .Errorf ("delete document by filter error: %s" , err )
180
174
return err
181
175
}
182
176
// store document attr
183
177
newAttrs := (& DocumentAttrList {}).FromModel (doc )
184
178
c .log .Debugf ("store doc of entryId %d attrs: %s" , doc .EntryId , newAttrs .String ())
185
- t , err = c .index ( "attr" ) .AddDocuments (newAttrs , "id" )
179
+ t , err = c .attrIndex .AddDocuments (newAttrs , "id" )
186
180
if err != nil {
187
181
c .log .Error (err )
188
182
return err
189
183
}
190
- if err := c .wait (ctx , "attr" , t .TaskUID ); err != nil {
184
+ if err := c .wait (ctx , c . attrIndex , t .TaskUID ); err != nil {
191
185
c .log .Errorf ("store document attr of entryId %d error: %s" , doc .EntryId , err )
192
186
return err
193
187
}
@@ -198,7 +192,7 @@ func (c *Client) GetDocument(ctx context.Context, entryId int64) (*doc.Document,
198
192
namespace := models .GetNamespace (ctx )
199
193
query := (& DocumentQuery {}).OfEntryId (namespace .String (), entryId )
200
194
c .log .Debugf ("get document by entryId: %d" , entryId )
201
- rep , err := c .index ( "document" ) .Search ("" , query .ToRequest ())
195
+ rep , err := c .docIndex .Search ("" , query .ToRequest ())
202
196
if err != nil {
203
197
return nil , err
204
198
}
@@ -215,7 +209,7 @@ func (c *Client) GetDocument(ctx context.Context, entryId int64) (*doc.Document,
215
209
// get attrs
216
210
attrQuery := (& DocumentAttrQuery {}).OfEntryId (document .Namespace , document .EntryId )
217
211
c .log .Debugf ("filter document attr: %s" , attrQuery .String ())
218
- attrRep , err := c .index ( "attr" ) .Search ("" , attrQuery .ToRequest ())
212
+ attrRep , err := c .attrIndex .Search ("" , attrQuery .ToRequest ())
219
213
if err != nil {
220
214
return nil , err
221
215
}
@@ -241,7 +235,7 @@ func (c *Client) FilterDocuments(ctx context.Context, filter *doc.DocumentFilter
241
235
attrQuery := (& DocumentAttrQueries {}).FromFilter (filter )
242
236
for _ , aq := range * attrQuery {
243
237
c .log .Debugf ("filter document attr: %s" , aq .String ())
244
- attrRep , err := c .index ( "attr" ) .Search ("" , aq .ToRequest ())
238
+ attrRep , err := c .attrIndex .Search ("" , aq .ToRequest ())
245
239
if err != nil {
246
240
return nil , err
247
241
}
@@ -263,11 +257,14 @@ func (c *Client) FilterDocuments(ctx context.Context, filter *doc.DocumentFilter
263
257
Option : "IN" ,
264
258
Value : entryIds ,
265
259
})
260
+ } else {
261
+ // no result
262
+ return nil , nil
266
263
}
267
264
}
268
265
269
266
c .log .Debugf ("search document: [%s] query: [%s]" , query .Search , query .String ())
270
- rep , err := c .index ( "document" ) .Search (query .Search , query .ToRequest ())
267
+ rep , err := c .docIndex .Search (query .Search , query .ToRequest ())
271
268
if err != nil {
272
269
return nil , err
273
270
}
@@ -282,16 +279,17 @@ func (c *Client) FilterDocuments(ctx context.Context, filter *doc.DocumentFilter
282
279
c .log .Errorf ("unmarshal document error: %s" , err )
283
280
continue
284
281
}
282
+ c .log .Debugf ("get document: %s" , document .String ())
285
283
286
284
// get attrs
287
285
attrQuery := (& DocumentAttrQuery {}).OfEntryId (document .Namespace , document .EntryId )
288
286
c .log .Debugf ("filter document attr: %s" , attrQuery .String ())
289
- attrRep , err := c .index ( "attr" ) .Search ("" , attrQuery .ToRequest ())
287
+ attrRep , err := c .attrIndex .Search ("" , attrQuery .ToRequest ())
290
288
if err != nil {
291
289
return nil , err
292
290
}
293
291
294
- attrs := make ([] * DocumentAttr , 0 )
292
+ attrs := DocumentAttrList {}
295
293
for _ , hit := range attrRep .Hits {
296
294
b , _ := json .Marshal (hit )
297
295
attr := & DocumentAttr {}
@@ -302,38 +300,47 @@ func (c *Client) FilterDocuments(ctx context.Context, filter *doc.DocumentFilter
302
300
}
303
301
attrs = append (attrs , attr )
304
302
}
303
+ c .log .Debugf ("filter [%d] document attr: %s" , len (attrs ), attrs .String ())
305
304
documents = append (documents , document .ToModel (attrs ))
306
305
}
307
306
return documents , nil
308
307
}
309
308
310
309
func (c * Client ) DeleteDocument (ctx context.Context , entryId int64 ) error {
311
310
c .log .Debugf ("delete document by entryId: %d" , entryId )
312
- aq := & AttrQuery {
313
- Attr : "entryId" ,
314
- Option : "=" ,
315
- Value : fmt .Sprintf ("%d" , entryId ),
311
+ ns := models .GetNamespace (ctx )
312
+ dq := (& DocumentQuery {}).OfEntryId (ns .String (), entryId )
313
+ t , err := c .docIndex .DeleteDocumentsByFilter (dq .ToFilter ())
314
+ if err != nil {
315
+ c .log .Error (err )
316
+ return err
316
317
}
317
- t , err := c .index ("attr" ).DeleteDocumentsByFilter (aq .ToFilter ())
318
+ if err := c .wait (ctx , c .docIndex , t .TaskUID ); err != nil {
319
+ c .log .Errorf ("delete document by filter error: %s" , err )
320
+ }
321
+
322
+ c .log .Debugf ("delete document attr by entryId: %d" , entryId )
323
+ aq := (& DocumentAttrQuery {}).OfEntryId (ns .String (), fmt .Sprintf ("%d" , entryId ))
324
+ t , err = c .attrIndex .DeleteDocumentsByFilter (aq .ToFilter ())
318
325
if err != nil {
319
326
c .log .Error (err )
320
327
return err
321
328
}
322
- if err := c .wait (ctx , "attr" , t .TaskUID ); err != nil {
329
+ if err := c .wait (ctx , c . attrIndex , t .TaskUID ); err != nil {
323
330
c .log .Errorf ("delete document by filter error: %s" , err )
324
331
}
325
332
return nil
326
333
}
327
334
328
- func (c * Client ) wait (ctx context.Context , kind string , taskUID int64 ) error {
335
+ func (c * Client ) wait (ctx context.Context , index meilisearch. IndexManager , taskUID int64 ) error {
329
336
t := time .NewTicker (100 * time .Millisecond )
330
337
defer t .Stop ()
331
338
for {
332
339
select {
333
340
case <- ctx .Done ():
334
341
return fmt .Errorf ("context timeout" )
335
342
case <- t .C :
336
- t , err := c . index ( kind ) .GetTask (taskUID )
343
+ t , err := index .GetTask (taskUID )
337
344
if err != nil {
338
345
c .log .Error (err )
339
346
return err
0 commit comments