如何检查Elastic Search服务器的运行状况?

时间:2017-02-17 11:48:47

标签: c# asp.net elasticsearch elasticsearch-plugin

我从Nuget包管理器下载了弹性搜索客户端Nest。

我无法识别检查服务器运行状况的功能。

请建议。

3 个答案:

答案 0 :(得分:2)

您需要使用NEST版本7.X

client.Cluster.Health();

答案 1 :(得分:0)

您需要从nuget包管理器安装ElasticSearch.net和Nest,两者必须具有相同的版本。

这是使用Nest Elastic Client检查状态的方法。

 public string HealthCheck(WaitForStatus waitForStatus = WaitForStatus.Red, bool logResults = false)
   {
             var response = this._client.ClusterHealth(new ClusterHealthRequest() { WaitForStatus = waitForStatus });

             var healthColor = response.Status.ToLower();

            // this will give you the color code of the elastic search server health.

            switch (healthColor)
                    {
                        case "green":
                        var message = "ElasticSearch Server health check returned [GREEN]";
                       break;

                    case "yellow":
                        var message = "ElasticSearch Server health check returned [YELLOW] (yellow is normal for single node clusters) ";
                        break;

                    default: // Includes "red"
                        var message = "ElasticSearch Server health check returned [{0}]".FormatInLine(response.Status.ToUpper());
                        break;

                }

        return message;
   }

您可以通过收到的消息识别服务器的运行状况。 绿色和黄色是可以接受的,红色可能会导致一些问题。

谢谢............

答案 2 :(得分:0)

当未连接弹性服务器时,返回绿色状态,这是不正确的,在这种情况下,您必须检查响应。 更改的代码如下

    public string HealthCheck(WaitForStatus waitForStatus = WaitForStatus.Red, bool logResults = false)
    {
        string message = string.Empty;
        var client = new ElasticClient(new Uri("http://localhost:9200"));
        var response = client.ClusterHealth(new ClusterHealthRequest() { WaitForStatus = waitForStatus });

        var healthColor = response.Status.ToString().ToLower();

        // this will give you the color code of the elastic search server health.
        if (response.IsValid)
        {
            switch (healthColor)
            {
                case "green":
                    message = "ElasticSearch Server health check returned [GREEN]";
                    break;

                case "yellow":
                    message = "ElasticSearch Server health check returned [YELLOW] (yellow is normal for single node clusters) ";
                    break;

                default: // Includes "red"
                    message = string.Format("ElasticSearch Server health check returned [{0}]", response.Status.ToString().ToUpper());
                    break;

            }
        }
        else
            message = response.ApiCall.OriginalException.Message;

        return message;
    }