云商品库 增加违规记录和违规字典

This commit is contained in:
chfei committed 2026-10-30 14:22:44 +08:00
1 parent d0f9f00b76
commit 53a4887d35
14 files changed
+943 -41

No files matched your search

@@ -2,6 +2,7 @@ using System.Diagnostics;
using MAX.Core.DependencyInjection;
using MAX.Framework.Commons;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OpenSearch.Client;
using OpenSearch.Net;
@@ -14,7 +15,7 @@ namespace Store.Modules.CloudProduct.Services
public interface IOpenSearchService : ISingletonDependency
{
Task AddDocumentAsync(GoodsSearchDocument document);
Task BulkIndexGoodsAsync(IEnumerable<GoodsSearchDocument> documents);
Task<long[]> BulkIndexGoodsAsync(IEnumerable<GoodsSearchDocument> documents);
Task<bool> ClearIndexAsync();
Task<long> CountDocumentsAsync();
Task<bool> CreateIndexAsync();
@@ -24,7 +25,7 @@ namespace Store.Modules.CloudProduct.Services
Task<bool> IndexExistsAsync();
Task<bool> IsIndexMappingHealthyAsync();
Task RefreshIndexAsync();
Task<long> RebuildIndexAsync(IEnumerable<GoodsEntity> goodsList);
Task<long[]> RebuildIndexAsync(IEnumerable<GoodsEntity> goodsList);
Task<(long Total, List<GoodsSearchDocument> Documents)> SearchByTitleAsync(GoodsSearchRequest request, GoodsSearchSortBy sortBy = GoodsSearchSortBy.Relevance);
}
@@ -39,9 +40,14 @@ namespace Store.Modules.CloudProduct.Services
// 批量写入/建索引/删除索引等管理类操作超时(数据量大时需要更长,可挪到配置文件)
private static readonly TimeSpan AdminTimeout = TimeSpan.FromSeconds(30);
private readonly ILogger<OpenSearchService> _logger;
private readonly IFreeSql _database;
private readonly GoodsIndexLock _indexLock;
public OpenSearchService(IConfiguration configuration,ILogger<OpenSearchService> logger)
public OpenSearchService(IConfiguration configuration,ILogger<OpenSearchService> logger,
[FromKeyedServices("CloudProductConnection")] IFreeSql database, GoodsIndexLock indexLock)
{
_database = database;
_indexLock = indexLock;
var config = configuration.GetSection("Search");
_nodeUrl = config.GetValue<string>("Url") ?? "";
_indexName = config.GetValue<string>("IndexName") ?? "";
@@ -204,12 +210,18 @@ namespace Store.Modules.CloudProduct.Services
/// <summary>
/// ============ 3.1 批量写入商品搜索文档(存在则覆盖) ============
/// </summary>
public async Task BulkIndexGoodsAsync(IEnumerable<GoodsSearchDocument> documents)
public async Task<long[]> BulkIndexGoodsAsync(IEnumerable<GoodsSearchDocument> documents)
{
if (documents == null) throw new ArgumentNullException(nameof(documents));
var list = documents.ToList();
if (list.Count == 0) return;
if (list.Count == 0) return [];
await using var lease = await _indexLock.AcquireAsync();
var ids = list.Select(x => x.Id).Distinct().ToArray();
var blocked = (await _database.Select<GoodsViolationEntity>()
.Where(x => ids.Contains(x.GoodsId)).ToListAsync(x => x.GoodsId)).ToHashSet();
list = list.Where(x => !blocked.Contains(x.Id)).ToList();
if (list.Count == 0) return [];
// 确保索引已按 IK 分词映射创建,否则中文分词无效
await CreateIndexAsync();
@@ -220,19 +232,20 @@ namespace Store.Modules.CloudProduct.Services
.RequestConfiguration(r => r.RequestTimeout(AdminTimeout))
);
if (!bulkResponse.IsValid)
if (!bulkResponse.IsValid || bulkResponse.Errors)
{
throw new AppException("写入 OpenSearch 失败: " + bulkResponse.DebugInformation);
}
// 刷新索引,保证数据立即可搜索(仅用于演示,生产环境不建议频繁 Refresh)
await RefreshIndexAsync();
return list.Select(x => x.Id).Distinct().ToArray();
}
/// <summary>
/// ============ 3.2 根据数据库商品重建搜索索引 ============
/// </summary>
public async Task<long> RebuildIndexAsync(IEnumerable<GoodsEntity> goodsList)
public async Task<long[]> RebuildIndexAsync(IEnumerable<GoodsEntity> goodsList)
{
if (goodsList == null) throw new ArgumentNullException(nameof(goodsList));
@@ -240,6 +253,8 @@ namespace Store.Modules.CloudProduct.Services
.Where(x => x != null && x.GoodsId > 0)
.Select(x => new GoodsSearchDocument
{
Platform = x.Platform,
ThumbUrl = x.ThumbUrl,
Id = x.GoodsId,
Title = x.Title ?? string.Empty,
CatId1 = x.CatId1,
@@ -251,10 +266,7 @@ namespace Store.Modules.CloudProduct.Services
})
.ToList();
if (documents.Count == 0) return 0;
await BulkIndexGoodsAsync(documents);
return documents.Count;
return await BulkIndexGoodsAsync(documents);
}
/// <summary>
@@ -269,7 +281,8 @@ namespace Store.Modules.CloudProduct.Services
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
if (!exists.Exists)
{
return;
if (exists.ApiCall?.HttpStatusCode == 404) return;
throw new AppException("检查 OpenSearch 索引失败: " + exists.DebugInformation);
}
var response = await _client.BulkAsync(b =>
@@ -282,7 +295,9 @@ namespace Store.Modules.CloudProduct.Services
return b;
});
if (!response.IsValid)
if (response.ApiCall?.HttpStatusCode is not (>= 200 and < 300)
|| response.Items.Count != list.Count
|| response.Items.Any(x => x.Status != 404 && (x.Error != null || x.Status < 200 || x.Status >= 300)))
{
throw new AppException("删除 OpenSearch 文档失败: " + response.DebugInformation);
}
@@ -296,6 +311,9 @@ namespace Store.Modules.CloudProduct.Services
public async Task AddDocumentAsync(GoodsSearchDocument document)
{
if (document == null) throw new ArgumentNullException(nameof(document));
await using var lease = await _indexLock.AcquireAsync();
if (await _database.Select<GoodsViolationEntity>().AnyAsync(x => x.GoodsId == document.Id))
throw new AppException("违规商品不能写入搜索索引");
var response = await _client.IndexAsync(document, i => i
.Index(_indexName)
@@ -522,6 +540,7 @@ namespace Store.Modules.CloudProduct.Services
/// </summary>
public async Task<bool> ClearIndexAsync()
{
await using var lease = await _indexLock.AcquireAsync();
var existsResponse = await _client.Indices.ExistsAsync(_indexName, d => d
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout)));
if (!existsResponse.Exists)
@@ -539,7 +558,10 @@ namespace Store.Modules.CloudProduct.Services
}
Console.WriteLine($"索引 {_indexName} 已删除");
return await CreateIndexAsync();
var created = await CreateIndexAsync();
await _database.Update<GoodsEntity>().Set(x => x.IsSegmented, (byte)0)
.Where(x => x.Status == 0).ExecuteAffrowsAsync();
return created;
}
}
}