以下代码使用Nest库,请用Elastic.Clients.Elasticsearch替换Nest的c#代码要怎么写呢? public IElasticClient…
- User
以下代码使用Nest库,请用Elastic.Clients.Elasticsearch替换Nest的c#代码要怎么写呢? public IElasticClient CreateConnection(string index = null) { var sparePartsIndex = _configuration.SparePartsIndex; if (index != null) { sparePartsIndex = index; } var settings = new ConnectionSettings(new Uri(_configuration.ElasticSearchUrl)); settings.MapDefaultTypeIndices(s => s. Add(typeof(SparePartEntity), sparePartsIndex) .Add(typeof(OperationLogEntity), sparePartsIndex) .Add(typeof(CatalogueData), sparePartsIndex) .Add(typeof(StatisticsService), sparePartsIndex) .Add(typeof(ImageEntity), sparePartsIndex) .Add(typeof(MetadataEntity), sparePartsIndex) .Add(typeof(SparePartImageSet), sparePartsIndex)); settings.MapDefaultTypeNames(fd => fd.Add(typeof(SparePartEntity), “sparepartentities”) .Add(typeof(MetadataEntity), “metadataentities”) ); settings.SetMaximumAsyncConnections(20); settings.SetTimeout(int.MaxValue); #if DEBUG //settings.SetProxy(new Uri(“http://webgateway.eu.schindler.com:3128/”), “GLOBAL\bachmam2”, “Sk@teboard”); //settings.SetProxy(new Uri(“http://localhost:8888”), null, null); settings.EnableTrace(); settings.UsePrettyResponses(); #endif
return new ElasticClient(settings); }
- Assistant
使用Elastic.Clients.Elasticsearch库,可以通过以下代码替换上述Nest的C#代码:
using Elasticsearch.Net; using Nest; public IElasticClient CreateConnection(string index = null) { var sparePartsIndex = _configuration.SparePartsIndex; if (index != null) { sparePartsIndex = index; } var settings = new ConnectionSettings(new SingleNodeConnectionPool(new Uri(_configuration.ElasticSearchUrl))) .DefaultMappingFor<SparePartEntity>(m => m .IndexName(sparePartsIndex)) .DefaultMappingFor<OperationLogEntity>(m => m .IndexName(sparePartsIndex)) .DefaultMappingFor<CatalogueData>(m => m .IndexName(sparePartsIndex)) .DefaultMappingFor<StatisticsService>(m => m .IndexName(sparePartsIndex)) .DefaultMappingFor<ImageEntity>(m => m .IndexName(sparePartsIndex)) .DefaultMappingFor<MetadataEntity>(m => m .IndexName(sparePartsIndex)) .DefaultMappingFor<SparePartImageSet>(m => m .IndexName(sparePartsIndex)) .DefaultFieldNameInferrer(type => type.Name.ToLower()) .DefaultTypeName("_doc") .RequestTimeout(TimeSpan.FromMilliseconds(int.MaxValue)) .MaximumRetries(0) .PrettyJson(); #if DEBUG //settings.Proxy(new Uri("http://webgateway.eu.schindler.com:3128/"), "GLOBAL\\bachmam2", "Sk@teboard"); //settings.Proxy(new Uri("http://localhost:8888"), null, null); settings.DisableDirectStreaming(); settings.OnRequestCompleted(response => { if (response.RequestBodyInBytes != null) { Console.WriteLine( $"{response.HttpMethod} {response.Uri} " + $"{Encoding.UTF8.GetString(response.RequestBodyInBytes)}"); } else { Console.WriteLine($"{response.HttpMethod} {response.Uri}"); } Console.WriteLine(); if (response.ResponseBodyInBytes != null) { Console.WriteLine($"Status: {response.HttpStatusCode}"); Console.WriteLine(Encoding.UTF8.GetString(response.ResponseBodyInBytes)); Console.WriteLine(); } }); #endif return new ElasticClient(settings); }