EsClientProvider.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Elasticsearch.Net;
  6. using Microsoft.Extensions.Configuration;
  7. using Nest;
  8. using Nest.JsonNetSerializer;
  9. using Newtonsoft.Json;
  10. using Newtonsoft.Json.Serialization;
  11. namespace SupplierWeb.Service
  12. {
  13. //public class MyFirstCustomJsonNetSerializer : ConnectionSettingsAwareSerializerBase
  14. //{
  15. // public MyFirstCustomJsonNetSerializer(IElasticsearchSerializer builtinSerializer, IConnectionSettingsValues connectionSettings)
  16. // : base(builtinSerializer, connectionSettings) { }
  17. // protected override JsonSerializerSettings CreateJsonSerializerSettings() =>
  18. // new JsonSerializerSettings
  19. // {
  20. // NullValueHandling = NullValueHandling.Include
  21. // };
  22. // protected override void ModifyContractResolver(ConnectionSettingsAwareContractResolver resolver) =>
  23. // resolver.NamingStrategy = new SnakeCaseNamingStrategy();
  24. //}
  25. public class EsClientProvider : IEsClientProvider
  26. {
  27. private readonly IConfiguration _configuration;
  28. private ElasticClient _client;
  29. private ElasticLowLevelClient _LowLevelclient;
  30. public static string EsUrl;
  31. public EsClientProvider(IConfiguration configuration)
  32. {
  33. _configuration = configuration;
  34. EsUrl = _configuration["EsUrl"];
  35. }
  36. public ElasticClient GetClient()
  37. {
  38. if (_client != null)
  39. return _client;
  40. InitClient();
  41. return _client;
  42. }
  43. public ElasticLowLevelClient GetLowLevelClient()
  44. {
  45. if (_LowLevelclient != null)
  46. return _LowLevelclient;
  47. InitClient();
  48. return _LowLevelclient;
  49. }
  50. private void InitClient()
  51. {
  52. var node = new Uri(EsUrl);
  53. //_client = new ElasticClient(new ConnectionSettings(node).DefaultIndex("demo"));
  54. //_client = new ElasticClient(new ConnectionSettings(node));
  55. var pool = new SingleNodeConnectionPool(node);
  56. //var connectionSettings = new ConnectionSettings(
  57. // pool,
  58. // connection: new InMemoryConnection(),
  59. // sourceSerializer: (builtin, settings) => new MyFirstCustomJsonNetSerializer(builtin, settings))
  60. // .DefaultIndex("my-index");
  61. var connectionSettings =
  62. new ConnectionSettings(pool, sourceSerializer: (builtin, settings) => new JsonNetSerializer(
  63. builtin, settings,
  64. () => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include },
  65. //resolver => new DefaultContractResolver()
  66. resolver => resolver.NamingStrategy = new Newtonsoft.Json.Serialization.DefaultNamingStrategy()
  67. ));
  68. _client = new ElasticClient(connectionSettings);
  69. var config = new ConnectionConfiguration(node);
  70. _LowLevelclient = new ElasticLowLevelClient(config);
  71. }
  72. }
  73. }