123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Elasticsearch.Net;
- using Microsoft.Extensions.Configuration;
- using Nest;
- using Nest.JsonNetSerializer;
- using Newtonsoft.Json;
- using Newtonsoft.Json.Serialization;
- namespace SupplierWeb.Service
- {
- //public class MyFirstCustomJsonNetSerializer : ConnectionSettingsAwareSerializerBase
- //{
- // public MyFirstCustomJsonNetSerializer(IElasticsearchSerializer builtinSerializer, IConnectionSettingsValues connectionSettings)
- // : base(builtinSerializer, connectionSettings) { }
- // protected override JsonSerializerSettings CreateJsonSerializerSettings() =>
- // new JsonSerializerSettings
- // {
- // NullValueHandling = NullValueHandling.Include
- // };
- // protected override void ModifyContractResolver(ConnectionSettingsAwareContractResolver resolver) =>
- // resolver.NamingStrategy = new SnakeCaseNamingStrategy();
- //}
- public class EsClientProvider : IEsClientProvider
- {
- private readonly IConfiguration _configuration;
- private ElasticClient _client;
- private ElasticLowLevelClient _LowLevelclient;
- public static string EsUrl;
- public EsClientProvider(IConfiguration configuration)
- {
- _configuration = configuration;
- EsUrl = _configuration["EsUrl"];
- }
- public ElasticClient GetClient()
- {
- if (_client != null)
- return _client;
- InitClient();
- return _client;
- }
- public ElasticLowLevelClient GetLowLevelClient()
- {
- if (_LowLevelclient != null)
- return _LowLevelclient;
- InitClient();
- return _LowLevelclient;
- }
- private void InitClient()
- {
- var node = new Uri(EsUrl);
- //_client = new ElasticClient(new ConnectionSettings(node).DefaultIndex("demo"));
- //_client = new ElasticClient(new ConnectionSettings(node));
- var pool = new SingleNodeConnectionPool(node);
- //var connectionSettings = new ConnectionSettings(
- // pool,
- // connection: new InMemoryConnection(),
- // sourceSerializer: (builtin, settings) => new MyFirstCustomJsonNetSerializer(builtin, settings))
- // .DefaultIndex("my-index");
- var connectionSettings =
- new ConnectionSettings(pool, sourceSerializer: (builtin, settings) => new JsonNetSerializer(
- builtin, settings,
- () => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Include },
- //resolver => new DefaultContractResolver()
- resolver => resolver.NamingStrategy = new Newtonsoft.Json.Serialization.DefaultNamingStrategy()
- ));
- _client = new ElasticClient(connectionSettings);
- var config = new ConnectionConfiguration(node);
- _LowLevelclient = new ElasticLowLevelClient(config);
- }
- }
- }
|