I can't understand something in the opensearch-go example, is it a mistake?

Looking at this:

When it defines the mapping, it uses opensearchapi.CreateRequest() but as far as I can see that doesn’t send anything and would need a Do() call. So is that a no-op or does it have some effect I’m not aware of?

I tried adding a Do() but Opensearch complains about the Document ID being missing like this:

[400 Bad Request] {“error”:{“root_cause”:[{“type”:“illegal_argument_exception”,“reason”:“if _id is specified it must not be empty”}],“type”:“illegal_argument_exception”,“reason”:“if _id is specified it must not be empty”},“status”:400}

So then how am I supposed to create a mapping and should the example be updated?
Thanks for reading.

@nethlaria CreateRequest is not a method, instead, it only initialize parameters for the request. The method ‘Do’ will perform action based on CreateRequest parameters. Can you post your complete code so that i can understand why your request fails? Thanks

@Vijay Thanks for responding. I was really asking about the example code in the User Guide. Let me put it another way: If that CreateRequest does not send anything, what is the mapping string for?

@nethlaria I understood your question now. You are right. The example is incomplete. I created a bug to update the same. Thanks for bringing this up.

1 Like

In the meantime, i will do something like below to create index

	mapping := map[string]interface{}{
		"settings": map[string]interface{}{
			"index": map[string]interface{}{
				"number_of_shards": 4,
				},
			},
	}
	jsonBody, _ := json.Marshal(mapping)
	// Create an index with non-default settings.
	indexCreateRequest := opensearchapi.IndicesCreateRequest{
		Index: IndexName,
		Body:  bytes.NewReader(jsonBody),
	}



	indexResponse, err := indexCreateRequest.Do(context.Background(), client)
	if err != nil {
		fmt.Println("failed to create index ", err)
		os.Exit(1)
	}
	fmt.Println(indexResponse)
1 Like