EsClientProvider.cs 935 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.Extensions.Configuration;
  6. using Nest;
  7. namespace SupplierWeb.Service
  8. {
  9. public class EsClientProvider : IEsClientProvider
  10. {
  11. private readonly IConfiguration _configuration;
  12. private ElasticClient _client;
  13. public EsClientProvider(IConfiguration configuration)
  14. {
  15. _configuration = configuration;
  16. }
  17. public ElasticClient GetClient()
  18. {
  19. if (_client != null)
  20. return _client;
  21. InitClient();
  22. return _client;
  23. }
  24. private void InitClient()
  25. {
  26. var node = new Uri(_configuration["EsUrl"]);
  27. //_client = new ElasticClient(new ConnectionSettings(node).DefaultIndex("demo"));
  28. _client = new ElasticClient(new ConnectionSettings(node));
  29. }
  30. }
  31. }