enhancement(search): implement engine restore

This commit is contained in:
fschade
2025-07-29 13:45:06 +02:00
parent 59b6788b28
commit bd5dec7327
2 changed files with 36 additions and 1 deletions

View File

@@ -61,6 +61,15 @@ func (e *Engine) Delete(id string) error {
}
func (e *Engine) Restore(id string) error {
_, err := e.client.Update(context.Background(), opensearchgoAPI.UpdateReq{
Index: e.index,
DocumentID: id,
Body: bytes.NewReader([]byte(`{"doc": {"Deleted": false}}`)),
})
if err != nil {
return fmt.Errorf("failed to mark document as deleted: %w", err)
}
return nil
}

View File

@@ -57,7 +57,33 @@ func TestEngine_Delete(t *testing.T) {
})
}
func TestEngine_Restore(t *testing.T) {}
func TestEngine_Restore(t *testing.T) {
index := "test-engine-delete"
tc := ostest.NewDefaultTestClient(t)
tc.Require.IndicesReset([]string{index})
tc.Require.IndicesCount([]string{index}, "", 0)
defer tc.Require.IndicesDelete([]string{index})
engine, err := opensearch.NewEngine(index, tc.Client())
assert.NoError(t, err)
t.Run("mark document as not deleted", func(t *testing.T) {
document := ostest.Testdata.Resources.Full
document.Deleted = true
tc.Require.DocumentCreate(index, document.ID, toJSON(t, document))
tc.Require.IndicesCount([]string{index}, "", 1)
tc.Require.IndicesCount([]string{index}, opensearch.NewRootQuery(
opensearch.NewTermQuery[bool]("Deleted").Value(true),
).String(), 1)
assert.NoError(t, engine.Restore(document.ID))
tc.Require.IndicesCount([]string{index}, opensearch.NewRootQuery(
opensearch.NewTermQuery[bool]("Deleted").Value(true),
).String(), 0)
})
}
func TestEngine_Purge(t *testing.T) {
index := "test-engine-purge"