TrackingES.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using Nest;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using XYY.Data.Model.Standard.Tracking;
  8. namespace XYY.Service.Standard.TrackingService
  9. {
  10. public class TrackingES : IDisposable
  11. {
  12. private readonly string Url = "";
  13. private ElasticClient Client;
  14. private ElasticClient _supplementClient;
  15. public TrackingES(string url)
  16. {
  17. this.Url = url;
  18. }
  19. public ElasticClient GetElasticClient()
  20. {
  21. if (Client == null)
  22. {
  23. string esNodeUrl = this.Url;
  24. string indexName = "logistics_tracking";
  25. Uri node = new Uri(esNodeUrl);
  26. ConnectionSettings settings = new ConnectionSettings(node).DefaultMappingFor<Logistics_TraceGroup>(x =>
  27. x.IdProperty(i => i.TrackingNumber).IndexName(indexName).TypeName("trackingGroup"));
  28. //600s超时
  29. settings.RequestTimeout(new TimeSpan(0, 0, 600));
  30. IIndexState indexState = new IndexState()
  31. {
  32. Settings = new IndexSettings()
  33. {
  34. NumberOfReplicas = 0,//副本数
  35. NumberOfShards = 1//分片数
  36. }
  37. };
  38. this.Client = new ElasticClient(settings);
  39. if (!Client.IndexExists(indexName).Exists)
  40. {
  41. Client.CreateIndex(indexName, p => p.InitializeUsing(indexState));
  42. }
  43. }
  44. return Client;
  45. }
  46. /// <summary>
  47. /// 补充轨迹的节点
  48. /// </summary>
  49. /// <returns></returns>
  50. public ElasticClient GetSupplementElasticClient()
  51. {
  52. if (_supplementClient == null)
  53. {
  54. string esNodeUrl = this.Url;
  55. string indexName = "logistics_supplement";
  56. Uri node = new Uri(esNodeUrl);
  57. ConnectionSettings settings = new ConnectionSettings(node).DefaultMappingFor<Logistics_TraceGroup>(x =>
  58. x.IdProperty(i => i.TrackingNumber).IndexName(indexName).TypeName("supplement"));
  59. //600s超时
  60. settings.RequestTimeout(new TimeSpan(0, 0, 600));
  61. IIndexState indexState = new IndexState()
  62. {
  63. Settings = new IndexSettings()
  64. {
  65. NumberOfReplicas = 0,//副本数
  66. NumberOfShards = 1//分片数
  67. }
  68. };
  69. this._supplementClient = new ElasticClient(settings);
  70. if (!_supplementClient.IndexExists(indexName).Exists)
  71. {
  72. _supplementClient.CreateIndex(indexName, p => p.InitializeUsing(indexState));
  73. }
  74. }
  75. return _supplementClient;
  76. }
  77. public void Dispose()
  78. {
  79. }
  80. }
  81. }