C#配合大数据开发,nest.dll的使用

一,添加Nest.dll引用

C#配合大数据开发,nest.dll的使用_第1张图片

结果如下多了如下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
{
    /// 
    /// Nest(ES)开发:
    /// 1,需要大数据提供节点地址,和账号密码
    /// 2,需要大数据提供表名和字段名
    /// 3,需要大数据提供一个可以查询数据的地址(这个地址是大数据搭建的)(需要大数据那边提供账号密码登陆),可以测试查询返回值,和测试查询语句是否正确
    /// 
    public class EasticSearchUtil
    {
        /// 
        /// ES查询上限
        /// 
        public const int limitcount = 10000;
        public static string ElasticSearchAccount = "demo";
        public static string ElasticSearchPassword = "123456";
        /// 
        /// 配置链接:
        /// 
        /// 查询的表名
        /// 
        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);
        }
        /// 
        /// 查询 
        /// Term这个是查询映射实体不存在的字段(PS)
        /// 
        /// 省份名称
        /// id
        public IEnumerable search(string province, int id)
        {
            var searchResults = GetClient("demo").Search((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;
        }
        /// 
        /// 新增
        /// 
        /// 
        public bool create()
        {
            var model = new demo
            {
                Id = 4,
                Num = 1,
                city = "123",
            };

            var searchResults = GetClient("demo").Create(model, null);
            return searchResults.IsValid;
        }
        /// 
        /// 删除
        /// 
        /// 
        public bool del()
        {
            var client = GetClient("demo");
            var model = new demo { Id = 2, Num = 111 };
            //删除文档
            var response2 = client.Delete(model.Id);
            return response2.IsValid;
        }
        /// 
        /// 更新
        /// 
        /// 
        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;
        }
    }
}

 

转载于:https://www.cnblogs.com/May-day/p/11338545.html

你可能感兴趣的:(大数据)