修改 Startup 加载 和Module 加载

This commit is contained in:
chfei committed 2026-09-23 18:47:54 +08:00
1 parent c732ecb1d2
commit c7fb447034
17 files changed
+462 -383

No files matched your search

@@ -1,6 +1,8 @@
using System.Diagnostics;
using MAX.Core.DependencyInjection;
using MAX.Framework.Commons;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OpenSearch.Client;
using OpenSearch.Net;
using Store.Modules.CloudProduct.Dtos;
@@ -19,6 +21,9 @@ namespace Store.Modules.CloudProduct.Services
Task DeleteDocumentsAsync(IEnumerable<long> ids);
Task DeleteIndexAsync();
Task<OpenSearchStatusDto> GetStatusAsync();
Task<bool> IndexExistsAsync();
Task<bool> IsIndexMappingHealthyAsync();
Task RefreshIndexAsync();
Task<long> RebuildIndexAsync(IEnumerable<GoodsEntity> goodsList);
Task<(long Total, List<GoodsSearchDocument> Documents)> SearchByTitleAsync(GoodsSearchRequest request, GoodsSearchSortBy sortBy = GoodsSearchSortBy.Relevance);
}
@@ -33,13 +38,15 @@ namespace Store.Modules.CloudProduct.Services
private static readonly TimeSpan QueryTimeout = TimeSpan.FromSeconds(5);
// 批量写入/建索引/删除索引等管理类操作超时(数据量大时需要更长,可挪到配置文件)
private static readonly TimeSpan AdminTimeout = TimeSpan.FromSeconds(30);
private readonly ILogger<OpenSearchService> _logger;
public OpenSearchService(IConfiguration configuration)
public OpenSearchService(IConfiguration configuration,ILogger<OpenSearchService> logger)
{
var config = configuration.GetSection("Search");
_nodeUrl = config.GetValue<string>("Url") ?? "";
_indexName = config.GetValue<string>("IndexName") ?? "";
_client = CreateClient();
_logger = logger;
}
private IOpenSearchClient CreateClient()
@@ -100,34 +107,38 @@ namespace Store.Modules.CloudProduct.Services
}
}
/// <summary>
/// 校验索引映射是否健康:title 字段必须使用 IK 分析器
/// </summary>
private async Task<bool> IsIndexMappingHealthyAsync()
public async Task<bool> IsIndexMappingHealthyAsync()
{
try
{
var getResponse = await _client.Indices.GetAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
if (!getResponse.IsValid || !getResponse.Indices.TryGetValue(_indexName, out var indexState))
{
return false;
}
var mappingStatus = await GetIndexMappingStatusAsync();
return mappingStatus.Healthy;
}
var mapping = indexState.Mappings;
if (mapping?.Properties == null
|| !mapping.Properties.TryGetValue(new PropertyName("title"), out var property)
|| property is not ITextProperty textProperty)
{
return false;
}
return textProperty.Analyzer == "ik_max_word" && textProperty.SearchAnalyzer == "ik_smart";
}
catch
private async Task<(bool Healthy, string? IndexAnalyzer, string? SearchAnalyzer)> GetIndexMappingStatusAsync()
{
var getResponse = await _client.Indices.GetAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
if (!getResponse.IsValid)
{
return false;
throw new AppException("读取索引映射失败: " + getResponse.DebugInformation);
}
if (!getResponse.Indices.TryGetValue(_indexName, out var indexState))
{
return (false, null, null);
}
var mapping = indexState.Mappings;
if (mapping?.Properties == null
|| !mapping.Properties.TryGetValue(new PropertyName("title"), out var property)
|| property is not ITextProperty textProperty)
{
return (false, null, null);
}
var indexAnalyzer = textProperty.Analyzer;
var searchAnalyzer = textProperty.SearchAnalyzer;
var usesIk = indexAnalyzer == "ik_max_word" && searchAnalyzer == "ik_smart";
var usesStandard = indexAnalyzer == "standard" && searchAnalyzer == "standard";
return (usesIk || usesStandard, indexAnalyzer, searchAnalyzer);
}
private async Task<CreateIndexResponse> TryCreateIndexAsync(bool useIk)
@@ -173,6 +184,23 @@ namespace Store.Modules.CloudProduct.Services
}
}
public async Task<bool> IndexExistsAsync()
{
var response = await _client.Indices.ExistsAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
return response.Exists;
}
public async Task RefreshIndexAsync()
{
var response = await _client.Indices.RefreshAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
if (!response.IsValid)
{
throw new AppException("刷新索引失败: " + response.DebugInformation);
}
}
/// <summary>
/// ============ 3.1 批量写入商品搜索文档(存在则覆盖) ============
/// </summary>
@@ -198,8 +226,7 @@ namespace Store.Modules.CloudProduct.Services
}
// 刷新索引,保证数据立即可搜索(仅用于演示,生产环境不建议频繁 Refresh)
await _client.Indices.RefreshAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
await RefreshIndexAsync();
}
/// <summary>
@@ -260,8 +287,7 @@ namespace Store.Modules.CloudProduct.Services
throw new AppException("删除 OpenSearch 文档失败: " + response.DebugInformation);
}
await _client.Indices.RefreshAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout)));
await RefreshIndexAsync();
}
/// <summary>
@@ -279,8 +305,7 @@ namespace Store.Modules.CloudProduct.Services
if (response.IsValid)
{
await _client.Indices.RefreshAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
await RefreshIndexAsync();
}
else
{
@@ -414,6 +439,7 @@ namespace Store.Modules.CloudProduct.Services
/// </summary>
public async Task<OpenSearchStatusDto> GetStatusAsync()
{
var stopwatch = Stopwatch.StartNew();
var status = new OpenSearchStatusDto
{
NodeUrl = _nodeUrl,
@@ -428,28 +454,65 @@ namespace Store.Modules.CloudProduct.Services
if (!ping.IsValid)
{
status.Message = ping.DebugInformation;
_logger.LogError(ping.DebugInformation);
return status;
}
_logger.LogInformation("OpenSerach 已连接");
var clusterHealth = await _client.Cluster.HealthAsync(Indices.All, h => h
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
if (!clusterHealth.IsValid)
{
status.Message = clusterHealth.DebugInformation;
return status;
}
var exists = await _client.Indices.ExistsAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
status.IndexExists = exists.Exists;
status.ClusterName = clusterHealth.ClusterName;
status.ClusterStatus = clusterHealth.Status.ToString().ToLowerInvariant();
status.NumberOfNodes = clusterHealth.NumberOfNodes;
status.NumberOfDataNodes = clusterHealth.NumberOfDataNodes;
status.ActivePrimaryShards = clusterHealth.ActivePrimaryShards;
status.ActiveShards = clusterHealth.ActiveShards;
status.RelocatingShards = clusterHealth.RelocatingShards;
status.InitializingShards = clusterHealth.InitializingShards;
status.UnassignedShards = clusterHealth.UnassignedShards;
status.IndexExists = await IndexExistsAsync();
if (exists.Exists)
if (status.IndexExists)
{
var mappingStatus = await GetIndexMappingStatusAsync();
status.MappingHealthy = mappingStatus.Healthy;
status.IndexAnalyzer = mappingStatus.IndexAnalyzer;
status.SearchAnalyzer = mappingStatus.SearchAnalyzer;
var count = await _client.CountAsync<GoodsSearchDocument>(c => c
.Index(_indexName)
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
status.DocumentCount = count.IsValid ? count.Count : -1;
if (!count.IsValid)
{
status.Message = count.DebugInformation;
}
}
else
{
status.Message = $"索引 [{_indexName}] 不存在";
}
status.Healthy = status.IndexExists
&& status.MappingHealthy
&& status.ClusterStatus != "red"
&& status.DocumentCount >= 0;
}
catch (Exception ex)
{
status.Connected = false;
status.Healthy = false;
status.Message = ex.Message;
}
finally
{
stopwatch.Stop();
status.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
}
return status;
}