Browse Source

fix(elasticseatch): Use the `ElasticsearchClient` client

pull/1416/head
colin 9 months ago
parent
commit
ea40a8640c
  1. 75
      aspnet-core/framework/elasticsearch/LINGYUN.Abp.Elasticsearch/LINGYUN/Abp/Elasticsearch/AbpElasticsearchOptions.cs
  2. 16
      aspnet-core/framework/elasticsearch/LINGYUN.Abp.Elasticsearch/LINGYUN/Abp/Elasticsearch/ElasticsearchClientFactory.cs
  3. 4
      aspnet-core/framework/elasticsearch/LINGYUN.Abp.Elasticsearch/LINGYUN/Abp/Elasticsearch/IElasticsearchClientFactory.cs

75
aspnet-core/framework/elasticsearch/LINGYUN.Abp.Elasticsearch/LINGYUN/Abp/Elasticsearch/AbpElasticsearchOptions.cs

@ -1,8 +1,9 @@
using Elasticsearch.Net; using Elastic.Clients.Elasticsearch;
using Nest; using Elastic.Transport;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using static Elastic.Clients.Elasticsearch.ElasticsearchClientSettings;
namespace LINGYUN.Abp.Elasticsearch namespace LINGYUN.Abp.Elasticsearch
{ {
@ -15,56 +16,90 @@ namespace LINGYUN.Abp.Elasticsearch
/// 默认:false /// 默认:false
/// </summary> /// </summary>
public bool FieldCamelCase { get; set; } public bool FieldCamelCase { get; set; }
/// <summary>
/// When set to true will disable (de)serializing directly to the request and response stream and return a byte[] copy of the raw request and response.
/// Defaults to false.
/// </summary>
public bool DisableDirectStreaming { get; set; } public bool DisableDirectStreaming { get; set; }
public string NodeUris { get; set; } public string NodeUris { get; set; }
public int ConnectionLimit { get; set; } public int ConnectionLimit { get; set; }
/// <summary>
/// `Base64ApiKey` for Elastic Cloud style encoded api keys
/// </summary>
public string? Base64ApiKey { get; set; }
/// <summary>
/// `ApiKey` for simple secret token
/// </summary>
public string? ApiKey { get; set; }
/// <summary>
/// `UserName` for basic authentication
/// </summary>
public string? UserName { get; set; } public string? UserName { get; set; }
/// <summary>
/// `Password` for basic authentication
/// </summary>
public string? Password { get; set; } public string? Password { get; set; }
public TimeSpan ConnectionTimeout { get; set; } /// <summary>
public IConnection Connection { get; set; } /// Default: 60s
public ConnectionSettings.SourceSerializerFactory SerializerFactory { get; set; } /// </summary>
public TimeSpan RequestTimeout { get; set; }
/// <summary>
///
/// </summary>
public IRequestInvoker RequestInvoker { get; set; }
public SourceSerializerFactory SerializerFactory { get; set; }
public AbpElasticsearchOptions() public AbpElasticsearchOptions()
{ {
ConnectionLimit = ConnectionConfiguration.DefaultConnectionLimit; ConnectionLimit = TransportConfiguration.DefaultConnectionLimit;
ConnectionTimeout = ConnectionConfiguration.DefaultTimeout; // Default: 60s
// See: https://www.elastic.co/docs/reference/elasticsearch/clients/dotnet/_options_on_elasticsearchclientsettings
RequestTimeout = TimeSpan.FromSeconds(60);
} }
internal IConnectionSettingsValues CreateConfiguration() internal IElasticsearchClientSettings CreateClientSettings()
{ {
IConnectionPool connectionPool; NodePool nodePool;
IEnumerable<Uri> nodes = NodeUris IEnumerable<Uri> nodes = NodeUris
.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries) .Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(uriString => new Uri(uriString)); .Select(uriString => new Uri(uriString));
if (nodes.Count() == 1) if (nodes.Count() == 1)
{ {
connectionPool = new SingleNodeConnectionPool(nodes.First()); nodePool = new SingleNodePool(nodes.First());
} }
else else
{ {
connectionPool = new StaticConnectionPool(nodes); nodePool = new StaticNodePool(nodes);
} }
var configuration = new ConnectionSettings( var clientSettings = new ElasticsearchClientSettings(
connectionPool, nodePool,
Connection, RequestInvoker,
SerializerFactory) SerializerFactory)
.ConnectionLimit(ConnectionLimit) .ConnectionLimit(ConnectionLimit)
.RequestTimeout(ConnectionTimeout); .RequestTimeout(RequestTimeout);
if (!FieldCamelCase) if (!FieldCamelCase)
{ {
configuration.DefaultFieldNameInferrer((name) => name); clientSettings.DefaultFieldNameInferrer((name) => name);
} }
if (!UserName.IsNullOrWhiteSpace()) if (!Base64ApiKey.IsNullOrWhiteSpace())
{
clientSettings.Authentication(new Base64ApiKey(Base64ApiKey));
}
else if (!ApiKey.IsNullOrWhiteSpace())
{
clientSettings.Authentication(new ApiKey(ApiKey));
}
else if (!UserName.IsNullOrWhiteSpace() && !Password.IsNullOrWhiteSpace())
{ {
configuration.BasicAuthentication(UserName, Password); clientSettings.Authentication(new BasicAuthentication(UserName, Password));
} }
configuration.DisableDirectStreaming(DisableDirectStreaming); clientSettings.DisableDirectStreaming(DisableDirectStreaming);
return configuration; return clientSettings;
} }
} }
} }

