Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions services/search/pkg/opensearch/batch.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (b *Batch) Purge(id string, onlyDeleted bool) error {
case err != nil:
return fmt.Errorf("failed to delete by query: %w", err)
case len(resp.Failures) != 0:
return fmt.Errorf("failed to delete by query, failures: %v", resp.Failures)
return fmt.Errorf("failed to delete by query, failures: %s", resp.Failures)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resp.Failures is a []json.RawMessage, and json.RawMessage is a []byte without a String() method, so %v rendered the failures as byte arrays:

[[123 34 105 110 100 101 120 34 58 34 111 112 101 110 ...]]

%s prints what the API actually returned:

[{"index":"opencloud-demo-dbq","id":"1$1!3","cause":{"type":"cluster_block_exception","reason":"index [opencloud-demo-dbq] blocked by: [FORBIDDEN/8/index write (api)];"},"status":403}]

}

return nil
Expand Down Expand Up @@ -204,10 +204,28 @@ func (b *Batch) Push() error {
body.WriteString("\n")
}

if _, err := b.client.Bulk(context.Background(), opensearchgoAPI.BulkReq{
resp, err := b.client.Bulk(context.Background(), opensearchgoAPI.BulkReq{
Body: strings.NewReader(body.String()),
}); err != nil {
})
switch {
case err != nil:
return fmt.Errorf("failed to execute bulk operations: %w", err)
case resp.Errors:
var failed []opensearchgoAPI.BulkRespItem
for _, item := range resp.Items {
for _, result := range item {
if result.Error != nil {
failed = append(failed, result)
}
}
}

failures, err := json.Marshal(failed)
if err != nil {
return fmt.Errorf("failed to marshal bulk failures: %w", err)
}

return fmt.Errorf("failed to execute bulk operations, failures: %s", failures)
}

bulkOperations = nil
Expand Down
53 changes: 53 additions & 0 deletions services/search/pkg/opensearch/batch_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package opensearch_test

import (
"strings"
"testing"

"github.com/stretchr/testify/require"

"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch"
"github.com/opencloud-eu/opencloud/services/search/pkg/opensearch/internal/test"
)

func TestBatch_Push(t *testing.T) {
tc := opensearchtest.NewDefaultTestClient(t, defaultConfig.Engine.OpenSearch.Client)

t.Run("reports the documents the bulk API rejected", func(t *testing.T) {
indexName := "opencloud-test-batch-push-rejected"
tc.Require.IndicesReset([]string{indexName})
defer tc.Require.IndicesDelete([]string{indexName})

// Name is a string, mapping it as a long makes every document fail to parse.
tc.Require.IndicesCreate(indexName, strings.NewReader(`{"mappings":{"properties":{"Name":{"type":"long"}}}}`))

batch, err := opensearch.NewBatch(tc.Client(), indexName, 10)
require.NoError(t, err)

document := opensearchtest.Testdata.Resources.File
require.NoError(t, batch.Upsert(document.ID, document))

err = batch.Push()
require.Error(t, err)
require.ErrorContains(t, err, document.ID)
require.ErrorContains(t, err, "mapper_parsing_exception")
tc.Require.IndicesCount([]string{indexName}, nil, 0)
})

t.Run("pushes the documents the bulk API accepted", func(t *testing.T) {
indexName := "opencloud-test-batch-push-accepted"
tc.Require.IndicesReset([]string{indexName})
defer tc.Require.IndicesDelete([]string{indexName})

tc.Require.IndicesCreate(indexName, strings.NewReader(opensearch.IndexManagerLatest.String()))

batch, err := opensearch.NewBatch(tc.Client(), indexName, 10)
require.NoError(t, err)

document := opensearchtest.Testdata.Resources.File
require.NoError(t, batch.Upsert(document.ID, document))
require.NoError(t, batch.Push())

tc.Require.IndicesCount([]string{indexName}, nil, 1)
})
}