Skip to content

Commit

Permalink
Merge #590
Browse files Browse the repository at this point in the history
590: Feat update documents by function r=curquiza a=Ja7ad

# Pull Request

## Related issue
Fixes #574
## What does this PR do?
- Add update documents by function

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Javad <[email protected]>
  • Loading branch information
meili-bors[bot] and Ja7ad authored Nov 27, 2024
2 parents 0bf60fa + 421d917 commit a3d1f82
Show file tree
Hide file tree
Showing 7 changed files with 2,628 additions and 825 deletions.
21 changes: 21 additions & 0 deletions index_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ func (i *index) UpdateDocumentsNdjsonInBatchesWithContext(ctx context.Context, d
return i.updateDocumentsNdjsonFromReaderInBatches(ctx, bytes.NewReader(documents), batchSize, primaryKey...)
}

func (i *index) UpdateDocumentsByFunction(req *UpdateDocumentByFunctionRequest) (*TaskInfo, error) {
return i.UpdateDocumentsByFunctionWithContext(context.Background(), req)
}

func (i *index) UpdateDocumentsByFunctionWithContext(ctx context.Context, req *UpdateDocumentByFunctionRequest) (*TaskInfo, error) {
resp := new(TaskInfo)
r := &internalRequest{
endpoint: "/indexes/" + i.uid + "/documents/edit",
method: http.MethodPost,
withRequest: req,
withResponse: resp,
contentType: contentTypeJSON,
acceptedStatusCodes: []int{http.StatusAccepted},
functionName: "UpdateDocumentsByFunction",
}
if err := i.client.executeRequest(ctx, r); err != nil {
return nil, err
}
return resp, nil
}

func (i *index) GetDocument(identifier string, request *DocumentQuery, documentPtr interface{}) error {
return i.GetDocumentWithContext(context.Background(), identifier, request, documentPtr)
}
Expand Down
33 changes: 33 additions & 0 deletions index_document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1658,3 +1658,36 @@ func TestIndex_DeleteDocumentsByFilter(t *testing.T) {
})
}
}

func TestIndex_UpdateDocumentsByFunction(t *testing.T) {
c := setup(t, "")

exp := c.ExperimentalFeatures()
exp.SetEditDocumentsByFunction(true)
res, err := exp.Update()
require.NoError(t, err)
require.True(t, res.EditDocumentsByFunction)

idx := setupMovieIndex(t, c)
t.Cleanup(cleanup(c))

t.Run("Test Upper Case and Add Sparkles around Movie Titles", func(t *testing.T) {
task, err := idx.UpdateDocumentsByFunction(&UpdateDocumentByFunctionRequest{
Filter: "id > 3000",
Function: "doc.title = `✨ ${doc.title.to_upper()} ✨`",
})
require.NoError(t, err)
testWaitForTask(t, idx, task)
})

t.Run("Test User-defined Context", func(t *testing.T) {
task, err := idx.UpdateDocumentsByFunction(&UpdateDocumentByFunctionRequest{
Context: map[string]interface{}{
"idmax": 50,
},
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}",
})
require.NoError(t, err)
testWaitForTask(t, idx, task)
})
}
6 changes: 6 additions & 0 deletions index_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ type DocumentManager interface {
// UpdateDocumentsNdjsonInBatchesWithContext updates documents in the index from a NDJSON byte array in batches of specified size using the provided context for cancellation.
UpdateDocumentsNdjsonInBatchesWithContext(ctx context.Context, documents []byte, batchsize int, primaryKey ...string) ([]TaskInfo, error)

// UpdateDocumentsByFunction update documents by using function
UpdateDocumentsByFunction(req *UpdateDocumentByFunctionRequest) (*TaskInfo, error)

// UpdateDocumentsByFunctionWithContext update documents by using function then provided context for cancellation.
UpdateDocumentsByFunctionWithContext(ctx context.Context, req *UpdateDocumentByFunctionRequest) (*TaskInfo, error)

// DeleteDocument deletes a single document from the index by identifier.
DeleteDocument(identifier string) (*TaskInfo, error)

Expand Down
24 changes: 24 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,30 @@ func setUpBasicIndex(sv ServiceManager, indexUID string) {
}
}

func setupMovieIndex(t *testing.T, client ServiceManager) IndexManager {
t.Helper()

idx := client.Index("indexUID")

testdata, err := os.Open("./testdata/movies.json")
require.NoError(t, err)
defer testdata.Close()

tests := make([]map[string]interface{}, 0)

require.NoError(t, json.NewDecoder(testdata).Decode(&tests))

task, err := idx.AddDocuments(tests)
require.NoError(t, err)
testWaitForTask(t, idx, task)

task, err = idx.UpdateFilterableAttributes(&[]string{"id"})
require.NoError(t, err)
testWaitForTask(t, idx, task)

return idx
}

func setUpIndexForFaceting(client ServiceManager) {
idx := client.Index("indexUID")

Expand Down
Loading

0 comments on commit a3d1f82

Please sign in to comment.