16
aspnet-core/framework/elasticsearch/LINGYUN.Abp.Elasticsearch/LINGYUN/Abp/Elasticsearch/ElasticsearchClientFactory.cs

@ -1,5 +1,5 @@
using Microsoft.Extensions.Options; using Elastic.Clients.Elasticsearch;
using Nest; using Microsoft.Extensions.Options;
using System; using System;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -8,23 +8,23 @@ namespace LINGYUN.Abp.Elasticsearch
public class ElasticsearchClientFactory : IElasticsearchClientFactory, ISingletonDependency public class ElasticsearchClientFactory : IElasticsearchClientFactory, ISingletonDependency
{ {
private readonly AbpElasticsearchOptions _options; private readonly AbpElasticsearchOptions _options;
private readonly Lazy<IElasticClient> _lazyClient; private readonly Lazy<ElasticsearchClient> _lazyClient;
public ElasticsearchClientFactory( public ElasticsearchClientFactory(
IOptions<AbpElasticsearchOptions> options) IOptions<AbpElasticsearchOptions> options)
{ {
_options = options.Value; _options = options.Value;
_lazyClient = new Lazy<IElasticClient>(CreateClient); _lazyClient = new Lazy<ElasticsearchClient>(CreateClient);
} }
public IElasticClient Create() => _lazyClient.Value; public ElasticsearchClient Create() => _lazyClient.Value;
protected virtual IElasticClient CreateClient() protected virtual ElasticsearchClient CreateClient()
{ {
var configuration = _options.CreateConfiguration(); var configuration = _options.CreateClientSettings();
var client = new ElasticClient(configuration); var client = new ElasticsearchClient(configuration);
return client; return client;
} }

4
aspnet-core/framework/elasticsearch/LINGYUN.Abp.Elasticsearch/LINGYUN/Abp/Elasticsearch/IElasticsearchClientFactory.cs

@ -1,9 +1,9 @@
using Nest; using Elastic.Clients.Elasticsearch;
namespace LINGYUN.Abp.Elasticsearch namespace LINGYUN.Abp.Elasticsearch
{ {
public interface IElasticsearchClientFactory public interface IElasticsearchClientFactory
{ {
IElasticClient Create(); ElasticsearchClient Create();
} }
} }

Loading…
Cancel
Save