Extending the Elasticsearch Service in Sitefinity

Overview

PREREQUISITES: This feature is available as of Sitefinity CMS 15.4.8637.

The ElasticsearchService class has been refactored to provide greater extensibility. Client initialization has been moved into dedicated factory methods, and the Elasticsearch client instance is now exposed through a lazy-initialized protected property. These changes allow derived classes to customize client configuration or replace the client entirely without modifying the base implementation.

Summary of changes

Lazy-initialized ElasticClient property

The Elasticsearch client is no longer instantiated directly in the constructor. Instead, a protected property named ElasticClient defers creation until the first time it is accessed. It calls the CreateElasticClient() method, which can be overridden in subclasses.

Overridable client creation methods

The following extension points have been introduced to allow consumers to customize the Elasticsearch client construction pipeline:

MethodAccessibilityPurpose
CreateElasticClient()protected virtualProvides complete control over client instantiation. Override this method when you need to supply an entirely custom ElasticsearchClient instance — for example, when integrating a custom HTTP transport, injecting interceptors, or applying non-standard connection pooling strategies. The default implementation calls GetElasticClientSettings() and passes the resulting settings to the ElasticsearchClient constructor.
ConfigureElasticClientSettings(ElasticsearchClientSettings settings)protected virtualOffers a lightweight customization hook that is invoked during settings construction. Override this method to append additional configuration, such as custom HTTP headers, proxy settings, or request timeout adjustments, without replacing the entire client creation logic. This method is called by GetElasticClientSettings() after all default configuration has been applied.
GetElasticClientSettings()protectedConstructs and returns the default ElasticsearchClientSettings instance with the service URI, authentication credentials, required mappings, and direct streaming enabled. This method guarantees a consistent baseline configuration. It calls ConfigureElasticClientSettings before returning, allowing derived classes to augment the settings.

Usage example

Adding custom headers via ConfigureElasticClientSettings

When you need only minor adjustments to the client settings, override ConfigureElasticClientSettings. This keeps the base settings (including the endpoint URI, authentication, and default mappings) intact:

C#
public class CustomElasticsearchService : ElasticsearchService
{

    protected override void ConfigureElasticClientSettings(ElasticsearchClientSettings settings)
    {
        // Add custom headers
        settings.GlobalHeaders(new NameValueCollection
        {
            { "X-Custom-Header", "my-value" }
        });
    }
}
Want to learn more?
Enhance your Sitefinity skills by enrolling in free training sessions. Become Sitefinity certified through Progress Education Community to strengthen your professional credentials.