IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 大数据 -> Net使用ElasticSearch入门 -> 正文阅读

[大数据]Net使用ElasticSearch入门

Net使用ElasticSearch入门

1    版本说明

Net5的版本
ElasticSearch 7.10.2的版本
Kibana 7.10.2 (主要用于ElasticSearch数据可视化)
OS: Windos10

2   ElasticSearch     简介 (引用百度百科)

Elasticsearch(以下简称es)是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。es是用Java语言开发的,并作为Apache许可条款下的开放源码发布,是一种流行的企业级搜索引擎。es用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,es是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene

3   下面以一张图 来简单对比下他们

 ![在这里插入图片描述](https://img-blog.csdnimg.cn/96b1b60ec7a847029fcb15ae09046f6f.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1Rlc3REYXRhcw==,size_16,color_FFFFFF,t_70#pic_center)

.

使用es之前 我们先安装 JDK 我们建立一个控制台项目 然后导入Elasticsearch.Net.dll 和Nest.dll 这两个dll 下面开始写代码了

**创建连接**
try
			{
				Uri uri = new Uri("http://localhost:9200/");
				ConnectionSettings connectionSettings = new ConnectionSettings(uri);
				connectionSettings.DefaultIndex(indexName);
				
				elasticClient = new ElasticClient(connectionSettings);
			}
			catch (Exception)
			{

				throw;
			}

创建索引

try
			{
				var createByResponceInfo = elasticClient.Indices.Create(indexName);
				if (createByResponceInfo.ApiCall.HttpStatusCode.Value == 200 && !string.IsNullOrWhiteSpace(createByResponceInfo.Index))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch (Exception)
			{

				throw;
			}

插入数据

try
			{
				var createResponce = elasticClient.CreateDocument<T>(t);
				if (createResponce.ApiCall.Success == true && createResponce.Result.ToString() == "Created") {

					return true;
				}
				return false;
			}
			catch (Exception)
			{

				throw;
			}

**查询数据 我们提供两种方法 **

try
			{
				DocumentPath<T> documentPath = new Nest.DocumentPath<T>(id);
				GetResponse<T> result = elasticClient.Get(documentPath);
				//GetResponse<Person> result = elasticClient.Get<Person>(new GetRequest(indexName, 10));
				return result.Source as T;
			}
			catch (Exception)
			{

				throw;
			}
	List<T> result = new List<T>();
			try
			{
				var result1 = elasticClient.Search<Person>(x => x.Size(size));
				foreach (var item in result1.Documents)
				{
					 var  obj=item as T;
					 result.Add(obj);
				}
				return result;
			}
			catch (Exception)
			{

				throw;
			}
下面我把我自己简单封装的类  贴出来 
public class ESHelper<T> where T : class, new()
	{

		public ESHelper(string indexName)
		{
			Build(indexName);
		}

		public static ElasticClient elasticClient { get; set; }
		/// <summary>
		/// 创建连接
		/// </summary>
		/// <param name="indexName"></param>
		public static void Build(string indexName) {
			try
			{
				Uri uri = new Uri("http://localhost:9200/");
				ConnectionSettings connectionSettings = new ConnectionSettings(uri);
				connectionSettings.DefaultIndex(indexName);
				
				elasticClient = new ElasticClient(connectionSettings);
			}
			catch (Exception)
			{

				throw;
			}
		}
		#region 索引操作
		/// <summary>
		/// 删除索引
		/// </summary>
		/// <param name="indexName"></param>
		/// <returns></returns>
		public  bool DeleteIndex(string indexName)
		{
			try
			{
				var deleteIndexResponse = elasticClient.Indices.Delete(Indices.Parse(indexName));
				if (deleteIndexResponse.ApiCall.Success == true)
				{
					return true;
				}
				return false;
			}
			catch (Exception)
			{

				throw;
			}
		}


		/// <summary>
		/// 判断索引是否存在
		/// </summary>
		/// <param name="indexName"></param>
		public  bool ExistIndex(string indexName)
		{
			try
			{
				return elasticClient.Indices.Exists(indexName).Exists;
			}
			catch (Exception)
			{

				throw;
			}

		}


		/// <summary>
		/// 创建索引
		/// </summary>
		/// <returns></returns>
		public  bool CreateIndex(string indexName)
		{
			try
			{
				var createByResponceInfo = elasticClient.Indices.Create(indexName);
				if (createByResponceInfo.ApiCall.HttpStatusCode.Value == 200 && !string.IsNullOrWhiteSpace(createByResponceInfo.Index))
				{
					return true;
				}
				else
				{
					return false;
				}
			}
			catch (Exception)
			{

				throw;
			}
		}
		#endregion

		#region 文档操作

		/// <summary>
		/// 是否存在文档
		/// </summary>
		/// <returns></returns>
		public  bool ExisitDocument() {
			try
			{
				T t = new T();
				Id id = Id.From<T>(t);
				return elasticClient.DocumentExists<T>(id).Exists;
			}
			catch (Exception)
			{

				throw;
			}

		}

		/// <summary>
		/// 创建文档  添加数据
		/// </summary>
		/// <returns></returns>
		public  bool CreateDocument(T t) {
			try
			{
				var createResponce = elasticClient.CreateDocument<T>(t);
				if (createResponce.ApiCall.Success == true && createResponce.Result.ToString() == "Created") {

					return true;
				}
				return false;
			}
			catch (Exception)
			{

				throw;
			}

		}

		/// <summary>
		/// 批量创建文档 批量添加数据
		/// </summary>
		/// <returns></returns>
		public bool BatchInsertDocument(List<T> lists) {
			try
			{
				
				var createResponce = elasticClient.CreateDocument<List<T>>(lists);
				if (createResponce.ApiCall.Success == true && createResponce.Result.ToString() == "Created")
				{

					return true;
				}
				return false;
			}
			catch (Exception)
			{

				throw;
			}
		}

		/// <summary>
		/// 删除文档
		/// </summary>
		/// <returns></returns>
		public  bool DeleteDocument(int index) {
			try
			{
			  return elasticClient.DeleteByQuery<T>(x => x.From(index)).ApiCall.Success==true? true:false;
			}
			catch (Exception)
			{

				throw;
			}
		}

		#endregion

		#region 查询操作
		//获取单个数据
		public  T GetSingleData(int id)
		{
			try
			{
				DocumentPath<T> documentPath = new Nest.DocumentPath<T>(id);
				GetResponse<T> result = elasticClient.Get(documentPath);
				//GetResponse<Person> result = elasticClient.Get<Person>(new GetRequest(indexName, 10));
				return result.Source as T;
			}
			catch (Exception)
			{

				throw;
			}
			
		}

		/// <summary>
		/// 分页获取
		/// </summary>
		/// <param name="size"></param>
		/// <returns></returns>
		public  List<T> GetDataBySize(int size = 10) {
			List<T> result = new List<T>();
			try
			{
				var result1 = elasticClient.Search<Person>(x => x.Size(size));
				foreach (var item in result1.Documents)
				{
					 var  obj=item as T;
					 result.Add(obj);
				}
				return result;
			}
			catch (Exception)
			{

				throw;
			}
		
		}
		#endregion
	}

因为这是我自己摸索 写的 很多东西 还不太懂 因为 es最重要的就是查询 后续我会把 es相关查询 就行 补充

  大数据 最新文章
实现Kafka至少消费一次
亚马逊云科技:还在苦于ETL?Zero ETL的时代
初探MapReduce
【SpringBoot框架篇】32.基于注解+redis实现
Elasticsearch:如何减少 Elasticsearch 集
Go redis操作
Redis面试题
专题五 Redis高并发场景
基于GBase8s和Calcite的多数据源查询
Redis——底层数据结构原理
上一篇文章      下一篇文章      查看所有文章
加:2021-07-25 16:15:03  更:2021-07-25 16:15:20 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/25 12:18:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码