【问题标题】:How to check if connection to Elasticsearch is established?如何检查与 Elasticsearch 的连接是否已建立?
【发布时间】:2018-01-11 23:14:38
【问题描述】:

我想检查与 Elasticsearch 数据库的连接是否正常。换句话说,我想 ping Elasticsearch。当我执行下面的代码时,会抛出异常。

    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() }
        };
    }

当我执行上面的方法时,抛出异常并收到以下消息:

嗅探集群状态失败。

如何检查与 Elasticsearch 的连接是否已建立?

【问题讨论】:

  • 您使用的是哪个版本的 NEST 以及您运行的是哪个版本的 Elasticsearch?在ConnectionSettings 上设置.DisableDirectStreaming() 以获取有关集群状态嗅探失败的更多详细信息。

标签: c# elasticsearch elasticsearch-net


【解决方案1】:

Nest IElasticClient 接口为此提供了一个Ping 方法

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我设法通过将 SniffingConnectionPool 更改为 SingleNodeConnectionPool 来解决此问题。

    【讨论】:

      猜你喜欢
      • 2015-11-30
      • 1970-01-01
      • 2020-03-31
      • 2015-08-04
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多