标签:版本号 static 运行环境 one isa rds ring tengine tcl
/******************************************************* * * 作者:朱皖苏 * 创建日期:20180508 * 说明:此文件只包含一个类,具体内容见类型注释。 * 运行环境:.NET 4.0 * 版本号:1.0.0 * * 历史记录: * 创建文件 朱皖苏 20180508 14:00 * *******************************************************/ using DBEN.Basic.ElasticSearch; using Elasticsearch.Net; using Nest; using System; using System.Collections.Generic; using System.Linq; namespace DBEN.ITEngine.Storages { /// <summary> /// ES 服务器 索引、类型仓库基类 /// </summary> /// <typeparam name="T"></typeparam> public abstract class BaseESStorage<T> where T : class, IESEntity { protected ElasticClient client; protected string index = string.Empty; protected string type = nameof(T).ToLower(); /// <summary> /// 默认的初始化 client /// </summary> /// <param name="nodes">服务器集群</param> /// <param name="index">索引名</param> protected virtual void InitClient(IEnumerable<Uri> nodes, string index) { var pool = new StaticConnectionPool(nodes); var settings = new ConnectionSettings(pool); settings.DefaultIndex(index); settings.DisableDirectStreaming(true); client = new ElasticClient(settings.ThrowExceptions()); var exits = client.IndexExists(index); if (!exits.Exists) { CreateIndex(index + "_real", index); } this.index = index; } /// <summary> /// 当索引不存在的时候创建索引。 /// 根据 <see cref="Nest.ElasticsearchTypeAttribute"/> 自动映射类型 /// 默认创建 NumberOfShards = 5 /// 默认创建 NumberOfReplicas = 1, /// </summary> /// <param name="index"></param> protected virtual void CreateIndex(string index, string Alias) { var descriptor = new CreateIndexDescriptor(index) .Settings(s => s.NumberOfShards(5).NumberOfReplicas(1)) .Aliases(m => m.Alias(Alias)) .Mappings(ms => ms.Map<T>(m => m .Properties(ps => ps .Text(t => t.Name(n => n.Id)) .Text(t => t.Name(n => n.CreateTime).Index(true))) .AutoMap())); client.CreateIndex(descriptor); } /// <summary> /// 新增单条信息 /// </summary> /// <param name="entity">数据实体</param> public bool InsertOne(T t) { if (!string.IsNullOrEmpty(t.Id)) { return false; } var rsp = client.Index(t, o => o.Index(index).Id(t.Id)); t.Id = rsp.Id; return rsp.ApiCall.Success; } /// <summary> /// 新增多条信息 /// </summary> /// <param name="entity">数据实体</param> public void InsertMany(IEnumerable<T> t) { var rsp = client.IndexMany(t, index); } /// <summary> /// 更新单条信息 /// </summary> /// <param name="t"></param> /// <param name="id"></param> public void UpdateOne(T t, Id id) { var rsp = client.Index<T>(t, o => o.Index(index).Id(id)); } /// <summary> /// (根据名称精确查找) /// </summary> /// <param name="t"></param> /// <param name="field"></param> /// <param name="value"></param> /// <returns></returns> public ISearchResponse<T> Find(string field, string value) { var searchResult = client.Search<T>(s => s .Index(index) .Query(q => q .Term(e => e.Suffix(field), value))); return searchResult; } /// <summary> /// 根据名称精确查找一个 /// </summary> /// <param name="field"></param> /// <param name="value"></param> /// <param name="compare"></param> /// <returns></returns> public T FindFirst(string field, string value, Func<T, bool> compare = null) { var searchResult = client.Search<T>(s => s .Index(index) .Query(q => q .Term(e => e.Suffix(field), value))); return searchResult.Hits.Select(m => m.Source) .FirstOrDefault(m => compare == null ? true : compare.Invoke(m)); } /// <summary> /// 模糊查询 /// </summary> /// <param name="field"></param> /// <param name="value"></param> /// <returns></returns> public ISearchResponse<T> FuzzyFind(string field, string value) { //模糊搜索 var searchResult = client.Search<T>(s => s .Index(index) .From(0) .Size(10) .Query(q => q .MatchPhrase(d => d.Field(field).Query(value).Slop(100)))); return searchResult; } /// <summary> /// term any item of <paramref name="values"/> /// </summary> /// <typeparam name="T"></typeparam> /// <param name="descriptor"></param> /// <param name="field"></param> /// <param name="values"></param> /// <returns></returns> protected QueryContainer TermAny(QueryContainerDescriptor<T> descriptor, Field field, IEnumerable<string> values) { QueryContainer q = new QueryContainer(); foreach (var value in values) { q |= descriptor.Term(t => t.Field(field).Value(value)); } return q; } public long Count() { return client.Count<T>(s => s.Index(index)).Count; } } }
ENtity
using Nest; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DBEN.Basic.ElasticSearch { public interface IESEntity { string Id { get; set; } DateTime CreateTime { get; set; } } }
ES 服务器 索引、类型仓库基类 BaseESStorage
标签:版本号 static 运行环境 one isa rds ring tengine tcl
原文地址:https://www.cnblogs.com/zhuwansu/p/10836898.html