Skip to content

[BUG] Expecting error in bulk upserts #662

@bentcoder

Description

@bentcoder

What is the bug?

To be fair I am not sure if this is a bug or an intentional design choice.

When I insert many docs in one go using Bulk Request API, there is no way of knowing how many docs have failed due to bad data they contain. For instance, if you insert a single doc (I know it is a bad doc) using non-bulk operation, you will get 400 error telling you what the problem was. However, bulk API carries on without an error.

How can one reproduce the bug?

// github.com/opensearch-project/opensearch-go/v4/opensearchapi
// Client: *opensearchapi.Client
func (o Opensearch) UpsertOffers(ctx context.Context, offers []*document.Offer) error {
	var body strings.Builder

	for _, offer := range offers {
		body.WriteString(fmt.Sprintf(`{"update":{"_id":"%s","_index":"%s"}}`, offer.Path, "my_index"))
		body.WriteString("\n")

		doc, err := json.Marshal(map[string]any{"doc": offer, "doc_as_upsert": true})
		if err != nil {
			return err
		}

		body.Write(doc)
		body.WriteString("\n")
	}

	req := opensearchapi.BulkReq{
		Body: strings.NewReader(body.String()),
	}

	res, err := o.Client.Client.Do(ctx, req, nil)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	if res.IsError() {
		return fmt.Errorf("unexpected response: %s - %s", res.Status(), res.String())
	}

	return nil
}

What is the expected behavior?

At least res.IsError() or something could list failed docs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions