当我启动我的程序时,我运行 ElasticSearch 服务并检查是否存在索引以及是否有任何文档,假设我只是运行 ES 服务并且我有这两个功能:
public ElasticClient getElasticSearchClient()
{
ConnectionSettings connectionSettings = new Nest.ConnectionSettings(new Uri("http://localhost:9200"))
.DefaultIndex("myindex")
.DisableDirectStreaming();
ElasticClient client = new ElasticClient(connectionSettings);
//var health = client.Cluster.Health("myindex", a => (a.WaitForStatus(WaitForStatus.Yellow)).Timeout(50));
return client;
}
public void checkElasticsearchIndex()
{
var client = getElasticSearchClient();
var health = this.client.Cluster.Health("myindex", a => (a.WaitForStatus(WaitForStatus.Yellow)));
CountResponse count = client.Count<myobject>();
if (!client.Indices.Exists("myindex").IsValid || count.Count == 0)
{
BulkWriteAllToIndexES(client);
}
}
在 checkElasticsearchIndex 函数内部,
计数操作失败并显示以下错误消息:
OriginalException:Elasticsearch.Net.ElasticsearchClientException:远程服务器返回错误:(503)服务器不可用..调用:状态代码503来自:GET /myindex/_count。ServerError:类型:search_phase_execution_exception 原因:“所有分片失败”---> System.Net.WebException:远程服务器返回错误:(503)服务器不可用。
健康也失败了:
OriginalException:Elasticsearch.Net.ElasticsearchClientException:无法连接到远程服务器。调用:状态码未知来自:GET /_cluster/health/myindex?wait_for_status=yellow ---> System.Net.WebException:无法连接到远程服务器 ---> System.Net.Sockets.SocketException:无法连接是因为目标机器主动拒绝它 127.0.0.1:9200
如您所见,我尝试了 Cluster WaitForStatus,但没有成功。
我的问题:有没有办法等到客户端/集群/节点准备好并且没有任何异常?