6

I want to check if the connection to Elasticsearch database is ok. In other words I want to ping Elasticsearch. When I execute the code below, an exception is thrown.

    public async Task<HealthCheckResult> Execute()
    {
        if (_configuration.Nodes?.Length == 0)
        {
            await Task.Delay(1);
            return new HealthCheckResult("Connection Failed - Missing elasticsearch connection string")
            {
                ChildResults = new List<HealthCheckResult>() {new HealthCheckResult()}
            };
        }

        var node = new Uri(_configuration.Nodes.First());


        try
        {
            var connectionPool = new SniffingConnectionPool(new[] {node});
            var settings = new ConnectionConfiguration(connectionPool);
            var client = new ElasticLowLevelClient(settings);
            client.IndicesExists<string>("applications");
        }
        catch (Exception exception)
        {
            return new HealthCheckResult(exception.Message)
            {
                ChildResults = new List<HealthCheckResult>() { new HealthCheckResult() }
            };
        }

        return new HealthCheckResult("Connection Passed")
        {
            ChildResults = new List<HealthCheckResult>() { new HealthCheckResult() }
        };
    }

When I execute method above, exception is thrown and I get this message:

Failed sniffing cluster state.

What can I do to check if the connection to Elasticsearch is established?

user247702
  • 23,641
  • 15
  • 110
  • 157
Jagjit Singh
  • 661
  • 9
  • 16
  • 1
    What version of NEST are you using and what version of Elasticsearch are you running against? Set `.DisableDirectStreaming()` on the `ConnectionSettings` to get more detail about the cluster state sniffing fail. – Russ Cam Feb 06 '17 at 08:35

2 Answers2

8

The Nest IElasticClient interface provides a Ping method for this purpose

Rob Willis
  • 4,706
  • 2
  • 28
  • 20
3

I was having the same problem and I managed to fix this by changing the SniffingConnectionPool to a SingleNodeConnectionPool.

Matthew Walton
  • 9,809
  • 3
  • 27
  • 36