All checks were successful
Build and Deploy / deploy (push) Successful in 1m17s
111 lines
3.4 KiB
C#
111 lines
3.4 KiB
C#
using Nest;
|
||
using TrackCenter.Domain.Consts;
|
||
using TrackCenter.Domain.SeedWork;
|
||
|
||
namespace TrackCenter.Repository.ES;
|
||
|
||
/// <summary>
|
||
/// es extensions
|
||
/// </summary>
|
||
public static class EsExtensions
|
||
{
|
||
/// <summary>
|
||
/// 轨迹明细实际写入月度索引 track_details-yyyy.MM;读请求走通配符,
|
||
/// 无匹配索引时返回空结果(首次部署 / 尚未 Migrate 别名)。
|
||
/// </summary>
|
||
public static SearchDescriptor<T> UseTrackDetailsIndex<T>(this SearchDescriptor<T> descriptor) where T : class
|
||
{
|
||
return descriptor
|
||
.Index($"{GlobalConst.TrackDetailsIndex}-*")
|
||
.IgnoreUnavailable()
|
||
.AllowNoIndices();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 轨迹明细按查询更新:目标索引不存在时视为无匹配文档。
|
||
/// </summary>
|
||
public static UpdateByQueryDescriptor<T> UseTrackDetailsIndex<T>(this UpdateByQueryDescriptor<T> descriptor)
|
||
where T : class
|
||
{
|
||
return descriptor
|
||
.Index($"{GlobalConst.TrackDetailsIndex}-*")
|
||
.IgnoreUnavailable()
|
||
.AllowNoIndices();
|
||
}
|
||
|
||
public static DeleteByQueryDescriptor<T> UseTrackDetailsIndex<T>(this DeleteByQueryDescriptor<T> descriptor)
|
||
where T : class
|
||
{
|
||
return descriptor
|
||
.Index($"{GlobalConst.TrackDetailsIndex}-*")
|
||
.IgnoreUnavailable()
|
||
.AllowNoIndices();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 索引/别名不存在(含 NEST 对 404 的包装错误)。
|
||
/// </summary>
|
||
public static bool IsIndexUnavailable(this IResponse response)
|
||
{
|
||
if (response.IsValid)
|
||
return false;
|
||
|
||
if (response.ApiCall?.HttpStatusCode == 404)
|
||
return true;
|
||
|
||
if (IsIndexNotFoundType(response.ServerError?.Error?.Type)
|
||
|| IsIndexNotFoundType(response.ServerError?.Error?.CausedBy?.Type))
|
||
return true;
|
||
|
||
var rootCauses = response.ServerError?.Error?.RootCause;
|
||
if (rootCauses != null && rootCauses.Any(x => IsIndexNotFoundType(x.Type)))
|
||
return true;
|
||
|
||
// NEST: Invalid NEST response built from a successful (404) low level call on POST: /track_details/_search
|
||
var debug = response.DebugInformation;
|
||
if (!string.IsNullOrEmpty(debug)
|
||
&& (debug.Contains("index_not_found_exception", StringComparison.OrdinalIgnoreCase)
|
||
|| debug.Contains("successful (404)", StringComparison.OrdinalIgnoreCase)))
|
||
return true;
|
||
|
||
return false;
|
||
}
|
||
|
||
private static bool IsIndexNotFoundType(string? type) =>
|
||
string.Equals(type, "index_not_found_exception", StringComparison.OrdinalIgnoreCase);
|
||
|
||
/// <summary>
|
||
/// return list with id
|
||
/// </summary>
|
||
public static List<T> ToIdList<T>(this ISearchResponse<T> response) where T : IdBaseIndex
|
||
{
|
||
var dtos = new List<T>();
|
||
|
||
foreach (var hit in response.Hits)
|
||
{
|
||
var dto = hit.Source;
|
||
dto.Id = hit.Id;
|
||
|
||
dtos.Add(dto);
|
||
}
|
||
|
||
return dtos;
|
||
}
|
||
|
||
public static List<T> ToIdAndVersionList<T>(this ISearchResponse<T> response) where T : IdVersionIndex
|
||
{
|
||
var dtos = new List<T>();
|
||
|
||
foreach (var hit in response.Hits)
|
||
{
|
||
var dto = hit.Source;
|
||
dto.Id = hit.Id;
|
||
dto.Version = hit.Version;
|
||
|
||
dtos.Add(dto);
|
||
}
|
||
|
||
return dtos;
|
||
}
|
||
}
|