Net使用ElasticSearch入门
程序员文章站
2022-07-05 08:04:06
...
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相关查询 就行 补充
上一篇: 《OS:PV操作 - 生产者消费者问题》
推荐阅读
-
asp.net core 使用 AccessControlHelper 控制访问权限
-
SVN入门及配置使用_PHP教程
-
Asp.net Webform 使用Repository模式实现CRUD操作代码生成工具
-
PHP使用SOAP调用.net的WebService数据_PHP
-
比较全的PHP 会话(session 时间设定)使用入门代码
-
idea创建一个入门Spring Boot项目(controller层)使用Moven代码管理
-
安全高效跨平台的. NET 模板引擎 Fluid 使用文档
-
ASP.NET MVC中使用jQuery时的浏览器缓存问题
-
使用Python开发windows GUI程序入门实例
-
我们可以使用Phalanger创建组合.NET和PHP的解决方案