diff --git a/src/Bing.Elasticsearch/Bing.Elasticsearch.xml b/src/Bing.Elasticsearch/Bing.Elasticsearch.xml index 96e3618..5e851d8 100644 --- a/src/Bing.Elasticsearch/Bing.Elasticsearch.xml +++ b/src/Bing.Elasticsearch/Bing.Elasticsearch.xml @@ -938,7 +938,7 @@ - 索引名称前缀 + 索引前缀(防止索引名称重复) diff --git a/src/Bing.Elasticsearch/ElasticsearchContext.cs b/src/Bing.Elasticsearch/ElasticsearchContext.cs index 880d4b5..6d2777b 100644 --- a/src/Bing.Elasticsearch/ElasticsearchContext.cs +++ b/src/Bing.Elasticsearch/ElasticsearchContext.cs @@ -117,7 +117,7 @@ public async Task CreateIndexAsync(string index, string ali /// 取消令牌 public async Task CreateIndexAsync(string index, string alias = null, CancellationToken cancellationToken = default) where TDocument : class { - await _client.CreateIndexAsync(index, _options.NumberOfShards, _options.NumberOfReplicas,cancellationToken); + await _client.CreateIndexAsync(index, _options.NumberOfShards, _options.NumberOfReplicas, cancellationToken); if (alias.IsEmpty() == false) await _client.Indices.PutAliasAsync(index, alias, ct: cancellationToken); } @@ -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); } /// @@ -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); } @@ -185,9 +185,9 @@ public async Task AddIndexesToAliasAsync(string alias, params string[] indexes) /// 索引名称列表。注意:必须小写 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); } @@ -211,7 +211,7 @@ public async Task RemoveIndexesFromAliasAsync(string alias, params string[] inde /// 索引名称。注意:必须小写 /// 取消令牌 /// 说明:最多返回10000条 - public async Task> GetAllAsync(string index = null, CancellationToken cancellationToken = default) + public async Task> GetAllAsync(string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -241,7 +241,7 @@ public async Task FindByIdAsync(object id, string index = /// 文档标识集合 /// 索引名称。注意:必须小写 /// 取消令牌 - public async Task> FindByIdsAsync(IEnumerable ids, string index = null, CancellationToken cancellationToken = default) + public async Task> FindByIdsAsync(IEnumerable ids, string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -259,7 +259,7 @@ public async Task> FindByIdsAsync(IEnumerable< /// 文档标识集合 /// 索引名称。注意:必须小写 /// 取消令牌 - public async Task> FindByIdsAsync(IEnumerable ids, string index = null, CancellationToken cancellationToken = default) + public async Task> FindByIdsAsync(IEnumerable ids, string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -276,7 +276,7 @@ public async Task> FindByIdsAsync(IEnumerable< /// 文档类型 /// 索引名称。注意:必须小写 /// 文档标识集合 - public Task> FindByIdsAsync(string index, params string[] ids) + public Task> FindByIdsAsync(string index, params string[] ids) where TDocument : class => FindByIdsAsync((IEnumerable)ids, index); /// @@ -295,7 +295,7 @@ public Task> FindByIdsAsync(string index, para /// 查询条件 /// 索引名称。注意:必须小写 /// 取消令牌 - public async Task> SearchAsync(Func, QueryContainer> searchTerms = null, string index = null, CancellationToken cancellationToken = default) + public async Task> SearchAsync(Func, QueryContainer> searchTerms = null, string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -309,7 +309,7 @@ public async Task> SearchAsync(Func文档类型 /// 查询表达式 /// 取消令牌 - public async Task> SearchAsync(Func, ISearchRequest> selector = null, CancellationToken cancellationToken = default) + public async Task> SearchAsync(Func, ISearchRequest> selector = null, CancellationToken cancellationToken = default) where TDocument : class { return await _client.SearchAsync(selector, cancellationToken); @@ -334,7 +334,7 @@ public async Task> SearchAsync(Func索引名称。注意:必须小写 /// 文档标识 /// 取消令牌 - public async Task AddAsync(TDocument document, string index = null, object id = null, CancellationToken cancellationToken = default) + public async Task AddAsync(TDocument document, string index = null, object id = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -351,7 +351,7 @@ public async Task AddAsync(TDocument document, string /// 索引名称。注意:必须小写 /// 超时时间间隔。单位:毫秒,默认值:300000,即5分钟 /// 取消令牌 - public async Task BulkSaveAsync(IEnumerable documents, string index = null, double timeout = 300000, CancellationToken cancellationToken = default) + public async Task BulkSaveAsync(IEnumerable documents, string index = null, double timeout = 300000, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -369,7 +369,7 @@ public async Task BulkSaveAsync(IEnumerable /// 文档标识 /// 索引名称。注意:必须小写 /// 取消令牌 - public async Task DeleteAsync(object id, string index = null, CancellationToken cancellationToken = default) + public async Task DeleteAsync(object id, string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -383,7 +383,7 @@ public async Task DeleteAsync(object id, string index /// 文档 /// 索引名称。注意:必须小写 /// 取消令牌 - public async Task DeleteAsync(TDocument document, string index = null, CancellationToken cancellationToken = default) + public async Task DeleteAsync(TDocument document, string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -408,7 +408,7 @@ public async Task DeleteByQueryAsync(Func文档 /// 索引名称。注意:必须小写 /// 取消令牌 - public async Task> UpdateAsync(TDocument document, string index = null, CancellationToken cancellationToken = default) + public async Task> UpdateAsync(TDocument document, string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); @@ -424,7 +424,7 @@ public async Task> UpdateAsync(TDocument do /// 文档 /// 索引名称。注意:必须小写 /// 取消令牌 - public async Task> UpdateAsync(object id, TDocument document, string index = null, CancellationToken cancellationToken = default) + public async Task> UpdateAsync(object id, TDocument document, string index = null, CancellationToken cancellationToken = default) where TDocument : class { index = GetIndexName(Helper.SafeIndexName(index)); diff --git a/src/Bing.Elasticsearch/Extensions/ElasticClientExtensions.cs b/src/Bing.Elasticsearch/Extensions/ElasticClientExtensions.cs index 23462a7..8ab2c22 100644 --- a/src/Bing.Elasticsearch/Extensions/ElasticClientExtensions.cs +++ b/src/Bing.Elasticsearch/Extensions/ElasticClientExtensions.cs @@ -34,12 +34,11 @@ public static async Task CreateIndexAsync(this IElasticClient client, var result = await client.Indices.CreateAsync( indexName, - x => x.Index(indexName) + x => x.Map(m=>m.AutoMap()) .Settings(o => o.NumberOfShards(numberOfShards) .NumberOfReplicas(numberOfReplicas) - .Setting("max_result_window", int.MaxValue)) - .Map(m => m.AutoMap()), + .Setting("max_result_window", int.MaxValue)), cancellationToken); if (!result.Acknowledged) throw new ElasticsearchException($"索引[{indexName}]创建失败:{result.ServerError.Error.Reason}"); diff --git a/src/Bing.Elasticsearch/Options/ElasticsearchOptions.cs b/src/Bing.Elasticsearch/Options/ElasticsearchOptions.cs index b3ec56c..4e517f5 100644 --- a/src/Bing.Elasticsearch/Options/ElasticsearchOptions.cs +++ b/src/Bing.Elasticsearch/Options/ElasticsearchOptions.cs @@ -65,7 +65,7 @@ public class ElasticsearchOptions public int NumberOfReplicas { get; set; } = 1; /// - /// 索引名称前缀 + /// 索引前缀(防止索引名称重复) /// public string Prefix { get; set; } diff --git a/tests/Bing.Elasticsearch.Tests/Models/StudentSample.cs b/tests/Bing.Elasticsearch.Tests/Models/StudentSample.cs index 3bdf415..422a632 100644 --- a/tests/Bing.Elasticsearch.Tests/Models/StudentSample.cs +++ b/tests/Bing.Elasticsearch.Tests/Models/StudentSample.cs @@ -17,5 +17,7 @@ public class StudentSample public DateTime BirthDay { get; set; } public bool IsValid { get; set; } + + public Guid UserId { get; set; } } }