Browse Source

add elasticsearch

master
guotianliang 3 years ago
parent
commit
251d810a0e
  1. 1
      Directory.Build.props
  2. 12
      Sanhe.Abp.Framework.sln
  3. 3
      modules/elasticsearch/Sanhe.Abp.Elasticsearch/FodyWeavers.xml
  4. 37
      modules/elasticsearch/Sanhe.Abp.Elasticsearch/README.md
  5. 15
      modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe.Abp.Elasticsearch.csproj
  6. 14
      modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/AbpElasticsearchModule.cs
  7. 94
      modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/AbpElasticsearchOptions.cs
  8. 32
      modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/ElasticsearchClientFactory.cs
  9. 9
      modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/IElasticsearchClientFactory.cs

1
Directory.Build.props

@ -4,5 +4,6 @@
<StackExchangeRedisVersion>2.0.593</StackExchangeRedisVersion> <StackExchangeRedisVersion>2.0.593</StackExchangeRedisVersion>
<MicrosoftPackageVersion>6.0.*</MicrosoftPackageVersion> <MicrosoftPackageVersion>6.0.*</MicrosoftPackageVersion>
<HangfirePostgreSqlStorageVersion>1.9.7</HangfirePostgreSqlStorageVersion> <HangfirePostgreSqlStorageVersion>1.9.7</HangfirePostgreSqlStorageVersion>
<NESTPackageVersion>7.15.1</NESTPackageVersion>
</PropertyGroup> </PropertyGroup>
</Project> </Project>

12
Sanhe.Abp.Framework.sln

@ -85,7 +85,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sanhe.Abp.Identity.HttpApi"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sanhe.Abp.Identity.HttpApi.Client", "modules\identity\Sanhe.Abp.Identity.HttpApi.Client\Sanhe.Abp.Identity.HttpApi.Client.csproj", "{6E5D3C18-45B4-478B-B064-140C5CDDD327}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sanhe.Abp.Identity.HttpApi.Client", "modules\identity\Sanhe.Abp.Identity.HttpApi.Client\Sanhe.Abp.Identity.HttpApi.Client.csproj", "{6E5D3C18-45B4-478B-B064-140C5CDDD327}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanhe.Abp.EntityFrameworkCore", "modules\common\Sanhe.Abp.EntityFrameworkCore\Sanhe.Abp.EntityFrameworkCore.csproj", "{464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sanhe.Abp.EntityFrameworkCore", "modules\common\Sanhe.Abp.EntityFrameworkCore\Sanhe.Abp.EntityFrameworkCore.csproj", "{464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "elasticsearch", "elasticsearch", "{449F764E-D3D5-4DFD-8707-2A378CF44C64}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sanhe.Abp.Elasticsearch", "modules\elasticsearch\Sanhe.Abp.Elasticsearch\Sanhe.Abp.Elasticsearch.csproj", "{3E1DA541-4D48-47A1-93FA-3D75BCB078F3}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -213,6 +217,10 @@ Global
{464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}.Debug|Any CPU.Build.0 = Debug|Any CPU {464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}.Release|Any CPU.ActiveCfg = Release|Any CPU {464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}.Release|Any CPU.Build.0 = Release|Any CPU {464E3A2C-BBEB-4CD9-BAB8-A51D6283940A}.Release|Any CPU.Build.0 = Release|Any CPU
{3E1DA541-4D48-47A1-93FA-3D75BCB078F3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3E1DA541-4D48-47A1-93FA-3D75BCB078F3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3E1DA541-4D48-47A1-93FA-3D75BCB078F3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3E1DA541-4D48-47A1-93FA-3D75BCB078F3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
@ -254,6 +262,8 @@ Global
{F53FC671-99FD-4CD8-915E-5B77619A6C6B} = {BAA5EE9D-A5C6-45F3-A89B-B3CC30FF3DFE} {F53FC671-99FD-4CD8-915E-5B77619A6C6B} = {BAA5EE9D-A5C6-45F3-A89B-B3CC30FF3DFE}
{6E5D3C18-45B4-478B-B064-140C5CDDD327} = {BAA5EE9D-A5C6-45F3-A89B-B3CC30FF3DFE} {6E5D3C18-45B4-478B-B064-140C5CDDD327} = {BAA5EE9D-A5C6-45F3-A89B-B3CC30FF3DFE}
{464E3A2C-BBEB-4CD9-BAB8-A51D6283940A} = {2A768109-31B7-4C52-928C-3023DAB9F254} {464E3A2C-BBEB-4CD9-BAB8-A51D6283940A} = {2A768109-31B7-4C52-928C-3023DAB9F254}
{449F764E-D3D5-4DFD-8707-2A378CF44C64} = {F5F5D604-531B-4B57-A88E-C9C5CEEC55D7}
{3E1DA541-4D48-47A1-93FA-3D75BCB078F3} = {449F764E-D3D5-4DFD-8707-2A378CF44C64}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {AB69BFDE-9DDB-4D16-8CB8-72472C0319CD} SolutionGuid = {AB69BFDE-9DDB-4D16-8CB8-72472C0319CD}

3
modules/elasticsearch/Sanhe.Abp.Elasticsearch/FodyWeavers.xml

@ -0,0 +1,3 @@
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<ConfigureAwait ContinueOnCapturedContext="false" />
</Weavers>

37
modules/elasticsearch/Sanhe.Abp.Elasticsearch/README.md

@ -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)

15
modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe.Abp.Elasticsearch.csproj

@ -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>

14
modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/AbpElasticsearchModule.cs

@ -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"));
}
}
}

94
modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/AbpElasticsearchOptions.cs

@ -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;
}
}
}

32
modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/ElasticsearchClientFactory.cs

@ -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;
}
}
}

9
modules/elasticsearch/Sanhe.Abp.Elasticsearch/Sanhe/Abp/Elasticsearch/IElasticsearchClientFactory.cs

@ -0,0 +1,9 @@
using Nest;
namespace Sanhe.Abp.Elasticsearch
{
public interface IElasticsearchClientFactory
{
IElasticClient Create();
}
}
Loading…
Cancel
Save