Connect to Elasticsearch
Esta página aún no está disponible en tu idioma.
This page describes how consuming apps connect to an Elasticsearch resource that’s already modeled in your AppHost. For the AppHost API surface — adding an Elasticsearch resource, data volumes, password parameters, and more — see Elasticsearch Hosting integration.
When you reference an Elasticsearch resource from your AppHost, Aspire injects the connection information into the consuming app as environment variables. Your app can either read those environment variables directly — the pattern works the same from any language — or, in C#, use the Aspire Elasticsearch client integration for automatic dependency injection, health checks, and telemetry.
Connection properties
Section titled “Connection properties”Aspire exposes each property as an environment variable named [RESOURCE]_[PROPERTY]. For instance, the Uri property of a resource called elasticsearch becomes ELASTICSEARCH_URI.
The Elasticsearch resource exposes the following connection properties:
| Property Name | Description |
|---|---|
Uri | The connection URI with format http://elastic:{Password}@{Host}:{Port} |
Example connection string:
Uri: http://elastic:p%40ssw0rd1@localhost:9200Connect from your app
Section titled “Connect from your app”Pick the language your consuming app is written in. Each example assumes your AppHost adds an Elasticsearch resource named elasticsearch and references it from the consuming app.
For C# apps, the recommended approach is the Aspire Elasticsearch client integration. It registers an ElasticsearchClient through dependency injection and adds health checks and telemetry automatically. If you’d rather read environment variables directly, see the Read environment variables section at the end of this tab.
Install the client integration
Section titled “Install the client integration”Install the 📦 Aspire.Elastic.Clients.Elasticsearch NuGet package in the client-consuming project:
dotnet add package Aspire.Elastic.Clients.Elasticsearch#:package Aspire.Elastic.Clients.Elasticsearch@*<PackageReference Include="Aspire.Elastic.Clients.Elasticsearch" Version="*" />Add the Elasticsearch client
Section titled “Add the Elasticsearch client”In Program.cs, call AddElasticsearchClient on your IHostApplicationBuilder to register an ElasticsearchClient:
builder.AddElasticsearchClient(connectionName: "elasticsearch");Resolve the client through dependency injection:
public class ExampleService(ElasticsearchClient client){ // Use client...}Add keyed Elasticsearch clients
Section titled “Add keyed Elasticsearch clients”To register multiple ElasticsearchClient instances with different connection names, use AddKeyedElasticsearchClient:
builder.AddKeyedElasticsearchClient(name: "products");builder.AddKeyedElasticsearchClient(name: "orders");Then resolve each instance by key:
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
Section titled “Configuration”The Aspire Elasticsearch client integration offers multiple ways to provide configuration.
Connection strings. When using a connection string from the ConnectionStrings configuration section, pass the connection name to AddElasticsearchClient:
builder.AddElasticsearchClient("elasticsearch");The connection string is resolved from the ConnectionStrings section:
{ "ConnectionStrings": { "elasticsearch": "http://elastic:password@localhost:9200" }}Configuration providers. The client integration supports Microsoft.Extensions.Configuration. It loads ElasticClientsElasticsearchSettings from appsettings.json (or any other configuration source) by using the Aspire:Elastic:Clients:Elasticsearch key:
{ "Aspire": { "Elastic": { "Clients": { "Elasticsearch": { "DisableHealthChecks": false, "DisableTracing": false, "HealthCheckTimeout": "00:00:03", "ApiKey": "<Valid ApiKey>", "Endpoint": "http://elastic:password@localhost:9200", "CloudId": "<Valid CloudId>" } } } }}Inline delegates. Pass an Action<ElasticClientsElasticsearchSettings> to configure settings inline:
builder.AddElasticsearchClient( "elasticsearch", static settings => settings.Endpoint = new Uri("http://elastic:password@localhost:9200"));Elastic Cloud. When using Elastic Cloud, you can provide the CloudId and ApiKey:
builder.AddElasticsearchClient( "elasticsearch", static settings => { settings.ApiKey = "<Valid ApiKey>"; settings.CloudId = "<Valid CloudId>"; });Or via configuration providers:
{ "Aspire": { "Elastic": { "Clients": { "Elasticsearch": { "ApiKey": "<Valid ApiKey>", "CloudId": "<Valid CloudId>" } } } }}Client integration health checks
Section titled “Client integration health checks”Aspire client integrations enable health checks by default. The Elasticsearch client integration uses the configured client to perform a PingAsync. If the result is HTTP 200 OK, the health check is considered healthy; otherwise it’s unhealthy. The health check is wired into the /health HTTP endpoint, where all registered health checks must pass before the app is considered ready to accept traffic.
Observability and telemetry
Section titled “Observability and telemetry”The Aspire Elasticsearch client integration automatically configures tracing through OpenTelemetry.
Tracing activities:
Elastic.Transport
Any of these telemetry features can be disabled through the configuration options above.
Read environment variables in C#
Section titled “Read environment variables in C#”If you prefer not to use the Aspire client integration, you can read the Aspire-injected connection URI from the environment and pass it to the 📦 Elastic.Clients.Elasticsearch NuGet package directly:
using Elastic.Clients.Elasticsearch;
var uri = Environment.GetEnvironmentVariable("ELASTICSEARCH_URI");
var client = new ElasticsearchClient(new Uri(uri!));
// Use client to interact with Elasticsearch...Use go-elasticsearch, the official Go client for Elasticsearch:
go get github.com/elastic/go-elasticsearch/v8Read the injected environment variable and connect:
package main
import ( "os" elasticsearch8 "github.com/elastic/go-elasticsearch/v8")
func main() { // Read the Aspire-injected connection URI cfg := elasticsearch8.Config{ Addresses: []string{os.Getenv("ELASTICSEARCH_URI")}, }
es, err := elasticsearch8.NewClient(cfg) if err != nil { panic(err) }
res, err := es.Info() if err != nil { panic(err) } defer res.Body.Close()}Install elasticsearch, the official Python client for Elasticsearch:
pip install elasticsearchRead the injected environment variable and connect:
import osfrom elasticsearch import Elasticsearch
# Read the Aspire-injected connection URIes = Elasticsearch(os.getenv("ELASTICSEARCH_URI"))
# Use client to interact with Elasticsearch...response = es.ping()print(f"Elasticsearch connection status: {response}")Install @elastic/elasticsearch, the official Node.js client for Elasticsearch:
npm install @elastic/elasticsearchRead the injected environment variable and connect:
import { Client } from '@elastic/elasticsearch';
// Read the Aspire-injected connection URIconst client = new Client({ node: process.env.ELASTICSEARCH_URI,});
// Use client to interact with Elasticsearch...const response = await client.ping();console.log('Elasticsearch connection status:', response);