Skip to content

Files

Latest commit

a2573c3 · Apr 16, 2025

History

History
140 lines (85 loc) · 3.36 KB

getting-started.md

File metadata and controls

140 lines (85 loc) · 3.36 KB
mapped_pages

Getting started [getting-started-net]

This page guides you through the installation process of the .NET client, shows you how to instantiate the client, and how to perform basic Elasticsearch operations with it.

Requirements [_requirements]

  • .NET Core, .NET 5+ or .NET Framework (4.6.1 and higher).

Installation [_installation]

To install the latest version of the client for SDK style projects, run the following command:

dotnet add package Elastic.Clients.Elasticsearch

Refer to the Installation page to learn more.

Connecting [_connecting]

You can connect to the Elastic Cloud using an API key and the Elasticsearch endpoint.

var client = new ElasticsearchClient("<CLOUD_ID>", new ApiKey("<API_KEY>"));

Your Elasticsearch endpoint can be found on the My deployment page of your deployment:

Finding Elasticsearch endpoint

You can generate an API key on the Management page under Security.

Create API key

For other connection options, refer to the Connecting section.

Operations [_operations]

Time to use Elasticsearch! This section walks you through the basic, and most important, operations of Elasticsearch. For more operations and more advanced examples, refer to the CRUD usage examples page.

Creating an index [_creating_an_index]

This is how you create the my_index index:

var response = await client.Indices.CreateAsync("my_index");

Indexing documents [_indexing_documents]

This is a simple way of indexing a document:

var doc = new MyDoc
{
    Id = 1,
    User = "flobernd",
    Message = "Trying out the client, so far so good?"
};

var response = await client.IndexAsync(doc, "my_index");

Getting documents [_getting_documents]

You can get documents by using the following code:

var response = await client.GetAsync<MyDoc>(id, idx => idx.Index("my_index"));

if (response.IsValidResponse)
{
    var doc = response.Source;
}

Searching documents [_searching_documents]

This is how you can create a single match query with the .NET client:

var response = await client.SearchAsync<MyDoc>(s => s
    .Index("my_index")
    .From(0)
    .Size(10)
    .Query(q => q
        .Term(t => t.User, "flobernd")
    )
);

if (response.IsValidResponse)
{
    var doc = response.Documents.FirstOrDefault();
}

Updating documents [_updating_documents]

This is how you can update a document, for example to add a new field:

doc.Message = "This is a new message";

var response = await client.UpdateAsync<MyDoc, MyDoc>("my_index", 1, u => u
    .Doc(doc));

Deleting documents [_deleting_documents]

var response = await client.DeleteAsync("my_index", 1);

Deleting an index [_deleting_an_index]

var response = await client.Indices.DeleteAsync("my_index");

Further reading [_further_reading]

  • Refer to the Usage recommendations page to learn more about how to use the client the most efficiently.