Skip to content

Commit 350d02e

Browse files
committed
feat: update documents by function
1 parent 0bf60fa commit 350d02e

7 files changed

+2627
-825
lines changed

index_document.go

+21
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,27 @@ func (i *index) UpdateDocumentsNdjsonInBatchesWithContext(ctx context.Context, d
223223
return i.updateDocumentsNdjsonFromReaderInBatches(ctx, bytes.NewReader(documents), batchSize, primaryKey...)
224224
}
225225

226+
func (i *index) UpdateDocumentsByFunction(req *UpdateDocumentByFunctionRequest) (*TaskInfo, error) {
227+
return i.UpdateDocumentsByFunctionWithContext(context.Background(), req)
228+
}
229+
230+
func (i *index) UpdateDocumentsByFunctionWithContext(ctx context.Context, req *UpdateDocumentByFunctionRequest) (*TaskInfo, error) {
231+
resp := new(TaskInfo)
232+
r := &internalRequest{
233+
endpoint: "/indexes/" + i.uid + "/documents/edit",
234+
method: http.MethodPost,
235+
withRequest: req,
236+
withResponse: resp,
237+
contentType: contentTypeJSON,
238+
acceptedStatusCodes: []int{http.StatusAccepted},
239+
functionName: "UpdateDocumentsByFunction",
240+
}
241+
if err := i.client.executeRequest(ctx, r); err != nil {
242+
return nil, err
243+
}
244+
return resp, nil
245+
}
246+
226247
func (i *index) GetDocument(identifier string, request *DocumentQuery, documentPtr interface{}) error {
227248
return i.GetDocumentWithContext(context.Background(), identifier, request, documentPtr)
228249
}

index_document_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -1658,3 +1658,35 @@ func TestIndex_DeleteDocumentsByFilter(t *testing.T) {
16581658
})
16591659
}
16601660
}
1661+
1662+
func TestIndex_UpdateDocumentsByFunction(t *testing.T) {
1663+
c := setup(t, "")
1664+
1665+
exp := c.ExperimentalFeatures()
1666+
exp.SetEditDocumentsByFunction(true)
1667+
res, err := exp.Update()
1668+
require.NoError(t, err)
1669+
require.True(t, res.EditDocumentsByFunction)
1670+
1671+
idx := setupMovieIndex(t, c)
1672+
1673+
t.Run("Test Upper Case and Add Sparkles around Movie Titles", func(t *testing.T) {
1674+
task, err := idx.UpdateDocumentsByFunction(&UpdateDocumentByFunctionRequest{
1675+
Filter: "id > 3000",
1676+
Function: "doc.title = `✨ ${doc.title.to_upper()} ✨`",
1677+
})
1678+
require.NoError(t, err)
1679+
testWaitForTask(t, idx, task)
1680+
})
1681+
1682+
t.Run("Test User-defined Context", func(t *testing.T) {
1683+
task, err := idx.UpdateDocumentsByFunction(&UpdateDocumentByFunctionRequest{
1684+
Context: map[string]interface{}{
1685+
"idmax": 50,
1686+
},
1687+
Function: "if doc.id >= context.idmax {\n\t\t doc = ()\n\t\t } else {\n\t\t\t doc.title = `✨ ${doc.title} ✨`\n\t\t\t}",
1688+
})
1689+
require.NoError(t, err)
1690+
testWaitForTask(t, idx, task)
1691+
})
1692+
}

index_interface.go

+6
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ type DocumentManager interface {
153153
// UpdateDocumentsNdjsonInBatchesWithContext updates documents in the index from a NDJSON byte array in batches of specified size using the provided context for cancellation.
154154
UpdateDocumentsNdjsonInBatchesWithContext(ctx context.Context, documents []byte, batchsize int, primaryKey ...string) ([]TaskInfo, error)
155155

156+
// UpdateDocumentsByFunction update documents by using function
157+
UpdateDocumentsByFunction(req *UpdateDocumentByFunctionRequest) (*TaskInfo, error)
158+
159+
// UpdateDocumentsByFunctionWithContext update documents by using function then provided context for cancellation.
160+
UpdateDocumentsByFunctionWithContext(ctx context.Context, req *UpdateDocumentByFunctionRequest) (*TaskInfo, error)
161+
156162
// DeleteDocument deletes a single document from the index by identifier.
157163
DeleteDocument(identifier string) (*TaskInfo, error)
158164

main_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,30 @@ func setUpBasicIndex(sv ServiceManager, indexUID string) {
210210
}
211211
}
212212

213+
func setupMovieIndex(t *testing.T, client ServiceManager) IndexManager {
214+
t.Helper()
215+
216+
idx := client.Index("indexUID")
217+
218+
testdata, err := os.Open("./testdata/movies.json")
219+
require.NoError(t, err)
220+
defer testdata.Close()
221+
222+
tests := make([]map[string]interface{}, 0)
223+
224+
require.NoError(t, json.NewDecoder(testdata).Decode(&tests))
225+
226+
task, err := idx.AddDocuments(tests)
227+
require.NoError(t, err)
228+
testWaitForTask(t, idx, task)
229+
230+
task, err = idx.UpdateFilterableAttributes(&[]string{"id"})
231+
require.NoError(t, err)
232+
testWaitForTask(t, idx, task)
233+
234+
return idx
235+
}
236+
213237
func setUpIndexForFaceting(client ServiceManager) {
214238
idx := client.Index("indexUID")
215239

0 commit comments

Comments
 (0)