.NET Aspire Elasticsearch integration
Includes: Hosting integration and Client integration
Elasticsearch is a distributed, RESTful search and analytics engine, scalable data store, and vector database capable of addressing a growing number of use cases. The .NET Aspire Elasticsearch integration enables you to connect to existing Elasticsearch instances, or create new instances from .NET with the docker.io/library/elasticsearch
container image.
Hosting integration
The Elasticsearch hosting integration models an Elasticsearch instance as the ElasticsearchResource type. To access this type and APIs that allow you to add it to your 📦 Aspire.Hosting.Elasticsearch NuGet package in the app host project.
dotnet add package Aspire.Hosting.Elasticsearch
For more information, see dotnet add package or Manage package dependencies in .NET applications.
Add Elasticsearch resource
In your app host project, call AddElasticsearch on the builder
instance to add an Elasticsearch resource:
var builder = DistributedApplication.CreateBuilder(args);
var elasticsearch = builder.AddElasticsearch("elasticsearch");
builder.AddProject<Projects.ExampleProject>()
.WithReference(elasticsearch);
// After adding all resources, run the app...
When .NET Aspire adds a container image to the app host, as shown in the preceding example with the docker.io/library/elasticsearch
image, it creates a new Elasticsearch instance on your local machine. A reference to your Elasticsearch resource (the elasticsearch
variable) is added to the ExampleProject
. The Elasticsearch resource includes default credentials with a username
of "elastic"
and randomly generated password
using the CreateDefaultPasswordParameter method when a password wasn't provided.
The WithReference method configures a connection in the ExampleProject
named "elasticsearch"
. For more information, see Container resource lifecycle.
Tip
If you'd rather connect to an existing Elasticsearch instance, call AddConnectionString instead. For more information, see Reference existing resources.
Add Elasticsearch resource with data volume
To add a data volume to the Elasticsearch resource, call the WithDataVolume method on the Elasticsearch resource:
var builder = DistributedApplication.CreateBuilder(args);
var elasticsearch = builder.AddElasticsearch("elasticsearch")
.WithDataVolume(isReadOnly: false);
builder.AddProject<Projects.ExampleProject>()
.WithReference(elasticsearch);
// After adding all resources, run the app...
The data volume is used to persist the Elasticsearch data outside the lifecycle of its container. The data volume is mounted at the /usr/share/elasticsearch/data
path in the Elasticsearch container and when a name
parameter isn't provided, the name is generated at random. For more information on data volumes and details on why they're preferred over bind mounts, see Docker docs: Volumes.
Add Elasticsearch resource with data bind mount
To add a data bind mount to the Elasticsearch resource, call the WithDataBindMount method:
var builder = DistributedApplication.CreateBuilder(args);
var elasticsearch = builder.AddElasticsearch("elasticsearch")
.WithDataBindMount(
source: @"C:\Elasticsearch\Data",
isReadOnly: false);
builder.AddProject<Projects.ExampleProject>()
.WithReference(elasticsearch);
// After adding all resources, run the app...
Important
Data bind mounts have limited functionality compared to volumes, which offer better performance, portability, and security, making them more suitable for production environments. However, bind mounts allow direct access and modification of files on the host system, ideal for development and testing where real-time changes are needed.
Data bind mounts rely on the host machine's filesystem to persist the Elasticsearch data across container restarts. The data bind mount is mounted at the C:\Elasticsearch\Data
on Windows (or /Elasticsearch/Data
on Unix) path on the host machine in the Elasticsearch container. For more information on data bind mounts, see Docker docs: Bind mounts.
Add Elasticsearch resource with password parameter
When you want to explicitly provide the password used by the container image, you can provide these credentials as parameters. Consider the following alternative example:
var builder = DistributedApplication.CreateBuilder(args);
var password = builder.AddParameter("password", secret: true);
var elasticsearch = builder.AddElasticsearch("elasticsearch", password);
builder.AddProject<Projects.ExampleProject>()
.WithReference(elasticsearch);
// After adding all resources, run the app...
For more information on providing parameters, see External parameters.
Hosting integration health checks
The Elasticsearch hosting integration automatically adds a health check for the Elasticsearch resource. The health check verifies that the Elasticsearch instance is running and that a connection can be established to it.
The hosting integration relies on the 📦 AspNetCore.HealthChecks.Elasticsearch NuGet package.
Client integration
To get started with the .NET Aspire Elasticsearch client integration, install the 📦 Aspire.Elastic.Clients.Elasticsearch NuGet package in the client-consuming project, that is, the project for the application that uses the Elasticsearch client. The Elasticsearch client integration registers an ElasticsearchClient instance that you can use to interact with Elasticsearch.
dotnet add package Aspire.Elastic.Clients.Elasticsearch
Add Elasticsearch client
In the Program.cs file of your client-consuming project, call the AddElasticsearchClient extension method on any IHostApplicationBuilder to register an ElasticsearchClient
for use via the dependency injection container. The method takes a connection name parameter.
builder.AddElasticsearchClient(connectionName: "elasticsearch");
Tip
The connectionName
parameter must match the name used when adding the Elasticsearch resource in the app host project. For more information, see Add Elasticsearch resource.
You can then retrieve the ElasticsearchClient
instance using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(ElasticsearchClient client)
{
// Use client...
}
Add keyed Elasticsearch client
There might be situations where you want to register multiple ElasticsearchClient
instances with different connection names. To register keyed Elasticsearch clients, call the AddKeyedElasticsearchClient:
builder.AddKeyedElasticsearchClient(name: "products");
builder.AddKeyedElasticsearchClient(name: "orders");
Then you can retrieve the ElasticsearchClient
instances using dependency injection. For example, to retrieve the connection from an example service:
public class ExampleService(
[FromKeyedServices("products")] ElasticsearchClient productsClient,
[FromKeyedServices("orders")] ElasticsearchClient ordersClient)
{
// Use clients...
}
For more information on keyed services, see .NET dependency injection: Keyed services.
Configuration
The .NET Aspire Elasticsearch client integration provides multiple options to configure the server connection based on the requirements and conventions of your project.
Use a connection string
When using a connection string from the ConnectionStrings
configuration section, you can provide the name of the connection string when calling builder.AddElasticsearchClient
:
builder.AddElasticsearchClient("elasticsearch");
Then the connection string will be retrieved from the ConnectionStrings
configuration section:
{
"ConnectionStrings": {
"elasticsearch": "http://elastic:password@localhost:27011"
}
}
Use configuration providers
The .NET Aspire Elasticsearch Client integration supports Microsoft.Extensions.Configuration. It loads the ElasticClientsElasticsearchSettings from configuration by using the Aspire:Elastic:Clients:Elasticsearch
key. Consider the following example appsettings.json that configures some of the options:
{
"Aspire": {
"Elastic": {
"Clients": {
"Elasticsearch": {
"DisableHealthChecks": false,
"DisableTracing": false,
"HealthCheckTimeout": "00:00:03",
"ApiKey": "<Valid ApiKey>",
"Endpoint": "http://elastic:password@localhost:27011",
"CloudId": "<Valid CloudId>"
}
}
}
}
}
For the complete Elasticsearch client integration JSON schema, see Aspire.Elastic.Clients.Elasticsearch/ConfigurationSchema.json.
Use inline delegates
Also you can pass the Action<ElasticClientsElasticsearchSettings> configureSettings
delegate to set up some or all the options inline, for example to set the API key from code:
builder.AddElasticsearchClient(
"elasticsearch",
static settings =>
settings.Endpoint = new Uri("http://elastic:password@localhost:27011"));
Use a CloudId
and an ApiKey
with configuration providers
When using Elastic Cloud, you can provide the CloudId
and ApiKey
in Aspire:Elastic:Clients:Elasticsearch
section when calling builder.AddElasticsearchClient
.
builder.AddElasticsearchClient("elasticsearch");
Consider the following example appsettings.json that configures the options:
{
"Aspire": {
"Elastic": {
"Clients": {
"Elasticsearch": {
"ApiKey": "<Valid ApiKey>",
"CloudId": "<Valid CloudId>"
}
}
}
}
}
Use a CloudId
and an ApiKey
with inline delegates
builder.AddElasticsearchClient(
"elasticsearch",
static settings =>
{
settings.ApiKey = "<Valid ApiKey>";
settings.CloudId = "<Valid CloudId>";
});
Client integration health checks
By default, .NET Aspire integrations enable health checks for all services. For more information, see .NET Aspire integrations overview.
The .NET Aspire Elasticsearch integration uses the configured client to perform a PingAsync
. If the result is an HTTP 200 OK, the health check is considered healthy, otherwise it's unhealthy. Likewise, if there's an exception, the health check is considered unhealthy with the error propagating through the health check failure.
Observability and telemetry
.NET Aspire integrations automatically set up Logging, Tracing, and Metrics configurations, which are sometimes known as the pillars of observability. For more information about integration observability and telemetry, see .NET Aspire integrations overview. Depending on the backing service, some integrations may only support some of these features. For example, some integrations support logging and tracing, but not metrics. Telemetry features can also be disabled using the techniques presented in the Configuration section.
Tracing
The .NET Aspire Elasticsearch integration will emit the following tracing activities using OpenTelemetry:
Elastic.Transport