| endpoint | create |
|---|---|
| lang | dotnet |
| es_version | 9.3 |
| client | Elastic.Clients.Elasticsearch==9.3.0 |
Use client.CreateAsync() to index a document only if it does not
already exist.
var product = new Product(
"Espresso Machine Pro", "BrewMaster",
899.99, "appliances", true, 4.7
);
var response = await client.CreateAsync("products", "prod-1", product);
Console.WriteLine($"{response.Result} document {response.Id}");Unlike IndexAsync, this will never replace an existing document.
If the document already exists, the response indicates a conflict:
var response = await client.CreateAsync("products", "prod-1", product);
if (!response.IsValidResponse)
{
Console.WriteLine($"Conflict: {response.ElasticsearchServerError?.Error.Reason}");
}Use IndexAsync with OpType.Create when you don't need to specify
an ID:
var response = await client.IndexAsync(product, x => x
.Index("products")
.OpType(OpType.Create));
Console.WriteLine($"Created with ID: {response.Id}");