PollyElasticsearch 1.0.0

dotnet add package PollyElasticsearch --version 1.0.0
                    
NuGet\Install-Package PollyElasticsearch -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PollyElasticsearch" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PollyElasticsearch" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PollyElasticsearch" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add PollyElasticsearch --version 1.0.0
                    
#r "nuget: PollyElasticsearch, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package PollyElasticsearch@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=PollyElasticsearch&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PollyElasticsearch&version=1.0.0
                    
Install as a Cake Tool

PollyElasticsearch

NuGet NuGet Downloads CI License: MIT

Polly v8 resilience for Elastic.Clients.Elasticsearch 8+ — add retry, timeout, and circuit-breaker to any Elasticsearch operation in two lines.

var client = new ElasticsearchClient(settings);

var resilient = client.WithPolly(pipeline => pipeline
    .AddRetry(new RetryStrategyOptions
    {
        MaxRetryAttempts = 3,
        Delay = TimeSpan.FromSeconds(1),
        BackoffType = DelayBackoffType.Exponential,
        ShouldHandle = ElasticTransientErrors.IsTransient,
    })
    .AddTimeout(TimeSpan.FromSeconds(10)));

var result = await resilient.ExecuteAsync((c, ct) => c.SearchAsync<Product>(s => s
    .Index("products")
    .Query(q => q.Match(m => m.Field(f => f.Name).Query("laptop"))), ct));

Why PollyElasticsearch?

Elastic.Clients.Elasticsearch does not natively integrate with Polly. This library bridges the gap:

Problem Solution
TransportException on connection failure Caught by ElasticTransientErrors.IsTransient
HTTP 429 rate-limit from Elasticsearch / proxy Auto-thrown as ElasticTransientException and retried
HTTP 503 cluster maintenance / rolling restart Auto-thrown as ElasticTransientException and retried
HTTP 504 gateway timeout behind a load balancer Auto-thrown as ElasticTransientException and retried
Slow queries blocking thread pool Wrap with AddTimeout
Cascading failures during outage Wrap with AddCircuitBreaker

Installation

dotnet add package PollyElasticsearch
dotnet add package Polly.Core

Quick-start

1. Manual wiring

using Polly;
using Polly.Retry;

var client = new ElasticsearchClient(
    new ElasticsearchClientSettings(new Uri("https://my-cluster:9200")));

var resilient = client.WithPolly(p => p
    .AddRetry(new RetryStrategyOptions
    {
        MaxRetryAttempts = 3,
        Delay = TimeSpan.FromSeconds(2),
        BackoffType = DelayBackoffType.Exponential,
        ShouldHandle = ElasticTransientErrors.IsTransient,
    }));

// Every call is now wrapped in the Polly pipeline.
var response = await resilient.ExecuteAsync(
    (c, ct) => c.GetAsync<Product>("products", "doc-id", ct));

2. Dependency injection

// Program.cs / Startup.cs
builder.Services.AddSingleton(new ElasticsearchClient(
    new ElasticsearchClientSettings(new Uri("https://my-cluster:9200"))));

builder.Services.AddPollyElasticsearch(pipeline => pipeline
    .AddRetry(new RetryStrategyOptions
    {
        MaxRetryAttempts = 3,
        Delay = TimeSpan.FromSeconds(1),
        BackoffType = DelayBackoffType.Exponential,
        ShouldHandle = ElasticTransientErrors.IsTransient,
    })
    .AddTimeout(TimeSpan.FromSeconds(10)));

// Inject ResilientElasticsearchClient into your services
public class ProductService(ResilientElasticsearchClient client)
{
    public Task<SearchResponse<Product>> SearchAsync(string q, CancellationToken ct) =>
        client.ExecuteAsync((c, ct2) => c.SearchAsync<Product>(s => s
            .Index("products")
            .Query(q2 => q2.Match(m => m.Field(f => f.Name).Query(q))), ct2), ct);
}

3. With a URI shortcut

builder.Services.AddPollyElasticsearch(
    new Uri("https://my-cluster:9200"),
    pipeline => pipeline
        .AddRetry(new RetryStrategyOptions
        {
            MaxRetryAttempts = 5,
            ShouldHandle = ElasticTransientErrors.IsTransient,
        }));

Transient error reference

// Use in any Polly strategy:
ShouldHandle = ElasticTransientErrors.IsTransient
Condition Why it's transient
ElasticTransientException (HTTP 429) Rate limited — back off and retry
ElasticTransientException (HTTP 503) Cluster down / maintenance — retry later
ElasticTransientException (HTTP 504) Proxy/load-balancer timeout — retry
TransportException Network failure or connection refused

