ES_Controller.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6. using CoreEntity.Entity;
  7. using Elasticsearch.Net;
  8. using Microsoft.AspNetCore.Http;
  9. using Microsoft.AspNetCore.Mvc;
  10. using Nest;
  11. using Newtonsoft.Json;
  12. using Newtonsoft.Json.Linq;
  13. using SupplierWeb.Commonss;
  14. using SupplierWeb.Service;
  15. namespace Elasticsearch.Controllers
  16. {
  17. public class MyPostData : PostData
  18. {
  19. public override void Write(Stream writableStream, IConnectionConfigurationValues settings)
  20. {
  21. writableStream.Write(WrittenBytes);
  22. }
  23. public override Task WriteAsync(Stream writableStream, IConnectionConfigurationValues settings, CancellationToken cancellationToken)
  24. {
  25. return null;
  26. }
  27. }
  28. [Route("web/value")]
  29. public class ES_Controller : ControllerBase
  30. {
  31. private readonly ElasticClient _esclient;
  32. private readonly ElasticLowLevelClient _LowLevelclient;
  33. public ES_Controller(IEsClientProvider clientProvider)
  34. {
  35. _esclient = clientProvider.GetClient();
  36. _LowLevelclient = clientProvider.GetLowLevelClient();
  37. }
  38. [HttpPost]
  39. [Route("search")]
  40. public QuerySqlResponse Seaarch(string sql,string index)//IReadOnlyCollection<Post>
  41. {
  42. //var result = _client.Search<Post>(s => s
  43. // .Index(index)
  44. // .From(0)
  45. // .Size(10)
  46. // .Query(q => q.Match(m => m.Field(f => f.Type).Query(type)))
  47. // ).Documents;
  48. QuerySqlRequest isql = new QuerySqlRequest();
  49. isql.Query = sql;
  50. isql.FetchSize = 10;
  51. isql.Format = "json";
  52. var result = _esclient.Sql.Query(isql);
  53. return result;
  54. }
  55. [HttpPost]
  56. [Route("searchd")]
  57. public dynamic SeaarchD([FromBody]dynamic data,string dsl, string index)//IReadOnlyCollection<Post>
  58. {
  59. dsl = JsonConvert.SerializeObject(data.dsl);
  60. PostData fromString = dsl;
  61. var result = _LowLevelclient.Search<StringResponse>("mem_memberbase_p",dsl);
  62. var DynamicObject = JsonConvert.DeserializeObject<dynamic>(result.Body);
  63. return result.Body;
  64. }
  65. [HttpGet]
  66. [Route("test")]
  67. public IReadOnlyCollection<mem_MemberBase> Test()
  68. {
  69. int pageSize = 10;
  70. int pageIndex = 1;
  71. var result = _esclient.Search<mem_MemberBase>(s => s.Index("mem_memberbase_p").Query(
  72. //q => q.MatchAll()
  73. q => q
  74. .Bool(r => r.Filter(m => m.Term(t => t.Name("MemUsualPhone").Field("MemUsualPhone").Value("CEDB61EEE275213EB4881CC502660EFD")))
  75. .Must(r1 => r1.Nested(
  76. n => n.Query(q1 => q1.MatchAll()).Path("Terminals").ScoreMode(NestedScoreMode.None).InnerHits(
  77. h => h.From(0).Size(1).Source(k => k.Includes( k1=>k1.Field("Terminals.*")))
  78. //.DocValueFields(k1 => k1.Field(k2 => k2.Terminals))
  79. )
  80. )
  81. )
  82. )
  83. )
  84. .Source(k => k.Includes(k1 => k1.Field("Terminals.*").Field("MemUsualPhone")))
  85. //.StoredFields("_none_")
  86. .Size(pageSize)
  87. .From((pageIndex - 1) * pageSize)
  88. //.Sort(st => st.Descending(d => d.RegisterTime))
  89. ).Documents;
  90. return result;
  91. }
  92. [HttpPost]
  93. [Route("sqllow")]
  94. public QuerySqlResponse SqlLow([FromBody]dynamic data)
  95. {
  96. var json = JsonConvert.SerializeObject(data);
  97. //var post = new MyPostData();
  98. //post.Write(new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(data)), _LowLevelclient.Settings);
  99. var result = _LowLevelclient.Sql.Query<QuerySqlResponse>(PostData.String(json));
  100. return result;
  101. }
  102. [HttpPost]
  103. [Route("sql")]
  104. public ActionResult Sql([FromBody]dynamic data)
  105. {
  106. var json = JsonConvert.SerializeObject(data);
  107. var str = Util.ElasticQuery("_sql?format=json", "POST", json);
  108. var result = new JSONNetResult((JObject)JsonConvert.DeserializeObject(str));
  109. return result;
  110. }
  111. [HttpPost]
  112. [Route("sqlQuery")]
  113. public ActionResult SqlQuery(string query)
  114. {
  115. var json = "{\"query\":\"" + query +
  116. "\"}";
  117. var str = Util.ElasticQuery("_sql?format=json", "POST", json);
  118. var result = new JSONNetResult((JObject)JsonConvert.DeserializeObject(str));
  119. return result;
  120. }
  121. [HttpPost]
  122. [Route("productClass")]
  123. public List<string[]> productClass()
  124. {
  125. var json = "{\"query\":\"" + "select SUBSTRING(ClassCode,0,2),count(1) from product_parent group by SUBSTRING(ClassCode,0,2)" +
  126. "\"}";
  127. var str = Util.ElasticQuery("_sql?format=json", "POST", json);
  128. var jo = (JObject)JsonConvert.DeserializeObject(str);
  129. var resultCate = Util.SqlQuery("select * from productcategory ");
  130. Dictionary<string, string> map = new Dictionary<string, string>();
  131. var listRow = new List<string[]>();
  132. foreach (var cate in resultCate.GetValue("rows"))
  133. {
  134. if (map.ContainsKey(cate[2].ToString()) == false)
  135. {
  136. map.Add(cate[2].ToString(), cate[3].ToString());
  137. }
  138. }
  139. foreach (var prod in jo.GetValue("rows"))
  140. {
  141. var row = new string[3];
  142. if (map.ContainsKey(prod[0].ToString()))
  143. {
  144. row[2] = prod[0].ToString();
  145. row[1] = prod[1].ToString();
  146. row[0] = map[prod[0].ToString()];
  147. listRow.Add(row);
  148. }
  149. }
  150. //var result = new JSONNetResult(jo);
  151. return listRow;
  152. }
  153. [HttpPost]
  154. [Route("memberProvince")]
  155. public List<string[]> memberProvince()
  156. {
  157. var json = "{\"query\":\"" + "select MemUsualProvinceCode,count(distinct MemUsualPhone) from mem_memberbase group by MemUsualProvinceCode " +
  158. "\"}";
  159. var str = Util.ElasticQuery("_sql?format=json", "POST", json);
  160. var jo = (JObject)JsonConvert.DeserializeObject(str);
  161. var resultCate = Util.SqlQuery("select* from areamap where Level = 1 ");
  162. Dictionary<string, string> map = new Dictionary<string, string>();
  163. var listRow = new List<string[]>();
  164. foreach (var cate in resultCate.GetValue("rows"))
  165. {
  166. if (map.ContainsKey(cate[2].ToString()) == false)
  167. {
  168. map.Add(cate[2].ToString(), cate[0].ToString());
  169. }
  170. }
  171. foreach (var prod in jo.GetValue("rows"))
  172. {
  173. var row = new string[3];
  174. if (map.ContainsKey(prod[0].ToString()))
  175. {
  176. row[2] = prod[0].ToString();
  177. row[1] = prod[1].ToString();
  178. row[0] = map[prod[0].ToString()];
  179. listRow.Add(row);
  180. }
  181. }
  182. //var result = new JSONNetResult(jo);
  183. return listRow;
  184. }
  185. [HttpPost]
  186. [Route("memberRegistYM")]
  187. public ActionResult memberRegistYM()
  188. {
  189. var json = "{\"query\":\"" + "select CONCAT(CONVERT(year(RegisterTime),text),CONVERT(month(RegisterTime),text)) as YearMonth,count(1) " +
  190. " from mem_memberbase group by CONCAT(CONVERT(year(RegisterTime),text),CONVERT(month(RegisterTime),text)) " +
  191. "\"}";
  192. var str = Util.ElasticQuery("_sql?format=json", "POST", json);
  193. var result = new JSONNetResult((JObject)JsonConvert.DeserializeObject(str));
  194. return result;
  195. }
  196. [HttpPost]
  197. [Route("memberRegistY")]
  198. public ActionResult memberRegistY()
  199. {
  200. var json = "{\"query\":\"" + "select year(RegisterTime) as YearMonth,count(1) from mem_memberbase group by year(RegisterTime)" +
  201. "\"}";
  202. var str = Util.ElasticQuery("_sql?format=json", "POST", json);
  203. var result = new JSONNetResult((JObject)JsonConvert.DeserializeObject(str));
  204. return result;
  205. }
  206. }
  207. }