From 66a85a221a4ca0b1cf8fbd58fee56f161f90ca10 Mon Sep 17 00:00:00 2001 From: xiaolipro <2357729423@qq.com> Date: Thu, 12 Sep 2024 12:14:13 +0800 Subject: [PATCH] attach --- .../Repositories/TrackDetailsEsRepository.cs | 42 ++++++++++++++----- .../Repositories/WarningRecordEsRepository.cs | 18 ++++---- 2 files changed, 41 insertions(+), 19 deletions(-) diff --git a/src/SJZY.Track.Repository/Repositories/TrackDetailsEsRepository.cs b/src/SJZY.Track.Repository/Repositories/TrackDetailsEsRepository.cs index 37ad9be1..63d0818e 100644 --- a/src/SJZY.Track.Repository/Repositories/TrackDetailsEsRepository.cs +++ b/src/SJZY.Track.Repository/Repositories/TrackDetailsEsRepository.cs @@ -20,6 +20,7 @@ public class TrackDetailsEsRepository : ElasticSearchContextAbstract(s => s + .Index(GlobalConst.TrackDetailsIndex) .Source() .Size(SafelyLimit) .Query(q => q @@ -32,6 +33,7 @@ public class TrackDetailsEsRepository : ElasticSearchContextAbstract(s => s + .Index(GlobalConst.TrackDetailsIndex) .Source(s2 => s2.Includes(fd => fd .Field(f => f.IndexName) .Field(f => f.OrderId) @@ -63,6 +65,7 @@ public class TrackDetailsEsRepository : ElasticSearchContextAbstract(x => x + .Index(GlobalConst.TrackDetailsIndex) .Source(s => s.Includes(fd => fd .Field(f => f.TrackEnContent) .Field(f => f.TrackContent) @@ -113,6 +116,7 @@ public class TrackDetailsEsRepository : ElasticSearchContextAbstract AnyAsync(int orderId, string originTrackDataHash) { var r = await Context.SearchAsync(x => x + .Index(GlobalConst.TrackDetailsIndex) .Source(s => s.Includes(fd => fd.Field(f => f.TrackCode))) .Query(q => q.Term(f => f.OrderId, orderId) @@ -182,6 +186,7 @@ public class TrackDetailsEsRepository : ElasticSearchContextAbstract(); var r = await Context.SearchAsync(x => x + .Index(GlobalConst.TrackDetailsIndex) .Source(s => s.Includes(fd => fd .Field(f => f.OrderNo) .Field(f => f.TrackStatus) @@ -225,6 +230,7 @@ public class TrackDetailsEsRepository : ElasticSearchContextAbstract> GetListAsync(int orderId) { var r = await Context.SearchAsync(x => x + .Index(GlobalConst.TrackDetailsIndex) .Query(q => q.Term(t => t.OrderId, orderId) ) @@ -241,27 +247,41 @@ public class TrackDetailsEsRepository : ElasticSearchContextAbstract trackCodes) { - var search = await Context.SearchAsync(x => x - .Source(s => s.Includes(fd => fd.Field(f => f.IndexName))) - .Size(SafelyLimit) + var deleteResponse = await Context.DeleteByQueryAsync(d => d + .Index($"{GlobalConst.TrackDetailsIndex}-*") // 使用通配符匹配所有相关索引 .Query(q => q.Terms(t => t.Field(f => f.OrderId).Terms(orderIds)) && q.Terms(t => t.Field(f => f.TrackCode.Suffix("keyword")).Terms(trackCodes)) ) ); - if (!search.IsValid) + if (!deleteResponse.IsValid) { throw new InvalidOperationException( - $"轨迹查询失败,orderIds:[{orderIds.JoinAsString(", ")}],trackCodes:[{trackCodes.JoinAsString(", ")}],原因:{search.DebugInformation}。"); + $"删除轨迹失败,orderIds:[{string.Join(", ", orderIds)}],trackCodes:[{string.Join(", ", trackCodes)}],原因:{deleteResponse.DebugInformation}。"); } - var tracks = search.ToIdList(); - - foreach (var track in tracks) - { - await DeleteTrackByIdAsync(track.Id, track.IndexName); - } + // var search = await Context.SearchAsync(x => x + // .Source(s => s.Includes(fd => fd.Field(f => f.IndexName))) + // .Size(SafelyLimit) + // .Query(q => + // q.Terms(t => t.Field(f => f.OrderId).Terms(orderIds)) + // && q.Terms(t => t.Field(f => f.TrackCode.Suffix("keyword")).Terms(trackCodes)) + // ) + // ); + // + // if (!search.IsValid) + // { + // throw new InvalidOperationException( + // $"轨迹查询失败,orderIds:[{orderIds.JoinAsString(", ")}],trackCodes:[{trackCodes.JoinAsString(", ")}],原因:{search.DebugInformation}。"); + // } + // + // var tracks = search.ToIdList(); + // + // foreach (var track in tracks) + // { + // await DeleteTrackByIdAsync(track.Id, track.IndexName); + // } } public async Task DeleteTrackByIdAsync(string id, string indexName) diff --git a/src/SJZY.Track.Repository/Repositories/WarningRecordEsRepository.cs b/src/SJZY.Track.Repository/Repositories/WarningRecordEsRepository.cs index e898ea3f..0bc3d029 100644 --- a/src/SJZY.Track.Repository/Repositories/WarningRecordEsRepository.cs +++ b/src/SJZY.Track.Repository/Repositories/WarningRecordEsRepository.cs @@ -48,14 +48,15 @@ public class WarningRecordEsRepository : ElasticSearchContextAbstract(u => - u.Query(q => - q.Term(f => f.RuleId, rule.ruleId) - && q.Term(f => f.OrderId, rule.orderId) - ) - .Script(s => s.Source("ctx._source.warningStatus = params.warningStatus") - .Params(p => p.Add("warningStatus", WarningStatus.Processed)) - ) + var r = await Context.UpdateByQueryAsync(u => u + .Index($"{GlobalConst.TrackWarningRecordIndex}-*") + .Query(q => + q.Term(f => f.RuleId, rule.ruleId) + && q.Term(f => f.OrderId, rule.orderId) + ) + .Script(s => s.Source("ctx._source.warningStatus = params.warningStatus") + .Params(p => p.Add("warningStatus", WarningStatus.Processed)) + ) ); if (!r.IsValid) @@ -94,6 +95,7 @@ public class WarningRecordEsRepository : ElasticSearchContextAbstract(x => x + .Index(GlobalConst.TrackWarningRecordIndex) .Source() .Sort(s => s.Field(f => f.CreateTime, SortOrder.Descending)) .From((input.PageIndex - 1) * input.PageSize)