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.