Note: ElasticTransientException is thrown automatically by ResilientElasticsearchClient when the response HTTP status code is in ElasticTransientErrors.StatusCodes (429, 503, 504). You do not need to throw it yourself.

Checking the status code

.AddRetry(new RetryStrategyOptions
{
    ShouldHandle = new PredicateBuilder()
        .Handle<ElasticTransientException>(ex => ex.StatusCode == 429),
    MaxRetryAttempts = 5,
    Delay = TimeSpan.FromSeconds(10),  // respect 429 back-off window
})

Advanced pipelines

Full production pipeline

client.WithPolly(p => p
    .AddTimeout(TimeSpan.FromSeconds(30))          // total call timeout
    .AddRetry(new RetryStrategyOptions
    {
        MaxRetryAttempts = 4,
        Delay = TimeSpan.FromSeconds(1),
        BackoffType = DelayBackoffType.Exponential,
        UseJitter = true,
        ShouldHandle = ElasticTransientErrors.IsTransient,
    })
    .AddCircuitBreaker(new CircuitBreakerStrategyOptions
    {
        FailureRatio = 0.5,
        SamplingDuration = TimeSpan.FromSeconds(30),
        MinimumThroughput = 10,
        BreakDuration = TimeSpan.FromSeconds(15),
        ShouldHandle = ElasticTransientErrors.IsTransient,
    }));

Observability via Polly events

.AddRetry(new RetryStrategyOptions
{
    ShouldHandle = ElasticTransientErrors.IsTransient,
    OnRetry = args =>
    {
        logger.LogWarning("Elasticsearch retry {Attempt} after {Delay}ms — {Exception}",
            args.AttemptNumber, args.RetryDelay.TotalMilliseconds, args.Outcome.Exception?.Message);
        return ValueTask.CompletedTask;
    },
})

API reference

ResilientElasticsearchClient

Member Description
Inner The underlying ElasticsearchClient
ExecuteAsync<TResponse>(operation, ct) Executes operation through the pipeline; throws ElasticTransientException for 429/503/504

ElasticTransientErrors

Member Description
IsTransient PredicateBuilder for ElasticTransientException + TransportException
StatusCodes IReadOnlySet<int>{429, 503, 504}

ElasticTransientException

Member Description
StatusCode The HTTP status code that triggered the exception
Message Human-readable description including the status code

Extension methods

Method Description
client.WithPolly(pipeline) Wraps an ElasticsearchClient with a pre-built ResiliencePipeline
client.WithPolly(configure) Builds a pipeline inline and wraps the client

DI extensions

Method Description
services.AddPollyElasticsearch(configure) Registers ResiliencePipeline + ResilientElasticsearchClient (requires ElasticsearchClient already in DI)
services.AddPollyElasticsearch(uri, configure) Registers ElasticsearchClient for uri, then pipeline + resilient client

Target frameworks

Framework Supported
.NET 6
.NET 8
.NET 9
Package Description
PollyRedis Polly v8 for StackExchange.Redis
PollyEFCore Polly v8 for Entity Framework Core
PollyDapper Polly v8 for Dapper
PollyMongo Polly v8 for MongoDB
PollyNpgsql Polly v8 for Npgsql (PostgreSQL)
PollySqlClient Polly v8 for Microsoft.Data.SqlClient
PollyCosmosDb Polly v8 for Azure Cosmos DB
PollyAzureBlob Polly v8 for Azure Blob Storage
PollyAzureServiceBus Polly v8 for Azure Service Bus
PollyGrpc Polly v8 for gRPC
PollyRabbitMQ Polly v8 for RabbitMQ
PollyKafka Polly v8 for Confluent.Kafka
PollySignalR Polly v8 for SignalR
PollyOpenAI Polly v8 for OpenAI .NET SDK
PollyMediatR Polly v8 for MediatR
PollyHealthChecks Polly v8 for ASP.NET Core Health Checks
PollyBackoff Polly v8 backoff helpers

License

MIT © Justin Bannister

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 49 6/24/2026

1.0.0: Initial release. ResilientElasticsearchClient wraps ElasticsearchClient in a Polly v8 ResiliencePipeline. Includes ElasticTransientErrors helper with pre-built ShouldHandle predicate covering 429, 503, 504, and TransportException. Auto-throws ElasticTransientException for retryable HTTP responses. Supports net6.0, net8.0, and net9.0.