修复用户收藏查询bug
This commit is contained in:
1 parent
08757d15e4
commit
f1fa0401c2
1 file changed
+72
-72
@@ -330,80 +330,80 @@ namespace Store.Modules.CloudProduct.Services
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
searchResponse = await _client.SearchAsync<UserFavoritesSearchDocument>(s => s
|
searchResponse = await _client.SearchAsync<UserFavoritesSearchDocument>(s => s
|
||||||
.Index(_indexName)
|
.Index(_indexName)
|
||||||
.Query(q => q
|
.Query(q => q
|
||||||
.Bool(b =>
|
.Bool(b =>
|
||||||
{
|
|
||||||
// 只检索当前用户自己的收藏
|
|
||||||
b.Filter(f => f.Term(t => t.Field(x => x.UserName).Value(request.UserName)));
|
|
||||||
|
|
||||||
var filters = new List<Func<QueryContainerDescriptor<UserFavoritesSearchDocument>, QueryContainer>>();
|
|
||||||
|
|
||||||
if (groupId.HasValue)
|
|
||||||
{
|
|
||||||
filters.Add(f => f.Term(t => t.Field(x => x.GroupId).Value(groupId.Value)));
|
|
||||||
}
|
|
||||||
else if (string.IsNullOrWhiteSpace(groupName) == false)
|
|
||||||
{
|
|
||||||
filters.Add(f => f.Term(t => t.Field(x => x.GroupName).Value(groupName)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.Platform.HasValue && request.Platform.Value > 0)
|
|
||||||
{
|
|
||||||
filters.Add(f => f.Term(t => t.Field(x => x.Platform).Value(request.Platform.Value)));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.CatId1.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId1).Value(request.CatId1.Value)));
|
|
||||||
if (request.CatId2.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId2).Value(request.CatId2.Value)));
|
|
||||||
if (request.CatId3.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId3).Value(request.CatId3.Value)));
|
|
||||||
if (request.CatId4.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId4).Value(request.CatId4.Value)));
|
|
||||||
|
|
||||||
if (request.MinPrice.HasValue || request.MaxPrice.HasValue)
|
|
||||||
{
|
|
||||||
filters.Add(f => f.Range(r =>
|
|
||||||
{
|
|
||||||
var range = r.Field(x => x.MinPrice);
|
|
||||||
if (request.MinPrice.HasValue) range = range.GreaterThanOrEquals(request.MinPrice.Value);
|
|
||||||
if (request.MaxPrice.HasValue) range = range.LessThanOrEquals(request.MaxPrice.Value);
|
|
||||||
return range;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.MinSoldQuantity.HasValue || request.MaxSoldQuantity.HasValue)
|
|
||||||
{
|
|
||||||
filters.Add(f => f.Range(r =>
|
|
||||||
{
|
|
||||||
var range = r.Field(x => x.SoldQuantity);
|
|
||||||
if (request.MinSoldQuantity.HasValue) range = range.GreaterThanOrEquals(request.MinSoldQuantity.Value);
|
|
||||||
if (request.MaxSoldQuantity.HasValue) range = range.LessThanOrEquals(request.MaxSoldQuantity.Value);
|
|
||||||
return range;
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (filters.Count > 0) b.Filter(filters.ToArray());
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(keyword) == false)
|
|
||||||
{
|
|
||||||
b.Must(m => m.Match(mm => mm.Field(f => f.Title).Query(keyword)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return b;
|
|
||||||
})
|
|
||||||
)
|
|
||||||
.Sort(sort => sortBy switch
|
|
||||||
{
|
{
|
||||||
UserFavoritesSortBy.CreatedTimeAsc => sort.Ascending(f => f.CreatedAt),
|
var filters = new List<Func<QueryContainerDescriptor<UserFavoritesSearchDocument>, QueryContainer>>();
|
||||||
UserFavoritesSortBy.PriceAsc => sort.Ascending(f => f.MinPrice),
|
|
||||||
UserFavoritesSortBy.PriceDesc => sort.Descending(f => f.MinPrice),
|
// 只检索当前用户自己的收藏 —— 放进 filters,避免被后面的 Filter 覆盖
|
||||||
UserFavoritesSortBy.SoldDesc => sort.Descending(f => f.SoldQuantity),
|
filters.Add(f => f.Term(t => t.Field(x => x.UserName).Value(request.UserName)));
|
||||||
_ => sort.Descending(f => f.CreatedAt)
|
|
||||||
|
if (groupId.HasValue)
|
||||||
|
{
|
||||||
|
filters.Add(f => f.Term(t => t.Field(x => x.GroupId).Value(groupId.Value)));
|
||||||
|
}
|
||||||
|
else if (string.IsNullOrWhiteSpace(groupName) == false)
|
||||||
|
{
|
||||||
|
filters.Add(f => f.Term(t => t.Field(x => x.GroupName).Value(groupName)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.Platform.HasValue && request.Platform.Value > 0)
|
||||||
|
{
|
||||||
|
filters.Add(f => f.Term(t => t.Field(x => x.Platform).Value(request.Platform.Value)));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.CatId1.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId1).Value(request.CatId1.Value)));
|
||||||
|
if (request.CatId2.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId2).Value(request.CatId2.Value)));
|
||||||
|
if (request.CatId3.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId3).Value(request.CatId3.Value)));
|
||||||
|
if (request.CatId4.HasValue) filters.Add(f => f.Term(t => t.Field(x => x.CatId4).Value(request.CatId4.Value)));
|
||||||
|
|
||||||
|
if (request.MinPrice.HasValue || request.MaxPrice.HasValue)
|
||||||
|
{
|
||||||
|
filters.Add(f => f.Range(r =>
|
||||||
|
{
|
||||||
|
var range = r.Field(x => x.MinPrice);
|
||||||
|
if (request.MinPrice.HasValue) range = range.GreaterThanOrEquals(request.MinPrice.Value);
|
||||||
|
if (request.MaxPrice.HasValue) range = range.LessThanOrEquals(request.MaxPrice.Value);
|
||||||
|
return range;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (request.MinSoldQuantity.HasValue || request.MaxSoldQuantity.HasValue)
|
||||||
|
{
|
||||||
|
filters.Add(f => f.Range(r =>
|
||||||
|
{
|
||||||
|
var range = r.Field(x => x.SoldQuantity);
|
||||||
|
if (request.MinSoldQuantity.HasValue) range = range.GreaterThanOrEquals(request.MinSoldQuantity.Value);
|
||||||
|
if (request.MaxSoldQuantity.HasValue) range = range.LessThanOrEquals(request.MaxSoldQuantity.Value);
|
||||||
|
return range;
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
// 统一一次性应用所有 filter,避免覆盖
|
||||||
|
b.Filter(filters.ToArray());
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(keyword) == false)
|
||||||
|
{
|
||||||
|
b.Must(m => m.Match(mm => mm.Field(f => f.Title).Query(keyword)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return b;
|
||||||
})
|
})
|
||||||
.From(from)
|
)
|
||||||
.Size(pageSize)
|
.Sort(sort => sortBy switch
|
||||||
.TrackTotalHits(true) // 命中数超过 10000 时默认不精确统计,需要显式开启
|
{
|
||||||
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout))
|
UserFavoritesSortBy.CreatedTimeAsc => sort.Ascending(f => f.CreatedAt),
|
||||||
);
|
UserFavoritesSortBy.PriceAsc => sort.Ascending(f => f.MinPrice),
|
||||||
}
|
UserFavoritesSortBy.PriceDesc => sort.Descending(f => f.MinPrice),
|
||||||
|
UserFavoritesSortBy.SoldDesc => sort.Descending(f => f.SoldQuantity),
|
||||||
|
_ => sort.Descending(f => f.CreatedAt)
|
||||||
|
})
|
||||||
|
.From(from)
|
||||||
|
.Size(pageSize)
|
||||||
|
.TrackTotalHits(true)
|
||||||
|
.RequestConfiguration(r => r.RequestTimeout(QueryTimeout))
|
||||||
|
);}
|
||||||
catch (OpenSearchClientException ex) when (ex.Response?.Success == false)
|
catch (OpenSearchClientException ex) when (ex.Response?.Success == false)
|
||||||
{
|
{
|
||||||
throw new AppException($"收藏检索超时或失败({QueryTimeout.TotalSeconds}秒): " + ex.Message);
|
throw new AppException($"收藏检索超时或失败({QueryTimeout.TotalSeconds}秒): " + ex.Message);
|
||||||
|
|||||||
Reference in new issue
Block a user