elasticsearch集成
StartUp.cs
//添加elasticsearch配置信息
services.AddElasticsearchProvider(_configuration);
//mapper 分库分表索引设置 或 数据保存到索引库
services.AddSingleton<IDbTableEs, DbTableEsProvider>();
elasticsearch.provider.Production.json
{
"esConfig": {
"enabled":true,//开启elasticsearch
"connectionStrings": [ "http://127.0.0.1:9200/"]
}
}
address类
[ElasticsearchType(IdProperty = "Id")]
public class Address
{
[Keyword]
public string Id { get; set; }
[Keyword]
public string Country { get; set; }
[Keyword]
public string City { get; set; }
[Keyword]
public string Pronvince { get; set; }
[Keyword]
public string Area { get; set; }
[Text]
public string Address1 { get; set; }
}
3、项目中注入和使用ElasticSearch
/// <summary>
/// 地址操作类
/// </summary>
public class AddressContext : BaseEsContext<Address>
{
/// <summary>
/// 索引名称
/// </summary>
public override string IndexName => "address";
public AddressContext(IEsClientProvider provider) : base(provider)
{
}
/// <summary>
/// 获取地址
/// </summary>
/// <param name="province"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public List<Address> GetAddresses(string province, int pageIndex, int pageSize)
{
var client = _EsClientProvider.GetClient(IndexName);
var musts = new List<Func<QueryContainerDescriptor<Address>, QueryContainer>>();
musts.Add(p => p.Term(m => m.Field(x=>x.Pronvince).Value(province)));
var search = new SearchDescriptor<Address>();
// search = search.Index(IndexName).Query(p => p.Bool(m => m.Must(musts))).From((pageIndex - 1) * pageSize).Take(pageSize);
search =search.Query(p => p.Bool(m => m.Must(musts))).From((pageIndex - 1) * pageSize).Take(pageSize);
var response = client.Search<Address>(search);
return response.Documents.ToList();
}
/// <summary>
/// 获取所有地址
/// </summary>
/// <returns></returns>
public List<Address> GetAllAddresses()
{
var client = _EsClientProvider.GetClient(IndexName);
var searchDescriptor = new SearchDescriptor<Address>();
// searchDescriptor = searchDescriptor.Index(IndexName).Query(p => p.MatchAll());
searchDescriptor = searchDescriptor.Query(p => p.MatchAll());
var response = client.Search<Address>(searchDescriptor);
return response.Documents.ToList();
}
/// <summary>
/// 删除指定城市的数据
/// </summary>
/// <param name="city"></param>
/// <returns></returns>
public bool DeleteByQuery(string city)
{
var client = _EsClientProvider.GetClient(IndexName);
var musts = new List<Func<QueryContainerDescriptor<Address>, QueryContainer>>();
musts.Add(p=>p.Term(m=>m.Field(f=>f.City).Value(city)));
var search = new DeleteByQueryDescriptor<Address>().Index(IndexName);
search = search.Query(p => p.Bool(m => m.Must(musts)));
var response = client.DeleteByQuery<Address>(p=>search);
return response.IsValid;
}
}
Controller类中使
[Route("api/[controller]")]
[ApiController]
public class AddressController : ControllerBase
{
private AddressContext _AddressContext;
public AddressController(AddressContext context)
{
_AddressContext = context;
}
/// <summary>
/// 新增或者修改
/// </summary>
/// <param name="address"></param>
[HttpPost("添加地址")]
public void AddAddress(List<Address> addressList)
{
if (addressList == null || addressList.Count < 1)
{
return;
}
_AddressContext.InsertMany(addressList);
}
/// <summary>
/// 删除地址
/// </summary>
/// <param name="id"></param>
[HttpPost("deleteAddress")]
public void DeleteAdress(string id)
{
_AddressContext.DeleteById(id);
}
/// <summary>
/// 获取所有与地址
/// </summary>
/// <returns></returns>
[HttpGet("getAllAddress")]
public List<Address> GetAllAddress()
{
return _AddressContext.GetAllAddresses();
}
/// <summary>
/// 获取地址总数
/// </summary>
/// <returns></returns>
[HttpGet("getAddressTotalCount")]
public long GetAddressTotalCount()
{
return _AddressContext.GetTotalCount();
}
/// <summary>
/// 分页获取(可以进一步封装查询条件)
/// </summary>
/// <param name="province"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
[HttpPost("getAddressByProvince")]
public List<Address> GetAddressByProvince(string province,int pageIndex,int pageSize)
{
return _AddressContext.GetAddresses(province,pageIndex,pageSize);
}
}
文档更新时间: 2021-09-07 08:00 作者:admin