.Net Core3.1 MVC + EF Core+ AutoFac+LayUI+Sqlserver的框架搭建--------ElasticSearch
阿新 • • 發佈:2021-11-13
ES資料庫配置和搭建以前也已經寫過了,感興趣的可以看我以前寫的。現在我就是直接把它整合到我的架構中了。就是在以前的基礎上又做了封裝。
ES資料庫索引的建立和資料的插入類:
using Nest; using System; using System.Collections.Generic; using System.Text; namespace Core.Net.Common.Core.Net.Core.ES { /// <summary> /// ES資料庫連線和建立索引,插入資料 /// </summary> /// <typeparam name="T"></typeparam>public class ESHelper<T> where T:class { public static readonly string url = "http://ip:port/"; /// <summary> /// 批量插入 /// </summary> /// <param name="obj">出入的資料</param> /// <param name="index">索引</param> public staticvoid ManyInsert(List<T> obj, string index) { //設定連線字串,DefaultIndex中的表名要小寫 var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(index); var client = new ElasticClient(settings); try { var ndexResponse = client.IndexMany<T>(obj); Core.FileHelper.AddLog("插入ES資料庫成功" + ndexResponse.ToString(), "Log"); } catch (Exception e) { Core.FileHelper.AddLog("插入ES資料庫異常" + e.ToString(), "Log"); } } /// <summary> /// 單行插入 /// </summary> /// <param name="t">插入的資料</param> /// <param name="index">索引</param> public static void insert(object t, string index) { //設定連線字串,DefaultIndex中的表名要小寫 var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(index); var client = new ElasticClient(settings); // client.IndexMany<> var doc = t; //通過 IndexDocument() 方法插入資料 var ndexResponse = client.IndexDocument(doc); } /// <summary> /// 連線ES /// </summary> /// <param name="index">索引</param> /// <returns></returns> public static ElasticClient Connect_Index(string index) { //設定連線字串,DefaultIndex中的表名要小寫 var settings = new ConnectionSettings(new Uri(url)).DefaultIndex(index); var client = new ElasticClient(settings); return client; } } }
ES資料庫查詢類封裝:
using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Text; namespace Core.Net.Common.Core.Net.Core.ES { /// <summary> /// ES資料庫查詢通用類 /// </summary> /// <typeparam name="T"></typeparam> public class ESSearchHelper<T> where T:class,new() { /// <summary> /// 帶有一個查詢條件的查詢 /// </summary> /// <param name="index">ES資料庫索引</param> /// <param name="onewhere">查詢條件表示式</param> /// <param name="onefiled">查詢條件欄位</param> /// <param name="orderby">排序</param> /// <param name="startPage">開始頁</param> /// <param name="endPage">結束頁</param> /// <returns></returns> public static List<T> GetOneESList(string index, Expression<Func<T, bool>> onewhere,string onefiled, Expression<Func<T, bool>> orderby,int startPage,int endPage) { var client = ESHelper<T>.Connect_Index(index); var result = client.Search<T> ( x => x.Index(index) .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled))) .Sort(q=>q.Descending(orderby)) .From(startPage) .Size(endPage) ).Documents.ToJson(); return result.ToList<T>(); } /// <summary> /// 帶有兩個查詢條件的查詢 /// </summary> /// <param name="index">ES資料庫索引</param> /// <param name="onewhere">第一個查詢條件表示式</param> /// <param name="onefiled">第一個查詢條件欄位</param> /// <param name="twowhere">第二個查詢條件表示式</param> /// <param name="twofiled">第二個查詢條件欄位</param> /// <param name="orderby">排序</param> /// <param name="startPage">開始頁數</param> /// <param name="endPage">結束頁數</param> /// <returns></returns> public static List<T> GetTwoESList(string index, Expression<Func<T, bool>> onewhere, string onefiled, Expression<Func<T, bool>> twowhere,string twofiled, Expression<Func<T, bool>> orderby, int startPage, int endPage) { var client = ESHelper<T>.Connect_Index(index); var result = client.Search<T> ( x => x.Index(index) .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled))) .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled))) .Sort(q => q.Descending(orderby)) .From(startPage) .Size(endPage) ).Documents.ToJson(); return result.ToList<T>(); } /// <summary> /// 三個查詢條件查詢ES資料庫 /// </summary> /// <param name="index">ES資料庫索引</param> /// <param name="onewhere">第一個查詢條件表示式</param> /// <param name="onefiled">第一個查詢條件欄位</param> /// <param name="twowhere">第二個查詢條件表示式</param> /// <param name="twofiled">第二個查詢條件欄位</param> /// <param name="threewhere">第三個查詢條件表示式</param> /// <param name="threefiled">第三個查詢條件欄位</param> /// <param name="orderby">排序</param> /// <param name="startPage">開始頁數</param> /// <param name="endPage">結束頁數</param> /// <returns></returns> public static List<T> GetThreeESList(string index, Expression<Func<T, bool>> onewhere, string onefiled, Expression<Func<T, bool>> twowhere, string twofiled, Expression<Func<T, bool>> threewhere, string threefiled, Expression<Func<T, bool>> orderby, int startPage, int endPage) { var client = ESHelper<T>.Connect_Index(index); var result = client.Search<T> ( x => x.Index(index) .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled))) .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled))) .Query(q => q.Match(m => m.Field(threefiled).Query(threefiled))) .Sort(q => q.Descending(orderby)) .From(startPage) .Size(endPage) ).Documents.ToJson(); return result.ToList<T>(); } /// <summary> /// 除時間外含有一個查詢條件 /// </summary> /// <param name="index">ES資料庫索引</param> /// <param name="onewhere">查詢條件表示式</param> /// <param name="onefiled">查詢條件欄位</param> /// <param name="timewhere">時間欄位表示式</param> /// <param name="startTime">開始時間</param> /// <param name="endTime">結束時間</param> /// <param name="orderby">排序</param> /// <param name="startPage">開始頁</param> /// <param name="endPage">結束頁</param> /// <returns></returns> public static List<T> GetOneTimeESList(string index, Expression<Func<T, bool>> onewhere, string onefiled, Expression<Func<T, bool>> timewhere,DateTime startTime,DateTime endTime, Expression<Func<T, bool>> orderby, int startPage, int endPage) { var client = ESHelper<T>.Connect_Index(index); var result = client.Search<T> ( x => x.Index(index) .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled))) //注意:這個按時間段查詢我試了好多此,好像只要新增這個時間段,就查不出來任何資料,非常奇怪,我百度了一下,好像都是這樣乾的。 .Query(q=>q.DateRange(c=>c.Field(timewhere).LessThanOrEquals(startTime).GreaterThanOrEquals(endTime))) .Sort(q => q.Descending(orderby)) .From(startPage) .Size(endPage) ).Documents.ToJson(); return result.ToList<T>(); } /// <summary> /// 除時間外含有一個查詢條件 /// </summary> /// <param name="index">ES資料庫索引</param> /// <param name="onewhere">第一個查詢條件表示式</param> /// <param name="onefiled">第一個查詢條件欄位</param> /// <param name="twowhere">第二個查詢條件表示式</param> /// <param name="twofiled">第二個查詢條件欄位</param> /// <param name="timewhere">時間欄位表示式</param> /// <param name="startTime">開始時間</param> /// <param name="endTime">結束時間</param> /// <param name="orderby">排序</param> /// <param name="startPage">開始頁</param> /// <param name="endPage">結束頁</param> /// <returns></returns> public static List<T> GetTwoTimeESList(string index, Expression<Func<T, bool>> onewhere, string onefiled, Expression<Func<T, bool>> twowhere, string twofiled, Expression<Func<T, bool>> timewhere, DateTime startTime, DateTime endTime, Expression<Func<T, bool>> orderby, int startPage, int endPage) { var client = ESHelper<T>.Connect_Index(index); var result = client.Search<T> ( x => x.Index(index) .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled))) .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled))) .Query(q => q.DateRange(c => c.Field(timewhere).LessThanOrEquals(startTime).GreaterThanOrEquals(endTime))) .Sort(q => q.Descending(orderby)) .From(startPage) .Size(endPage) ).Documents.ToJson(); return result.ToList<T>(); } /// <summary> /// 除時間外含有一個查詢條件 /// </summary> /// <param name="index">ES資料庫索引</param> /// <param name="onewhere">第一個查詢條件表示式</param> /// <param name="onefiled">第一個查詢條件欄位</param> /// <param name="twowhere">第二個查詢條件表示式</param> /// <param name="twofiled">第二個查詢條件欄位</param> /// <param name="threewhere">第三個查詢條件表示式</param> /// <param name="threefiled">第三個查詢條件欄位</param> /// <param name="timewhere">時間欄位表示式</param> /// <param name="startTime">開始時間</param> /// <param name="endTime">結束時間</param> /// <param name="orderby">排序</param> /// <param name="startPage">開始頁</param> /// <param name="endPage">結束頁</param> /// <returns></returns> public static List<T> GetThreeTimeESList(string index, Expression<Func<T, bool>> onewhere, string onefiled, Expression<Func<T, bool>> twowhere, string twofiled, Expression<Func<T, bool>> timewhere, DateTime startTime, DateTime endTime, Expression<Func<T, bool>> threewhere, string threefiled, Expression<Func<T, bool>> orderby, int startPage, int endPage) { var client = ESHelper<T>.Connect_Index(index); var result = client.Search<T> ( x => x.Index(index) .Query(q => q.Match(m => m.Field(onewhere).Query(onefiled))) .Query(q => q.Match(m => m.Field(twowhere).Query(twofiled))) .Query(q => q.Match(m => m.Field(threefiled).Query(threefiled))) .Query(q => q.DateRange(c => c.Field(timewhere).LessThanOrEquals(startTime).GreaterThanOrEquals(endTime))) .Sort(q => q.Descending(orderby)) .From(startPage) .Size(endPage) ).Documents.ToJson(); return result.ToList<T>(); } } }.Net Core