Skip to content

Commit

Permalink
feat: 支持映射配置文件
Browse files Browse the repository at this point in the history
  • Loading branch information
jianxuanbing committed Apr 12, 2022
1 parent 430138c commit 96afeab
Show file tree
Hide file tree
Showing 22 changed files with 904 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Bing.Elasticsearch\Bing.Elasticsearch.csproj" />
</ItemGroup>
Expand Down
258 changes: 257 additions & 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.

13 changes: 11 additions & 2 deletions src/Bing.Elasticsearch/ElasticsearchContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Bing.Elasticsearch.Internals;
using Bing.Elasticsearch.Mapping;
using Bing.Elasticsearch.Options;
using Bing.Elasticsearch.Provider;
using Bing.Elasticsearch.Repositories;
Expand Down Expand Up @@ -39,16 +40,23 @@ public class ElasticsearchContext : IElasticsearchContext
/// </summary>
private readonly ElasticsearchOptions _options;

/// <summary>
/// ES映射工厂
/// </summary>
private readonly IElasticMappingFactory _mappingFactory;

/// <summary>
/// 初始化一个<see cref="ElasticsearchContext"/>类型的实例
/// </summary>
/// <param name="provider">ES客户端提供程序</param>
/// <param name="resolver">索引名称解析器</param>
/// <param name="mappingFactory">ES映射工厂</param>
/// <param name="options">ES选项配置</param>
public ElasticsearchContext(IElasticClientProvider provider, IIndexNameResolver resolver, IOptions<ElasticsearchOptions> options)
public ElasticsearchContext(IElasticClientProvider provider, IIndexNameResolver resolver, IElasticMappingFactory mappingFactory, IOptions<ElasticsearchOptions> options)
{
_provider = provider;
_resolver = resolver;
_mappingFactory = mappingFactory;
_client = provider.GetClient();
_options = options.Value;
}
Expand Down Expand Up @@ -117,7 +125,8 @@ 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);
await _client.CreateIndexAsync<TDocument>(_mappingFactory, index, cancellationToken);
if (alias.IsEmpty() == false)
await _client.Indices.PutAliasAsync(index, alias, ct: cancellationToken);
}
Expand Down
4 changes: 4 additions & 0 deletions src/Bing.Elasticsearch/Extensions.Service.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Bing.Elasticsearch.Mapping;
using Bing.Elasticsearch.Options;
using Bing.Elasticsearch.Provider;
using Bing.Elasticsearch.Repositories;
Expand All @@ -20,9 +21,12 @@ public static partial class Extensions
public static void AddElasticsearch(this IServiceCollection services, Action<ElasticsearchOptions> setupAction)
{
services.Configure(setupAction);
services.GetOrAddAllAssemblyFinder();
services.GetOrAddTypeFinder<IElasticMappingTypeFinder>(assemblyFinder => new ElasticMappingTypeFinder(assemblyFinder));
services.TryAddSingleton<IIndexNameResolver, IndexNameResolver>();
services.TryAddSingleton<IElasticClientProvider, ElasticClientProvider>();
services.TryAddScoped<IElasticsearchContext, ElasticsearchContext>();
services.TryAddSingleton<IElasticMappingFactory, ElasticMappingFactory>();
services.TryAddScoped(typeof(IEsRepository<>), typeof(EsRepository<>));
}
}
Expand Down
Loading

0 comments on commit 96afeab

Please sign in to comment.