mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-01-05 19:59:37 -06:00
enhancement(search): implement engine delete
This commit is contained in:
@@ -14,13 +14,8 @@ type IDsQueryOptions struct {
|
||||
Boost float32 `json:"boost,omitempty"`
|
||||
}
|
||||
|
||||
func NewIDsQuery(o ...IDsQueryOptions) *IDsQuery {
|
||||
return &IDsQuery{options: merge(o...)}
|
||||
}
|
||||
|
||||
func (q *IDsQuery) Values(v ...string) *IDsQuery {
|
||||
q.values = slices.Compact(append(q.values, v...))
|
||||
return q
|
||||
func NewIDsQuery(v []string, o ...IDsQueryOptions) *IDsQuery {
|
||||
return &IDsQuery{values: slices.Compact(v), options: merge(o...)}
|
||||
}
|
||||
|
||||
func (q *IDsQuery) Map() (map[string]any, error) {
|
||||
|
||||
@@ -12,14 +12,12 @@ func TestIDsQuery(t *testing.T) {
|
||||
tests := []tableTest[opensearch.Builder, map[string]any]{
|
||||
{
|
||||
name: "empty",
|
||||
got: opensearch.NewIDsQuery(),
|
||||
got: opensearch.NewIDsQuery(nil),
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "ids",
|
||||
got: opensearch.NewIDsQuery(opensearch.IDsQueryOptions{Boost: 1.0}).
|
||||
Values("1", "2").
|
||||
Values("3", "3"),
|
||||
got: opensearch.NewIDsQuery([]string{"1", "2", "3", "3"}, opensearch.IDsQueryOptions{Boost: 1.0}),
|
||||
want: map[string]any{
|
||||
"ids": map[string]any{
|
||||
"values": []string{"1", "2", "3"},
|
||||
|
||||
@@ -48,6 +48,15 @@ func (e *Engine) Move(id string, parentID string, target string) error {
|
||||
}
|
||||
|
||||
func (e *Engine) Delete(id string) error {
|
||||
_, err := e.client.Update(context.Background(), opensearchgoAPI.UpdateReq{
|
||||
Index: e.index,
|
||||
DocumentID: id,
|
||||
Body: bytes.NewReader([]byte(`{"doc": {"Deleted": true}}`)),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to mark document as deleted: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ func TestEngine_Upsert(t *testing.T) {
|
||||
index := "test-engine-upsert"
|
||||
tc := ostest.NewDefaultTestClient(t)
|
||||
tc.Require.IndicesReset([]string{index})
|
||||
tc.Require.IndicesCount([]string{index}, 0)
|
||||
tc.Require.IndicesCount([]string{index}, "", 0)
|
||||
|
||||
defer tc.Require.IndicesDelete([]string{index})
|
||||
|
||||
@@ -24,13 +24,38 @@ func TestEngine_Upsert(t *testing.T) {
|
||||
document := ostest.Testdata.Resources.Full
|
||||
assert.NoError(t, engine.Upsert(document.ID, document))
|
||||
|
||||
tc.Require.IndicesCount([]string{index}, 1)
|
||||
tc.Require.IndicesCount([]string{index}, "", 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEngine_Move(t *testing.T) {}
|
||||
|
||||
func TestEngine_Delete(t *testing.T) {}
|
||||
func TestEngine_Delete(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 deleted", func(t *testing.T) {
|
||||
document := ostest.Testdata.Resources.Full
|
||||
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(), 0)
|
||||
|
||||
assert.NoError(t, engine.Delete(document.ID))
|
||||
tc.Require.IndicesCount([]string{index}, opensearch.NewRootQuery(
|
||||
opensearch.NewTermQuery[bool]("Deleted").Value(true),
|
||||
).String(), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEngine_Restore(t *testing.T) {}
|
||||
|
||||
@@ -38,7 +63,7 @@ func TestEngine_Purge(t *testing.T) {
|
||||
index := "test-engine-purge"
|
||||
tc := ostest.NewDefaultTestClient(t)
|
||||
tc.Require.IndicesReset([]string{index})
|
||||
tc.Require.IndicesCount([]string{index}, 0)
|
||||
tc.Require.IndicesCount([]string{index}, "", 0)
|
||||
|
||||
defer tc.Require.IndicesDelete([]string{index})
|
||||
|
||||
@@ -48,11 +73,11 @@ func TestEngine_Purge(t *testing.T) {
|
||||
t.Run("Purge with full document", func(t *testing.T) {
|
||||
document := ostest.Testdata.Resources.Full
|
||||
tc.Require.DocumentCreate(index, document.ID, toJSON(t, document))
|
||||
tc.Require.IndicesCount([]string{index}, 1)
|
||||
tc.Require.IndicesCount([]string{index}, "", 1)
|
||||
|
||||
assert.NoError(t, engine.Purge(document.ID))
|
||||
|
||||
tc.Require.IndicesCount([]string{index}, 0)
|
||||
tc.Require.IndicesCount([]string{index}, "", 0)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ type TestClient struct {
|
||||
|
||||
func NewDefaultTestClient(t *testing.T) *TestClient {
|
||||
client, err := opensearchgoAPI.NewDefaultClient()
|
||||
require.NoError(t, err, "Failed to create OpenSearch client")
|
||||
require.NoError(t, err, "failed to create OpenSearch client")
|
||||
|
||||
return NewTestClient(t, client)
|
||||
}
|
||||
@@ -92,7 +92,7 @@ func (tc *TestClient) IndicesRefresh(ctx context.Context, indices []string, allo
|
||||
}
|
||||
|
||||
func (tc *TestClient) IndicesDelete(ctx context.Context, indices []string) error {
|
||||
if err := tc.IndicesRefresh(ctx, indices, []int{404}); err != nil {
|
||||
if err := tc.IndicesRefresh(ctx, indices, []int{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -109,13 +109,14 @@ func (tc *TestClient) IndicesDelete(ctx context.Context, indices []string) error
|
||||
}
|
||||
}
|
||||
|
||||
func (tc *TestClient) IndicesCount(ctx context.Context, indices []string) (int, error) {
|
||||
func (tc *TestClient) IndicesCount(ctx context.Context, indices []string, body string) (int, error) {
|
||||
if err := tc.IndicesRefresh(ctx, indices, []int{404}); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
resp, err := tc.c.Indices.Count(ctx, &opensearchgoAPI.IndicesCountReq{
|
||||
Indices: indices,
|
||||
Body: strings.NewReader(body),
|
||||
})
|
||||
|
||||
switch {
|
||||
@@ -126,26 +127,6 @@ func (tc *TestClient) IndicesCount(ctx context.Context, indices []string) (int,
|
||||
}
|
||||
}
|
||||
|
||||
func (tc *TestClient) IndexCreate(ctx context.Context, index string, body string) error {
|
||||
if err := tc.IndicesRefresh(ctx, []string{index}, []int{404}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
resp, err := tc.c.Indices.Create(ctx, opensearchgoAPI.IndicesCreateReq{
|
||||
Index: index,
|
||||
Body: strings.NewReader(body),
|
||||
})
|
||||
|
||||
switch {
|
||||
case err != nil:
|
||||
return fmt.Errorf("failed to create index %s: %w", index, err)
|
||||
case !resp.Acknowledged:
|
||||
return fmt.Errorf("index creation not acknowledged for index %s", index)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (tc *TestClient) DocumentCreate(ctx context.Context, index string, id, body string) error {
|
||||
if err := tc.IndicesRefresh(ctx, []string{index}, []int{404}); err != nil {
|
||||
return err
|
||||
@@ -173,18 +154,6 @@ func (trc *testRequireClient) IndicesReset(indices []string) {
|
||||
require.NoError(trc.t, trc.tc.IndicesReset(trc.t.Context(), indices))
|
||||
}
|
||||
|
||||
func (trc *testRequireClient) IndicesExists(indices []string, expected bool) {
|
||||
exist, err := trc.tc.IndicesExists(trc.t.Context(), indices)
|
||||
switch {
|
||||
case expected == true:
|
||||
require.NoError(trc.t, err, "Expected indices to exist, but got an error")
|
||||
require.True(trc.t, exist, "Expected indices to exist, but got an error response")
|
||||
default:
|
||||
require.Error(trc.t, err)
|
||||
require.False(trc.t, exist, "Expected indices to not exist, but got an error response")
|
||||
}
|
||||
}
|
||||
|
||||
func (trc *testRequireClient) IndicesRefresh(indices []string, ignore []int) {
|
||||
require.NoError(trc.t, trc.tc.IndicesRefresh(trc.t.Context(), indices, ignore))
|
||||
}
|
||||
@@ -193,22 +162,18 @@ func (trc *testRequireClient) IndicesDelete(indices []string) {
|
||||
require.NoError(trc.t, trc.tc.IndicesDelete(trc.t.Context(), indices))
|
||||
}
|
||||
|
||||
func (trc *testRequireClient) IndicesCount(indices []string, expected int) {
|
||||
count, err := trc.tc.IndicesCount(trc.t.Context(), indices)
|
||||
func (trc *testRequireClient) IndicesCount(indices []string, body string, expected int) {
|
||||
count, err := trc.tc.IndicesCount(trc.t.Context(), indices, body)
|
||||
|
||||
switch {
|
||||
case expected <= 0:
|
||||
require.True(trc.t, count <= 0, "Expected indices to have no documents, but got a count of %d", count)
|
||||
require.True(trc.t, count <= 0, "expected indices to have no documents, but got a count of %d", count)
|
||||
default:
|
||||
require.Equal(trc.t, expected, count, "Expected indices to have %d documents, but got %d", expected, count)
|
||||
require.NoError(trc.t, err, "Expected indices to have documents, but got an error")
|
||||
require.Equal(trc.t, expected, count, "expected indices to have %d documents, but got %d", expected, count)
|
||||
require.NoError(trc.t, err, "expected indices to have documents, but got an error")
|
||||
}
|
||||
}
|
||||
|
||||
func (trc *testRequireClient) IndexCreate(index string, body string) {
|
||||
require.NoError(trc.t, trc.tc.IndexCreate(trc.t.Context(), index, body))
|
||||
}
|
||||
|
||||
func (trc *testRequireClient) DocumentCreate(index string, id, body string) {
|
||||
require.NoError(trc.t, trc.tc.DocumentCreate(trc.t.Context(), index, id, body))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user