Browse Source

fix(elasticseatch): Use the `ElasticsearchClient` client

pull/1416/head
colin 4 weeks 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 Nest;
using Elastic.Clients.Elasticsearch;
using Elastic.Transport;
using System;
using System.Collections.Generic;
using System.Linq;
using static Elastic.Clients.Elasticsearch.ElasticsearchClientSettings;
namespace LINGYUN.Abp.Elasticsearch
{
@ -15,56 +16,90 @@ namespace LINGYUN.Abp.Elasticsearch
/// 默认:false
/// </summary>
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 string NodeUris { 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; }
/// <summary>
/// `Password` for basic authentication
/// </summary>
public string? Password { get; set; }
public TimeSpan ConnectionTimeout { get; set; }
public IConnection Connection { get; set; }
public ConnectionSettings.SourceSerializerFactory SerializerFactory { get; set; }
/// <summary>
/// Default: 60s
/// </summary>
public TimeSpan RequestTimeout { get; set; }
/// <summary>
///
/// </summary>
public IRequestInvoker RequestInvoker { get; set; }
public SourceSerializerFactory SerializerFactory { get; set; }
public AbpElasticsearchOptions()
{
ConnectionLimit = ConnectionConfiguration.DefaultConnectionLimit;
ConnectionTimeout = ConnectionConfiguration.DefaultTimeout;
ConnectionLimit = TransportConfiguration.DefaultConnectionLimit;
// 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
.Split(new[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(uriString => new Uri(uriString));
if (nodes.Count() == 1)
{
connectionPool = new SingleNodeConnectionPool(nodes.First());
nodePool = new SingleNodePool(nodes.First());
}
else
{
connectionPool = new StaticConnectionPool(nodes);
nodePool = new StaticNodePool(nodes);
}
var configuration = new ConnectionSettings(
connectionPool,
Connection,
var clientSettings = new ElasticsearchClientSettings(
nodePool,
RequestInvoker,
SerializerFactory)
.ConnectionLimit(ConnectionLimit)
.RequestTimeout(ConnectionTimeout);
.RequestTimeout(RequestTimeout);
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 Nest;
using Elastic.Clients.Elasticsearch;
using Microsoft.Extensions.Options;
using System;
using Volo.Abp.DependencyInjection;
@ -8,23 +8,23 @@ namespace LINGYUN.Abp.Elasticsearch
public class ElasticsearchClientFactory : IElasticsearchClientFactory, ISingletonDependency
{
private readonly AbpElasticsearchOptions _options;
private readonly Lazy<IElasticClient> _lazyClient;
private readonly Lazy<ElasticsearchClient> _lazyClient;
public ElasticsearchClientFactory(
IOptions<AbpElasticsearchOptions> options)
{
_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;
}

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
{
public interface IElasticsearchClientFactory
{
IElasticClient Create();
ElasticsearchClient Create();
}
}

Loading…
Cancel
Save