using Nest; using System; using System.Collections.Generic; using System.Configuration; using System.Linq.Expressions; using System.Threading.Tasks; namespace Demo { public class ESHelp { private static ElasticClient client; static ESHelp() { string esConnectionString = ConfigurationManager.AppSettings["ElasticsearchConnectionString"].ToString(); ConnectionSettings settings = new ConnectionSettings(new Uri(esConnectionString)).DefaultIndex("eventinfo"); client = new ElasticClient(settings); } /// <summary> /// 写入接口 异步 /// </summary> /// <param name="data">要写入的文档</param> /// <param name="index">写入的索引</param> /// <param name="type">写入的type</param> /// <returns></returns> public static async Task<bool> InsertAsync(object data, string index, string type) { var response = await client.IndexAsync(data, m => m.Index(index).Type(type)); return true; } /// <summary> /// 写入接口 同步写入 /// </summary> /// <param name="data">要写入的文档</param> /// <param name="index">写入的索引</param> /// <param name="type">写入的type</param> public static void Insert(object data, string index, string type) { var response = client.Index(data, m => m.Index(index.ToLower()).Type(type)); if (!response.Created) { throw response.OriginalException; } } /// <summary> /// 文档更新(部分更新) /// </summary> /// <param name="data">需要更新的部分</param> /// <param name="index">es的索引</param> /// <param name="type">es的Type</param> /// <param name="id">文档的id 非实体id</param> /// <returns></returns> public static bool Update(object data, string index, string type, string id) { var result = client.Update<object, object>(id, m => m.Index(index).Type(type).Doc(data)); //写入失败,返回错误信息 if (!result.IsValid) { throw result.OriginalException; } return result.IsValid; } public static async Task<bool> UpdateAsync(object data, string index, string type, string id) { var result = await client.UpdateAsync<object, object>(id, m => m.Index(index).Type(type).Doc(data)); //写入失败,返回错误信息 if (!result.IsValid) { throw result.OriginalException; } return result.IsValid; } /// <summary> /// ES查询 /// </summary> /// <typeparam name="T">Entity</typeparam> /// <param name="func"></param> /// <param name="index">索引</param> /// <param name="type">类型</param> /// <param name="page">第几页</param> /// <param name="rows">取多少行</param> /// <returns></returns> public static ISearchResponse<T> Seacrh<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, string index, string type, int page, int rows) where T : class { var result = client.Search<T>(s => s.Index(index).Type(type).Query(q => q.Bool( m => m.Must(func) )) .From((page - 1) * rows) .Size(rows)); return result; } public static ISearchResponse<object> SearchRequery() { //HttpClient http = new HttpClient(); //http.BaseAddress =new Uri(ConfigurationManager.AppSettings["ElasticsearchConnectionString"].ToString()); //var result = http.PostAsync(http.BaseAddress,""); return null; } public static ISearchResponse<T> SeacrhEventInfo<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, Expression<Func<T, DateTime?>> sort, SearchEventDto dto, int from) where T : class { var result = client.Search<T>(s => s.Index(dto.Index).Type(dto.Type).Query(q => q.Bool( m => m.Must(func) )) .Sort(st => st.Ascending(sort)) .From(from) .Size(1).Source(d => d.Excludes(f => f.Field("*$id")))); return result; } public static ISearchResponse<T> SeacrhEventInfoQuerys<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, Expression<Func<T, DateTime?>> sort, SearchEventDto dto) where T : class { var result = client.Search<T>(s => s.Index(dto.Index).Type(dto.Type).Query(q => q.Bool( m => m.Must(func) )) .Sort(st => st.Ascending(sort)) .From(dto.From) .Size(dto.Size).Source(d=>d.Excludes(f=>f.Field("*$id")))); return result; } public static ISearchResponse<T> SearchInfo<T>(IEnumerable<Func<QueryContainerDescriptor<T>, QueryContainer>> func, string index, string type, int page, int rows, Func<SortDescriptor<T>,IPromise<IList<ISort>>> sort) where T : class { var result = client.Search<T>(s => s.Index(index).Type(type).Query(q => q.Bool( m => m.Must(func) )) .Sort(sort) .From((page - 1) * rows) .Size(rows)); return result; } } }