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 下面以一张图 来简单对比下他们

.
使用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);
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; }
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 索引操作
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;
}
}
public bool ExistIndex(string indexName)
{
try
{
return elasticClient.Indices.Exists(indexName).Exists;
}
catch (Exception)
{
throw;
}
}
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 文档操作
public bool ExisitDocument() {
try
{
T t = new T();
Id id = Id.From<T>(t);
return elasticClient.DocumentExists<T>(id).Exists;
}
catch (Exception)
{
throw;
}
}
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;
}
}
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;
}
}
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);
return result.Source as T;
}
catch (Exception)
{
throw;
}
}
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相关查询 就行 补充
|