9 changed files with 216 additions and 1 deletions
			
			
		| @ -0,0 +1,3 @@ | ||||
| <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> | ||||
|   <ConfigureAwait ContinueOnCapturedContext="false" /> | ||||
| </Weavers> | ||||
| @ -0,0 +1,37 @@ | ||||
| # Sanhe.Abp.Elasticsearch | ||||
| 
 | ||||
| Abp Elasticsearch集成,提供全局唯一IElasticClient访问接口 | ||||
| 
 | ||||
| ## 模块引用 | ||||
| 
 | ||||
| 
 | ||||
| ```csharp | ||||
| [DependsOn(typeof(AbpElasticsearchModule))] | ||||
| public class YouProjectModule : AbpModule | ||||
| { | ||||
|   // other | ||||
| } | ||||
| ``` | ||||
| 
 | ||||
| ## 配置项 | ||||
| 
 | ||||
| *	AbpElasticsearchOptions.FieldCamelCase      字段是否采用 camelCase 格式, 默认false | ||||
| *	AbpElasticsearchOptions.NodeUris            ES端点,多个端点以,或;分隔 | ||||
| *	AbpElasticsearchOptions.TypeName            文档名称,默认_doc | ||||
| *	AbpElasticsearchOptions.ConnectionLimit     最大连接数,详情见 NEST 文档 | ||||
| *	AbpElasticsearchOptions.UserName            连接用户,详情见 NEST 文档 | ||||
| *	AbpElasticsearchOptions.Password            用户密码,详情见 NEST 文档 | ||||
| *	AbpElasticsearchOptions.ConnectionTimeout   连接超时时间,详情见 NEST 文档 | ||||
| 
 | ||||
| ## appsettings.json | ||||
| 
 | ||||
| ```json | ||||
| { | ||||
|   "Elasticsearch": { | ||||
|     "NodeUris": "http://localhost:9200" | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| ``` | ||||
| 
 | ||||
