我是靠谱客的博主 机灵棒棒糖,最近开发中收集的这篇文章主要介绍elasticsearch 客户端NEST 7.6.2最新版本使用.net core 封装 curd,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
第一步,安装Elasticsearch ,不清楚的看我上篇文章
第二步,NuGet引用NEST包到项目中,我目前使用的是最新的版本 7.6.2
第三步:上代码~
#region CreateDocument
/// <summary>
/// Create Document
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="elasticClient"></param>
/// <param name="indexName">索引名称</param>
/// <param name="entity"></param>
/// <param name="id">文档Id需为唯一标识。文档Id主要用途:1.修改 / 删除 数据时,需指定文档Id</param>
/// <returns></returns>
public static CreateResponse CreateDocument<TEntity>(this IElasticClient elasticClient
, TEntity entity
, string indexName
)
where TEntity : class
{
string idValue = ElasticsearchKit.GetIdPropertyValue(entity).ToStrAndTrim();
CreateResponse response = elasticClient.Create<TEntity>(entity, t => t.Index(indexName).Id(idValue));
return response;
}
#endregion
#region UpdateDocument
/// <summary>
/// Update Document
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="elasticClient"></param>
/// <param name="entity"></param>
/// <returns></returns>
public static UpdateResponse<TEntity> UpdateDocument<TEntity>(this IElasticClient elasticClient
, TEntity entity
)
where TEntity : class
{
string idValue = ElasticsearchKit.GetIdPropertyValue(entity).ToStrAndTrim();
// [ 全文更新 ]
UpdateResponse<TEntity> response = elasticClient.Update<TEntity>(idValue, t => t.Doc(entity));
return response;
}
/// <summary>
/// 部分更新
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TPartialEntity"></typeparam>
/// <param name="elasticClient"></param>
/// <param name="request"></param>
/// <returns></returns>
public static UpdateResponse<TEntity> UpdateDocumentPartial<TEntity, TPartialEntity>(this IElasticClient elasticClient
, IUpdateRequest<TEntity, TPartialEntity> request
)
where TEntity : class
where TPartialEntity : class
{
//[
部分字段/局部更新 (TPartialEntity中的字段) ]
UpdateResponse<TEntity> response = elasticClient.Update(request);
return response;
}
/// <summary>
/// 部分更新
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <typeparam name="TPartialEntity"></typeparam>
/// <param name="elasticClient"></param>
/// <param name="request"></param>
/// <returns></returns>
public static UpdateResponse<TEntity> UpdateDocumentPartial<TEntity>(this IElasticClient elasticClient
, TEntity entity
, object partialEntity
, string indexName = null
)
where TEntity : class
{
string idValue = ElasticsearchKit.GetIdPropertyValue(entity).ToStrAndTrim();
IUpdateRequest<TEntity, object> request = new UpdateRequest<TEntity, object>(indexName, idValue)
{
Doc = partialEntity
};
//[
部分字段/局部更新 (TPartialEntity中的字段) ]
UpdateResponse<TEntity> response = elasticClient.Update(request);
return response;
}
#endregion
#region DeleteDocument
public static DeleteResponse DeleteDocument<TEntity>(this IElasticClient elasticClient
, TEntity entity
, string indexName
)
{
string idValue = ElasticsearchKit.GetIdPropertyValue(entity).ToStrAndTrim();
return DeleteDocument(elasticClient, indexName, idValue);
}
public static DeleteResponse DeleteDocument(this IElasticClient elasticClient
, string id
, string indexName
)
{
DeleteRequest request = new DeleteRequest(indexName, id);
DeleteResponse response = elasticClient.Delete(request);
return response;
}
public static DeleteResponse DeleteDocument(this IElasticClient elasticClient
, long id
, string indexName
)
{
DeleteRequest request = new DeleteRequest(indexName, id);
DeleteResponse response = elasticClient.Delete(request);
return response;
}
#endregion
#region GetVersionDocument
public static GetResponse<TEntity> GetVersion<TEntity>(this IElasticClient elasticClient
, string id
, string indexName
) where TEntity : class
{
DocumentPath<TEntity> path = new DocumentPath<TEntity>(id);
var response = elasticClient.Get<TEntity>(path, s => s.Index(indexName));
return response;// response.Version
}
public static GetResponse<TEntity> GetDocument<TEntity>(this IElasticClient elasticClient
, string id
, string indexName
) where TEntity : class
{
DocumentPath<TEntity> path = new DocumentPath<TEntity>(id);
var response = elasticClient.Get<TEntity>(path, s => s.Index(indexName));
return response;// response.Version
}
#endregion
/// <summary>
/// 获取IdProperty的名称
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public static string GetIdPropertyName<TEntity>(TEntity entity)
{
ElasticsearchTypeAttribute elasticsearchType = ReflectionKit.GetClassCustomAttribute<ElasticsearchTypeAttribute>(typeof(TEntity));
if (elasticsearchType.NotNull())
return elasticsearchType.IdProperty;
else
throw new Exception("");
}
/// <summary>
/// 获取 IdProperty 的 值
/// </summary>
/// <typeparam name="TEntity"></typeparam>
/// <param name="entity"></param>
/// <returns></returns>
public static object GetIdPropertyValue<TEntity>(TEntity entity)
{
string idPropertyName = GetIdPropertyName(entity);
if (idPropertyName.NotNull())
{
Dictionary<string, object> keyValues = entity.GetProperty();
keyValues.TryGetValue(idPropertyName, out object obj);
return obj;
}
return null;
}
#region ExistsIndex
/// <summary>
/// 判断索引是否存在
/// </summary>
/// <param name="elasticClient"></param>
/// <param name="indexName"></param>
/// <param name="selector"></param>
/// <returns></returns>
public static bool ExistsIndex(this IElasticClient elasticClient
, string indexName, Func<IndexExistsDescriptor, IIndexExistsRequest> selector = null)
{
var existResponse = elasticClient.Indices.Exists(indexName, selector);
return existResponse.Exists;
}
#endregion
#region CreateIndex
/// <summary>
/// 创建索引
/// </summary>
/// <param name="elasticClient"></param>
public static CreateIndexResponse CreateIndex(this IElasticClient elasticClient
, string indexName
, int numberOfReplicas = 1
, int numberOfShards = 5
)
{
IIndexState indexState = new IndexState
{
Settings = new IndexSettings
{
NumberOfReplicas = numberOfReplicas,[ 副本数量 ]
NumberOfShards = numberOfShards [ 分片数量 ]
}
};
Func<CreateIndexDescriptor, ICreateIndexRequest> func = x => x.InitializeUsing(indexState).Map(m => m.AutoMap());
CreateIndexResponse response = elasticClient.Indices.Create(indexName, func);
return response;
}
#endregion
#region DeleteIndex
/// <summary>
/// 删除索引
/// </summary>
/// <param name="elasticClient"></param>
/// <param name="indexName"></param>
public static DeleteIndexResponse DeleteIndex(this IElasticClient elasticClient, string indexName)
{
DeleteIndexResponse response = elasticClient.Indices.Delete(indexName);
return response;
}
#endregion
欢迎各位一起留言一起交流一起学习~~
最后
以上就是机灵棒棒糖为你收集整理的elasticsearch 客户端NEST 7.6.2最新版本使用.net core 封装 curd的全部内容,希望文章能够帮你解决elasticsearch 客户端NEST 7.6.2最新版本使用.net core 封装 curd所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复