Skip to content

Commit

Permalink
fix: 优化索引创建方式
Browse files Browse the repository at this point in the history
  • Loading branch information
jianxuanbing committed Apr 5, 2022
1 parent f55b9be commit 430138c
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/Bing.Elasticsearch/Bing.Elasticsearch.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 19 additions & 19 deletions src/Bing.Elasticsearch/ElasticsearchContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async Task<CreateIndexResponse> CreateIndexAsync(string index, string ali
/// <param name="cancellationToken">取消令牌</param>
public async Task CreateIndexAsync<TDocument>(string index, string alias = null, CancellationToken cancellationToken = default) where TDocument : class
{
await _client.CreateIndexAsync<TDocument>(index, _options.NumberOfShards, _options.NumberOfReplicas,cancellationToken);
await _client.CreateIndexAsync<TDocument>(index, _options.NumberOfShards, _options.NumberOfReplicas, cancellationToken);
if (alias.IsEmpty() == false)
await _client.Indices.PutAliasAsync(index, alias, ct: cancellationToken);
}
Expand Down Expand Up @@ -153,9 +153,9 @@ public async Task DeleteIndexesByAliasAsync(string alias, CancellationToken canc
{
if (alias.IsEmpty())
return;
var indexes = await GetIndexesByAliasAsync(alias,cancellationToken);
foreach (var index in indexes)
await DeleteIndexAsync(index,cancellationToken);
var indexes = await GetIndexesByAliasAsync(alias, cancellationToken);
foreach (var index in indexes)
await DeleteIndexAsync(index, cancellationToken);
}

/// <summary>
Expand All @@ -174,7 +174,7 @@ public async Task AddIndexesToAliasAsync(string alias, params string[] indexes)
{
if (alias.IsEmpty())
return;
foreach (var index in indexes)
foreach (var index in indexes)
await _client.Indices.PutAliasAsync(GetIndexName(index), alias);
}

Expand All @@ -185,9 +185,9 @@ public async Task AddIndexesToAliasAsync(string alias, params string[] indexes)
/// <param name="indexes">索引名称列表。注意:必须小写</param>
public async Task RemoveIndexesFromAliasAsync(string alias, params string[] indexes)
{
if(alias.IsEmpty())
if (alias.IsEmpty())
return;
foreach (var index in indexes)
foreach (var index in indexes)
await _client.Indices.DeleteAliasAsync(GetIndexName(index), alias);
}

Expand All @@ -211,7 +211,7 @@ public async Task RemoveIndexesFromAliasAsync(string alias, params string[] inde
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
/// <remarks>说明:最多返回10000条</remarks>
public async Task<List<TDocument>> GetAllAsync<TDocument>(string index = null, CancellationToken cancellationToken = default)
public async Task<List<TDocument>> GetAllAsync<TDocument>(string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand Down Expand Up @@ -241,7 +241,7 @@ public async Task<TDocument> FindByIdAsync<TDocument>(object id, string index =
/// <param name="ids">文档标识集合</param>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(IEnumerable<string> ids, string index = null, CancellationToken cancellationToken = default)
public async Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(IEnumerable<string> ids, string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -259,7 +259,7 @@ public async Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(IEnumerable<
/// <param name="ids">文档标识集合</param>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(IEnumerable<long> ids, string index = null, CancellationToken cancellationToken = default)
public async Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(IEnumerable<long> ids, string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -276,7 +276,7 @@ public async Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(IEnumerable<
/// <typeparam name="TDocument">文档类型</typeparam>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="ids">文档标识集合</param>
public Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(string index, params string[] ids)
public Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(string index, params string[] ids)
where TDocument : class => FindByIdsAsync<TDocument>((IEnumerable<string>)ids, index);

/// <summary>
Expand All @@ -295,7 +295,7 @@ public Task<IEnumerable<TDocument>> FindByIdsAsync<TDocument>(string index, para
/// <param name="searchTerms">查询条件</param>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<ISearchResponse<TDocument>> SearchAsync<TDocument>(Func<QueryContainerDescriptor<TDocument>, QueryContainer> searchTerms = null, string index = null, CancellationToken cancellationToken = default)
public async Task<ISearchResponse<TDocument>> SearchAsync<TDocument>(Func<QueryContainerDescriptor<TDocument>, QueryContainer> searchTerms = null, string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -309,7 +309,7 @@ public async Task<ISearchResponse<TDocument>> SearchAsync<TDocument>(Func<QueryC
/// <typeparam name="TDocument">文档类型</typeparam>
/// <param name="selector">查询表达式</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<ISearchResponse<TDocument>> SearchAsync<TDocument>(Func<SearchDescriptor<TDocument>, ISearchRequest> selector = null, CancellationToken cancellationToken = default)
public async Task<ISearchResponse<TDocument>> SearchAsync<TDocument>(Func<SearchDescriptor<TDocument>, ISearchRequest> selector = null, CancellationToken cancellationToken = default)
where TDocument : class
{
return await _client.SearchAsync<TDocument>(selector, cancellationToken);
Expand All @@ -334,7 +334,7 @@ public async Task<ISearchResponse<TDocument>> SearchAsync<TDocument>(Func<Search
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="id">文档标识</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<IndexResponse> AddAsync<TDocument>(TDocument document, string index = null, object id = null, CancellationToken cancellationToken = default)
public async Task<IndexResponse> AddAsync<TDocument>(TDocument document, string index = null, object id = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -351,7 +351,7 @@ public async Task<IndexResponse> AddAsync<TDocument>(TDocument document, string
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="timeout">超时时间间隔。单位:毫秒,默认值:300000,即5分钟</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<BulkResponse> BulkSaveAsync<TDocument>(IEnumerable<TDocument> documents, string index = null, double timeout = 300000, CancellationToken cancellationToken = default)
public async Task<BulkResponse> BulkSaveAsync<TDocument>(IEnumerable<TDocument> documents, string index = null, double timeout = 300000, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -369,7 +369,7 @@ public async Task<BulkResponse> BulkSaveAsync<TDocument>(IEnumerable<TDocument>
/// <param name="id">文档标识</param>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<DeleteResponse> DeleteAsync<TDocument>(object id, string index = null, CancellationToken cancellationToken = default)
public async Task<DeleteResponse> DeleteAsync<TDocument>(object id, string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -383,7 +383,7 @@ public async Task<DeleteResponse> DeleteAsync<TDocument>(object id, string index
/// <param name="document">文档</param>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<DeleteResponse> DeleteAsync<TDocument>(TDocument document, string index = null, CancellationToken cancellationToken = default)
public async Task<DeleteResponse> DeleteAsync<TDocument>(TDocument document, string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -408,7 +408,7 @@ public async Task<DeleteByQueryResponse> DeleteByQueryAsync<TDocument>(Func<Dele
/// <param name="document">文档</param>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<UpdateResponse<TDocument>> UpdateAsync<TDocument>(TDocument document, string index = null, CancellationToken cancellationToken = default)
public async Task<UpdateResponse<TDocument>> UpdateAsync<TDocument>(TDocument document, string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand All @@ -424,7 +424,7 @@ public async Task<UpdateResponse<TDocument>> UpdateAsync<TDocument>(TDocument do
/// <param name="document">文档</param>
/// <param name="index">索引名称。注意:必须小写</param>
/// <param name="cancellationToken">取消令牌</param>
public async Task<UpdateResponse<TDocument>> UpdateAsync<TDocument>(object id, TDocument document, string index = null, CancellationToken cancellationToken = default)
public async Task<UpdateResponse<TDocument>> UpdateAsync<TDocument>(object id, TDocument document, string index = null, CancellationToken cancellationToken = default)
where TDocument : class
{
index = GetIndexName(Helper.SafeIndexName<TDocument>(index));
Expand Down
5 changes: 2 additions & 3 deletions src/Bing.Elasticsearch/Extensions/ElasticClientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ public static async Task CreateIndexAsync<T>(this IElasticClient client,

var result = await client.Indices.CreateAsync(
indexName,
x => x.Index(indexName)
x => x.Map<T>(m=>m.AutoMap())
.Settings(o =>
o.NumberOfShards(numberOfShards)
.NumberOfReplicas(numberOfReplicas)
.Setting("max_result_window", int.MaxValue))
.Map(m => m.AutoMap<T>()),
.Setting("max_result_window", int.MaxValue)),
cancellationToken);
if (!result.Acknowledged)
throw new ElasticsearchException($"索引[{indexName}]创建失败:{result.ServerError.Error.Reason}");
Expand Down
2 changes: 1 addition & 1 deletion src/Bing.Elasticsearch/Options/ElasticsearchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public class ElasticsearchOptions
public int NumberOfReplicas { get; set; } = 1;

/// <summary>
/// 索引名称前缀
/// 索引前缀(防止索引名称重复)
/// </summary>
public string Prefix { get; set; }

Expand Down
2 changes: 2 additions & 0 deletions tests/Bing.Elasticsearch.Tests/Models/StudentSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public class StudentSample
public DateTime BirthDay { get; set; }

public bool IsValid { get; set; }

public Guid UserId { get; set; }
}
}

0 comments on commit 430138c

Please sign in to comment.