| [作者colinin,Github](https://github.com/colinin/abp-next-admin) | ||||
| @ -0,0 +1,15 @@ | ||||
| <Project Sdk="Microsoft.NET.Sdk"> | ||||
| 
 | ||||
| 	<Import Project="..\..\..\configureawait.props" /> | ||||
| 	<Import Project="..\..\..\common.props" /> | ||||
| 
 | ||||
| 	<PropertyGroup> | ||||
| 		<TargetFramework>netstandard2.0</TargetFramework> | ||||
| 		<RootNamespace /> | ||||
| 	</PropertyGroup> | ||||
| 
 | ||||
| 	<ItemGroup> | ||||
| 		<PackageReference Include="NEST" Version="$(NESTPackageVersion)" /> | ||||
| 		<PackageReference Include="Volo.Abp.Core" Version="$(VoloAbpVersion)" /> | ||||
| 	</ItemGroup> | ||||
| </Project> | ||||
| @ -0,0 +1,14 @@ | ||||
| using Microsoft.Extensions.DependencyInjection; | ||||
| using Volo.Abp.Modularity; | ||||
| 
 | ||||
| namespace Sanhe.Abp.Elasticsearch | ||||
| { | ||||
|     public class AbpElasticsearchModule : AbpModule | ||||
|     { | ||||
|         public override void ConfigureServices(ServiceConfigurationContext context) | ||||
|         { | ||||
|             var configuration = context.Services.GetConfiguration(); | ||||
|             Configure<AbpElasticsearchOptions>(configuration.GetSection("Elasticsearch")); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,94 @@ | ||||
| using Elasticsearch.Net; | ||||
| using Nest; | ||||
| using System; | ||||
| using System.Linq; | ||||
| 
 | ||||
| namespace Sanhe.Abp.Elasticsearch | ||||
| { | ||||
|     public class AbpElasticsearchOptions | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// 字段名称 是否为 camelCase 格式。 | ||||
|         /// 如果为true, 在ES中为 camelCase。 | ||||
|         /// 如果为false,在ES中为 CamelCase。 | ||||
|         /// 默认:false。 | ||||
|         /// </summary> | ||||
|         public bool FieldCamelCase { get; set; } | ||||
|         /// <summary> | ||||
|         /// Ensures the response bytes are always available on the <see cref="ElasticsearchResponse{T}" /> | ||||
|         /// </summary> | ||||
|         public bool DisableDirectStreaming { get; set; } | ||||
|         /// <summary> | ||||
|         /// ES端点,多个端点以,或;分隔 | ||||
|         /// </summary> | ||||
|         public string NodeUris { get; set; } | ||||
|         /// <summary> | ||||
|         /// 最大连接数 | ||||
|         /// </summary> | ||||
|         public int ConnectionLimit { get; set; } | ||||
|         /// <summary> | ||||
|         /// 连接用户名 | ||||
|         /// </summary> | ||||
|         public string UserName { get; set; } | ||||
|         /// <summary> | ||||
|         /// 用户密码 | ||||
|         /// </summary> | ||||
|         public string Password { get; set; } | ||||
|         /// <summary> | ||||
|         /// 连接超时时间 | ||||
|         /// </summary> | ||||
|         public TimeSpan ConnectionTimeout { get; set; } | ||||
|         /// <summary> | ||||
|         ///  | ||||
|         /// </summary> | ||||
|         public IConnection Connection { get; set; } | ||||
|         /// <summary> | ||||
|         ///  | ||||
|         /// </summary> | ||||
|         public ConnectionSettings.SourceSerializerFactory SerializerFactory { get; set; } | ||||
| 
 | ||||
|         public AbpElasticsearchOptions() | ||||
|         { | ||||
|             ConnectionLimit = ConnectionConfiguration.DefaultConnectionLimit; | ||||
|             ConnectionTimeout = ConnectionConfiguration.DefaultTimeout; | ||||
|         } | ||||
| 
 | ||||
|         internal IConnectionSettingsValues CreateConfiguration() | ||||
|         { | ||||
|             IConnectionPool connectionPool; | ||||
|             var nodes = NodeUris | ||||
|                 .Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) | ||||
|                 .Select(uriString => new Uri(uriString)); | ||||
| 
 | ||||
|             if (nodes.Count() == 1) | ||||
|             { | ||||
|                 connectionPool = new SingleNodeConnectionPool(nodes.First()); | ||||
|             } | ||||
|             else | ||||
|             { | ||||
|                 connectionPool = new StaticConnectionPool(nodes); | ||||
|             } | ||||
| 
 | ||||
|             var configuration = new ConnectionSettings( | ||||
|                 connectionPool, | ||||
|                 Connection, | ||||
|                 SerializerFactory) | ||||
|                 .ConnectionLimit(ConnectionLimit) | ||||
|                 .RequestTimeout(ConnectionTimeout); | ||||
| 
 | ||||
|             if (!FieldCamelCase) | ||||
|             { | ||||
|                 configuration.DefaultFieldNameInferrer((name) => name); | ||||
|             } | ||||
| 
 | ||||
|             if (UserName.IsNullOrWhiteSpace()) | ||||
|             { | ||||
|                 configuration.BasicAuthentication(UserName, Password); | ||||
|             } | ||||
| 
 | ||||
|             configuration.DisableDirectStreaming(DisableDirectStreaming); | ||||
| 
 | ||||
|             return configuration; | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @ -0,0 +1,32 @@ | ||||
| using Microsoft.Extensions.Options; | ||||
| using Nest; | ||||
| using System; | ||||
| using Volo.Abp.DependencyInjection; | ||||
| 
 | ||||
| namespace Sanhe.Abp.Elasticsearch | ||||
| { | ||||
|     public class ElasticsearchClientFactory : IElasticsearchClientFactory, ISingletonDependency | ||||
|     { | ||||
|         private readonly AbpElasticsearchOptions _options; | ||||
|         private readonly Lazy<IElasticClient> _lazyClient; | ||||
| 
 | ||||
|         public ElasticsearchClientFactory( | ||||
|             IOptions<AbpElasticsearchOptions> options) | ||||
|         { | ||||
|             _options = options.Value; | ||||
| 
 | ||||
|             _lazyClient = new Lazy<IElasticClient>(CreateClient); | ||||
|         } | ||||
| 
 | ||||
|         public IElasticClient Create() => _lazyClient.Value; | ||||
| 
 | ||||
|         protected virtual IElasticClient CreateClient() | ||||
|         { | ||||
|             var configuration = _options.CreateConfiguration(); | ||||
| 
 | ||||
|             var client = new ElasticClient(configuration); | ||||
| 
 | ||||
|             return client; | ||||
|         } | ||||
|     } | ||||
| } | ||||
					Loading…
					
					
				
		Reference in new issue