using Elasticsearch.Net; using Nest; namespace Admin.NET.Core; /// /// ES服务注册 /// public static class ElasticSearchSetup { public static void AddElasticSearch(this IServiceCollection services) { var enabled = App.GetConfig("Logging:ElasticSearch:Enabled"); if (!enabled) return; var serverUris = App.GetConfig>("Logging:ElasticSearch:ServerUris"); var defaultIndex = App.GetConfig("Logging:ElasticSearch:DefaultIndex"); var uris = serverUris.Select(u => new Uri(u)); var connectionPool = new SniffingConnectionPool(uris); var settings = new ConnectionSettings(connectionPool).DefaultIndex(defaultIndex); var client = new ElasticClient(settings); client.Indices.Create(defaultIndex, i => i.Map(m => m.AutoMap())); services.AddSingleton(client); // 单例注册 } }