(精华)2020年8月18日 C#基础知识点 搜索引擎ElasticSearch的使用
项目需要添加Elasticsearch.Net和Nest
相关文档地址
Elasticsearch文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html
Elasticsearch.Net和Nest官方文档:https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.x/index.html
1、封装ElasticClient提供者
1)创建ElasticSearch配置类
public class EsConfig : IOptionsEsConfig>
{
public Liststring> Urls { get; set; }
public EsConfig Value => this;
}
2)创建ElasticSearch提供者接口以及类
///
/// ElasticClient 提供者接口
///
public interface IEsClientProvider
{
///
/// 获取ElasticClient
///
///
ElasticClient GetClient();
///
/// 指定index获取ElasticClient
///
///
///
ElasticClient GetClient(string indexName);
}
///
/// ElasticClient提供者
///
public class EsClientProvider : IEsClientProvider
{
private readonly IOptionsEsConfig> _EsConfig;
public EsClientProvider(IOptionsEsConfig> esConfig)
{
_EsConfig = esConfig;
}
///
/// 获取elastic client
///
///
public ElasticClient GetClient()
{
if (_EsConfig == null || _EsConfig.Value == null || _EsConfig.Value.Urls == null || _EsConfig.Value.Urls.Count 1)
{
throw new Exception("urls can not be null");
}
return GetClient(_EsConfig.Value.Urls.ToArray(), "");
}
///
/// 指定index获取ElasticClient
///
///
///
public ElasticClient GetClient(string indexName)
{
if (_EsConfig == null || _EsConfig.Value == null || _EsConfig.Value.Urls == null || _EsConfig.Value.Urls.Count 1)
{
throw new Exception("urls can not be null");
}
return GetClient(_EsConfig.Value.Urls.ToArray(), indexName);
}
///
/// 根据url获取ElasticClient
///
///
///
///
private ElasticClient GetClient(string url, string defaultIndex = "")
{
if (string.IsNullOrWhiteSpace(url))
{
throw new Exception("es 地址不可为空");
}
var uri = new Uri(url);
var connectionSetting = new ConnectionSettings(uri);
if (!string.IsNullOrWhiteSpace(url))
{
connectionSetting.DefaultIndex(defaultIndex);
}
return new ElasticClient(connectionSetting);
}
///
/// 根据urls获取ElasticClient
///
///
///
///
private ElasticClient GetClient(string[] urls, string defaultIndex = "")
{
if (urls == null || urls.Length 1)
{
throw new Exception("urls can not be null");
}
var uris = urls.Select(p => new Uri(p)).ToArray();
var connectionPool = new SniffingConnectionPool(uris);
var connectionSetting = new ConnectionSettings(connectionPool);
if (!string.IsNullOrWhiteSpace(defaultIndex))
{
connectionSetting.DefaultIndex(defaultIndex);
}
return new ElasticClient(connectionSetting);
}
}
---------用户密码验证(注释部分),可以配置在EsConfig中---------
///
/// 根据urls获取ElasticClient
///
///
///
///
public ElasticClient GetClient(string[] urls, string defaultIndex = "")
{
if (urls == null || urls.Length 1)
{
throw new Exception("urls can not be null");
}
var uris = urls.Select(p => new Uri(p)).ToArray();
var connectionPool = new SniffingConnectionPool(uris);
var connectionSetting = new ConnectionSettings(connectionPool);
if (!string.IsNullOrWhiteSpace(defaultIndex))
{
connectionSetting.DefaultIndex(defaultIndex);
}
//connectionSetting.BasicAuthentication("", ""); //设置账号密码
return new ElasticClient(connectionSetting);
}
2、封装操作ElasticSearch实现
1)、扩展ElasticClient类
///
/// ElasticClient 扩展类
///
public static class ElasticClientExtension
{
///
/// 创建索引
///
///
///
///
///
///
///
public static bool CreateIndexT>(this ElasticClient elasticClient, string indexName = "", int numberOfShards = 5, int numberOfReplicas = 1) where T : class
{
if (string.IsNullOrWhiteSpace(indexName))
{
indexName = typeof(T).Name;
}
if (elasticClient.Indices.Exists(indexName).Exists)
{
return false;
}
else
{
var indexState = new IndexState()
{
Settings = new IndexSettings()
{
NumberOfReplicas = numberOfReplicas,
NumberOfShards = numberOfShards,
},
};
var response = elasticClient.Indices.Create(indexName, p => p.InitializeUsing(indexState).MapT>(p => p.AutoMap()));
return response.Acknowledged;
}
}
}
2)、创建ElasticSearch操作基类
///
/// 接口限定
///
public interface IBaseEsContext { }
///
/// es操作基类
///
///
public abstract class BaseEsContextT> : IBaseEsContext where T : class
{
protected IEsClientProvider _EsClientProvider;
public abstract string IndexName { get; }
public BaseEsContext(IEsClientProvider provider)
{
_EsClientProvider = provider;
}
///
/// 批量更新
///
///
///
public bool InsertMany(ListT> tList)
{
var client = _EsClientProvider.GetClient(IndexName);
if (!client.Indices.Exists(IndexName).Exists)
{
client.CreateIndexT>(IndexName);
}
var response = client.IndexMany(tList);
//var response = client.Bulk(p=>p.Index(IndexName).IndexMany(tList));
return response.IsValid;
}
///
/// 获取总数
///
///
public long GetTotalCount()
{
var client = _EsClientProvider.GetClient(IndexName);
var search = new SearchDescriptorT>().MatchAll(); //指定查询字段 .Source(p => p.Includes(x => x.Field("Id")));
var response = client.SearchT>(search);
return response.Total;
}
///
/// 根据Id删除数据
///
///
public bool DeleteById(string id)
{
var client = _EsClientProvider.GetClient(IndexName);
var response = client.DeleteT>(id);
return response.IsValid;
}
}
3)、具体操作类(示例)
///
/// 地址操作类
///
public class AddressContext : BaseEsContextAddress>
{
///
/// 索引名称
///
public override string IndexName => "address";
public AddressContext(IEsClientProvider provider) : base(provider)
{
}
///
/// 获取地址
///
///
///
///
///
public ListAddress> GetAddresses(string province, int pageIndex, int pageSize)
{
var client = _EsClientProvider.GetClient(IndexName);
var musts = new ListFuncQueryContainerDescriptorAddress>, QueryContainer>>();
musts.Add(p => p.Term(m => m.Field(x=>x.Pronvince).Value(province)));
var search = new SearchDescriptorAddress>();
// 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.SearchAddress>(search);
return response.Documents.ToList();
}
///
/// 获取所有地址
///
///
public ListAddress> GetAllAddresses()
{
var client = _EsClientProvider.GetClient(IndexName);
var searchDescriptor = new SearchDescriptorAddress>();
// searchDescriptor = searchDescriptor.Index(IndexName).Query(p => p.MatchAll());
searchDescriptor = searchDescriptor.Query(p => p.MatchAll());
var response = client.SearchAddress>(searchDescriptor);
return response.Documents.ToList();
}
///
/// 删除指定城市的数据
///
///
///
public bool DeleteByQuery(string city)
{
var client = _EsClientProvider.GetClient(IndexName);
var musts = new ListFuncQueryContainerDescriptorAddress>, QueryContainer>>();
musts.Add(p=>p.Term(m=>m.Field(f=>f.City).Value(city)));
var search = new DeleteByQueryDescriptorAddress>().Index(IndexName);
search = search.Query(p => p.Bool(m => m.Must(musts)));
var response = client.DeleteByQueryAddress>(p=>search);
return response.IsValid;
}
}
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
1)、配置文件
1 "EsConfig": {
2 "ConnectionStrings": [ "http://127.0.0.1:9200/" ]
3 }
2)、注入ElasticSearch
services.ConfigureEsConfig>(options =>
{
options.Urls = Configuration.GetSection("EsConfig:ConnectionStrings").GetChildren().ToList().Select(p => p.Value).ToList();
});
services.AddSingletonIEsClientProvider, EsClientProvider>();
var types = Assembly.Load("John.DotNetCoreStudy.EsCommon").GetTypes().Where(p => !p.IsAbstract && (p.GetInterfaces().Any(i => i == typeof(IBaseEsContext)))).ToList();
types.ForEach(p =>
services.AddTransient(p)
);
3)、Controller类中使用
[Route("api/[controller]")]
[ApiController]
public class AddressController : ControllerBase
{
private AddressContext _AddressContext;
public AddressController(AddressContext context)
{
_AddressContext = context;
}
///
/// 新增或者修改
///
///
[HttpPost("添加地址")]
public void AddAddress(ListAddress> addressList)
{
if (addressList == null || addressList.Count 1)
{
return;
}
_AddressContext.InsertMany(addressList);
}
///
/// 删除地址
///
///
[HttpPost("deleteAddress")]
public void DeleteAdress(string id)
{
_AddressContext.DeleteById(id);
}
///
/// 获取所有与地址
///
///
[HttpGet("getAllAddress")]
public ListAddress> GetAllAddress()
{
return _AddressContext.GetAllAddresses();
}
///
/// 获取地址总数
///
///
[HttpGet("getAddressTotalCount")]
public long GetAddressTotalCount()
{
return _AddressContext.GetTotalCount();
}
///
/// 分页获取(可以进一步封装查询条件)
///
///
///
///
///
[HttpPost("getAddressByProvince")]
public ListAddress> GetAddressByProvince(string province,int pageIndex,int pageSize)
{
return _AddressContext.GetAddresses(province,pageIndex,pageSize);
}
}
4、测试(略)
当然es还有很多操作的,聚合查询、不同条件的查询(范围查询、匹配查询等等)、分词等。具体可以去查看其官方文档对应实现!
文章评论