C#配合大数据开发,nest.dll的使用
一,添加Nest.dll引用
结果如下多了如下DLL:
Elasticsearch.Net.dll,Nest.dll
二,上代码:
using Common.EsModel; using Elasticsearch.Net; using Nest; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Common.Es { /// <summary> /// Nest(ES)开发: /// 1,需要大数据提供节点地址,和账号密码 /// 2,需要大数据提供表名和字段名 /// 3,需要大数据提供一个可以查询数据的地址(这个地址是大数据搭建的)(需要大数据那边提供账号密码登陆),可以测试查询返回值,和测试查询语句是否正确 /// </summary> public class EasticSearchUtil { /// <summary> /// ES查询上限 /// </summary> public const int limitcount = 10000; public static string ElasticSearchAccount = "demo"; public static string ElasticSearchPassword = "123456"; /// <summary> /// 配置链接: /// </summary> /// <param name="data">查询的表名</param> /// <returns></returns> private ElasticClient GetClient(string data) { ////一个节点 var nodes = new Uri[] { ///节点地址 new Uri("http://192.168.0.1:1001"), new Uri("http://192.168.0.2:1001"), new Uri("http://192.168.0.3:1001"), }; var pool = new StaticConnectionPool(nodes); var settings = new ConnectionSettings(pool).DefaultIndex(data).DefaultFieldNameInferrer((name) => name); settings.BasicAuthentication(ElasticSearchAccount, ElasticSearchPassword); return new ElasticClient(settings); } /// <summary> /// 查询 /// Term这个是查询映射实体不存在的字段(PS) /// </summary> /// <param name="province">省份名称</param> /// <param name="id">id</param> public IEnumerable<demo> search(string province, int id) { var searchResults = GetClient("demo").Search<demo>((s => s.From(0).Size(limitcount).Query(q => q.Term("Id", id) && q.Term("province", province)).Sort(st => st.Descending(ds => ds.Num)))); return searchResults.Documents; } /// <summary> /// 新增 /// </summary> /// <returns></returns> public bool create() { var model = new demo { Id = 4, Num = 1, city = "123", }; var searchResults = GetClient("demo").Create<demo>(model, null); return searchResults.IsValid; } /// <summary> /// 删除 /// </summary> /// <returns></returns> public bool del() { var client = GetClient("demo"); var model = new demo { Id = 2, Num = 111 }; //删除文档 var response2 = client.Delete<demo>(model.Id); return response2.IsValid; } /// <summary> /// 更新 /// </summary> /// <returns></returns> public bool update() { var model = new demo { Id = 2, Num = 111 }; //根据demoID来修改文档,如果存储里存在一个CompanyID等于2的文档,他就会用新的替换旧的,否则就创建一个。 var response = GetClient("demo").Index(model, i => i.Id(model.Id)); return response.IsValid; } } }