Getting started
editGetting started
editThis 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
edit- .NET Core, .NET 5+ or .NET Framework (4.6.1 and higher).
Installation
editTo 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
editYou can connect to the Elastic Cloud using an API key and your Elasticsearch Cloud ID.
var client = new ElasticsearchClient("<CLOUD_ID>", new ApiKey("<API_KEY>"));
You can find your Elasticsearch Cloud ID on the deployment page:
To generate an API key, use the Elasticsearch Create API key API or Kibana Stack Management.
For other connection options, refer to the Connecting section.
Operations
editTime 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
editThis is how you create the my_index
index:
var response = await client.Indices.CreateAsync("my_index");
Indexing documents
editThis 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
editYou 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
editThis 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
editThis 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
editvar response = await client.DeleteAsync("my_index", 1);
Deleting an index
editvar response = await client.Indices.DeleteAsync("my_index");
Further reading
edit- Refer to the Usage recommendations page to learn more about how to use the client the most efficiently.