在弹性搜索中检查IndexExists()时出错404

时间:2015-04-02 06:55:01

标签: elasticsearch nest

我有一个功能,可以通过弹性搜索创建连接并推送文档。

public static void CreateIndex<T>(string indexName, string aliasName, List<T> documents) where T : class
        {
            ElasticClient elasticClient = CreateElasticClient(indexName);

            // create new bucket if not exists
            if (!elasticClient.IndexExists(i => i.Index(indexName)).Exists)
            {
                elasticClient.CreateIndex(indexName, s => s.AddMapping<T>(m => m
                .MapFromAttributes()));
            }

            // create indexes
            var response = elasticClient.IndexMany<T>(documents);
         }

如果索引不存在,则给出404,远程服务器不存在flexClient.IndexExists()的错误。但仍然在弹性搜索中创建文档以及新索引。 下面是我用来建立连接的函数(CreateElasticClient)

private static ElasticClient CreateElasticClient(string indexName)
        {
            var connectionString = "http://localhost:9200/"
            Uri uri = new Uri(connectionString);
            ConnectionSettings settings = new ConnectionSettings(uri, defaultIndex: indexName);
            settings.SetDefaultPropertyNameInferrer(p => p);
            ElasticClient elasticClient = new ElasticClient(settings);

            var rootNodeInfo = elasticClient.RootNodeInfo();

            if (!rootNodeInfo.ConnectionStatus.Success)
            {
                throw new ApplicationException("Could not connect to Elastic search!", rootNodeInfo.ConnectionStatus.OriginalException);
            }

            return elasticClient;
        }

0 个答案:

没有答案