.net core中简单使用NEST发布ElasticSearch api
作者使用的NEST版本7.11.1官方文档https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.x/nest-getting-started.html测试API类代码using Nest;using System;using System.Collections.Generic;namespace TestAPI.Models
·
作者使用的NEST版本7.11.1
官方文档https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.x/nest-getting-started.html
测试API类代码
using Nest;
using System;
using System.Collections.Generic;
namespace TestAPI.Models
{
public class StudentAPI
{
private ElasticClient _client;
public static string ConnectionSettings = "http://192.168.1.156:9200";
public static string IndexName = "student";
private static int _size = 20;
public StudentAPI()
{
var settings = new ConnectionSettings(new Uri(ConnectionSettings))
.DefaultIndex(IndexName);
_client = new ElasticClient(settings);
}
/// <summary>
/// 创建索引
/// </summary>
/// <returns></returns>
public bool CreateIndex() {
try
{
dynamic response = _client.Indices.Get(IndexName);
if (!response.IsValid)
{
response = _client.Indices.Create(IndexName, c => c
.Map<Student>(m => m
.AutoMap()
));
Console.WriteLine(response);
}
return true;
}
catch (Exception e)
{
return false;
throw;
}
}
/// <summary>
/// 删除索引
/// </summary>
/// <returns></returns>
public bool DeleteIndex()
{
try
{
//根据名称删除索引
var response = _client.Indices.Delete(IndexName);
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 添加文档
/// </summary>
/// <param name="stu"></param>
/// <returns></returns>
public bool Add(Student stu)
{
try
{
var indexResponse = _client.Index(stu, i => i.Index(IndexName).Id(stu.ID));
if (!indexResponse.IsValid)
{
// If the request isn't valid, we can take action here
return false;
}
return true;
}
catch (Exception e)
{
return false;
throw;
}
}
/// <summary>
/// 批量添加文档
/// </summary>
/// <param name="lists"></param>
/// <returns></returns>
public bool BulkDescriptor(List<Student> lists)
{
try
{
//批量创建文档
BulkDescriptor descriptor = new BulkDescriptor();
for (int i = 0; i < lists.Count; i++)
{
var stu = lists[i];
descriptor.Index<Student>(op => op.Document(stu).Index(IndexName).Id(stu.ID));
}
var response = _client.Bulk(descriptor);
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 根据ID删除文档
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public bool Delete(string id)
{
try
{
//根据特定的条件来删除文档
var response = _client.Delete<Student>(id, d => d.Index(IndexName));
//删除文档
//response = _client.Delete<Student>(id.CompanyID);
return true;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 更新文档
/// </summary>
/// <param name="stu"></param>
/// <returns></returns>
public bool Updelete(Student stu)
{
try
{
var response = _client.Index(stu, i => i.Id(stu.ID));
//修改指定版本的文档
//response = _client.Index(stu, i => i.Id(stu.CompanyID).Version(1));
return true;
}
catch (Exception)
{
throw;
return false;
}
}
/// <summary>
/// 获取索引中所有文档
/// </summary>
/// <returns></returns>
public IReadOnlyCollection<Student> GetAll()
{
try
{
var str = "";
var searchResponse = _client.Search<Student>(s => s
.Index(IndexName)
.Query(q => q
.MatchAll()
)
);
return searchResponse.Documents;
}
catch (Exception e)
{
return null;
throw;
}
}
/// <summary>
/// 分页查询,keyword过滤所有字段
/// </summary>
/// <param name="keyword"></param>
/// <param name="page"></param>
/// <returns></returns>
public IReadOnlyCollection<Student> GetAllField(string keyword, int page)
{
try
{
var searchAll = _client.Search<Student>(s => s
.Index(IndexName)
.From(page)
.Size(_size)
.Query(q => q
.QueryString(qs => qs
.Query(keyword).DefaultOperator(Operator.And))
));
return searchAll.Documents;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 分页查询,根据Name获取文档
/// </summary>
/// <param name="keyword"></param>
/// <param name="page"></param>
/// <returns></returns>
public IReadOnlyCollection<Student> GetByName(string keyword,int page)
{
try
{
var searchResponse = _client.Search<Student>(s => s
.Index(IndexName)
.From(page)
.Size(_size)
.Query(q => q
.Match(m => m
.Field(
f => f.Name
)
.Query(keyword))
));
return searchResponse.Documents;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 查询所有,选择Name,ID,Age,三个字段输出数据
/// </summary>
/// <returns></returns>
public IReadOnlyCollection<Student> GetSelectFields()
{
try
{
var searchResponse = _client.Search<Student>(s => s
.Index(IndexName)
.Source(fs => fs
.Includes(fh => fh //包含以下字段
.Fields(
f => f.Name,
f => f.ID,
f => f.Age
)
)
.Excludes(e => e //排除以下字段
.Fields(
f => f.Birthday,
f => f.IsManager
)
)
)
.Query(q => q
.MatchAll()
)
);
return searchResponse.Documents;
}
catch (Exception)
{
throw;
}
}
/// <summary>
/// 分页查询,根据Name,Birthday字段
/// </summary>
/// <param name="keyword"></param>
/// <param name="sdate"></param>
/// <param name="edate"></param>
/// <param name="page"></param>
/// <returns></returns>
public IReadOnlyCollection<Student> GetByNameAndDay(string keyword, DateTime sdate, DateTime edate, int page)
{
try
{
var searchResponse = _client.Search<Student>(s => s
.Index(IndexName)
.From(page)
.Size(_size)
.Query(q => q
.Bool(b => b
.Must(mu => mu
.Match(m => m
.Field(f => f.Name)
.Query(keyword)
)
)
.Filter(fi => fi
.DateRange(r => r
.Field(f => f.Birthday)
.GreaterThanOrEquals(sdate)
.LessThan(edate)
)
)
)
)
);
另一个写法
//var searchResponse = _client.Search<Project>(s => s
// .Query(q => q
// .Match(m => m
// .Field(f => f.FirstName)
// .Query("许")
// ) && q
// .Match(m => m
// .Field(f => f.LastName)
// .Query("嵩")
// ) && +q
// .DateRange(r => r
// .Field(f => f.StartedOn)
// .GreaterThanOrEquals(new DateTime(2017, 01, 01))
// .LessThan(new DateTime(2018, 01, 01))
// )
// )
//);
return searchResponse.Documents;
}
catch (Exception)
{
throw;
}
}
}
}
linux中发布.net core api参考:
链接: https://www.jianshu.com/p/5fda6340ce22.
更多推荐
所有评论(0)