Skip to content

Latest commit

 

History

History
47 lines (36 loc) · 1.11 KB

File metadata and controls

47 lines (36 loc) · 1.11 KB
endpoint create
lang dotnet
es_version 9.3
client Elastic.Clients.Elasticsearch==9.3.0

Elasticsearch 9.3 create endpoint (.NET example)

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.

Handling conflicts

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}");
}

Auto-generated IDs

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}");