forked from yc-l/yc.boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
新增elasticSearch 大数据量查询示例,修复租户和验证码之间bug
- Loading branch information
1 parent
d5340d1
commit 069764b
Showing
22 changed files
with
1,160 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
...e/BasicAppLayer/YC.ApplicationService/ApplicationService/BookAppService/BookAppService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
| ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Autofac.Extras.DynamicProxy; | ||
using Microsoft.AspNetCore.Http; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
using YC.Model; | ||
using YC.Core; | ||
using AutoMapper; | ||
using System.Linq.Expressions; | ||
using YC.Core.Attribute; | ||
using YC.Core.Domain; | ||
using YC.Core.Autofac; | ||
using YC.Common.ShareUtils; | ||
using YC.Core.Cache; | ||
using YC.ApplicationService.DefaultConfigure; | ||
using YC.FreeSqlFrameWork; | ||
using YC.Core.DynamicApi; | ||
using YC.Core.DynamicApi.Attributes; | ||
using YC.Model.SysDbEntity; | ||
using YC.ApplicationService.Dto; | ||
using YC.Core.Domain.Output; | ||
using YC.ElasticSearch.Models; | ||
using YC.ElasticSearch; | ||
using Nest; | ||
using YC.ApplicationService.ApplicationService.BookAppService.Dto; | ||
|
||
namespace YC.ApplicationService | ||
{ | ||
/// <summary> | ||
/// 业务实现接口 | ||
/// </summary> | ||
public class BookAppService : FreeSqlBaseApplicationService, IBookAppService | ||
{ | ||
|
||
private readonly ICacheManager _cacheManager; | ||
private readonly IMapper _mapper; | ||
private IElasticSearchRepository<Book> _elasticSearchRepository; | ||
/// <summary> | ||
/// 构造函数自动注入我们所需要的类或接口 | ||
/// </summary> | ||
public BookAppService( | ||
IHttpContextAccessor httpContextAccessor, ICacheManager cacheManager, IMapper mapper, IElasticSearchRepository<Book> elasticSearchRepository) : base(httpContextAccessor, cacheManager) | ||
{ | ||
_cacheManager = cacheManager; | ||
_mapper = mapper; | ||
_elasticSearchRepository = elasticSearchRepository; | ||
} | ||
|
||
|
||
/// <summary> | ||
/// 查查默认1页10条 | ||
/// </summary> | ||
/// <returns>返回数据集合</returns> | ||
|
||
public async Task<ApiResult<List<BookAddOrEditDto>>> GetAllAsync() | ||
{ | ||
var res = new ApiResult<List<BookAddOrEditDto>>(); | ||
var data = await _elasticSearchRepository.GetAllAsync(); | ||
|
||
var entityDtoList = _mapper.Map<List<BookAddOrEditDto>>(data); | ||
return res.Ok(entityDtoList); | ||
} | ||
|
||
/// <summary> | ||
/// 分页查询,高亮处理 | ||
/// </summary> | ||
/// <param name="input"></param> | ||
/// <returns></returns> | ||
[HttpPost] | ||
public async Task<IApiResult> GetPageBookListAsync(BookPageInput<PageInputDto> input) | ||
{ | ||
Expression<Func<Book, bool>> exp = null; | ||
Func<QueryContainerDescriptor<Book>, QueryContainer> query = null; | ||
|
||
if (input.Filter != null && input.Filter?.QueryString != "") | ||
{ | ||
if (input.PublishDateRange != null) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(input.PublishDateRange[0]) && !string.IsNullOrWhiteSpace(input.PublishDateRange[1])) | ||
{ | ||
var startDate = DateTime.Parse(input.PublishDateRange[0]).ToLocalTime(); | ||
var stopDate = DateTime.Parse(input.PublishDateRange[1]).ToLocalTime(); | ||
|
||
query = q => q.DateRange(mq => | ||
mq.Field(f => f.PublishDate).GreaterThanOrEquals(startDate).LessThan(stopDate))&&( q.Term(t => t.BookName, input.Filter.QueryString) || | ||
//q.Term(t => t.Price, "23") || | ||
q.Match(mq => mq.Field(f => f.BookContent).Query(input.Filter.QueryString).Operator(Operator.And)) || | ||
q.Match(mq => mq.Field(f => f.Auther).Query(input.Filter.QueryString).Operator(Operator.And))); | ||
//全字匹配+ 分词查询 这种也可以 | ||
//query = q => q.Bool(b => | ||
// //should 必须要和must 一起用,must 中用and 操作 | ||
// b.Must(m => m.Bool(mb => mb.Should(mbs => mbs.DateRange(mq => | ||
// mq.Field(f => f.PublishDate).GreaterThanOrEquals(startDate).LessThan(stopDate)))) | ||
// , m => m.Bool(mb => mb.Should(s => s.Term(t => t.BookName, input.Filter.QueryString), s => s.Term(t => t.Price, 23) | ||
// , s => s.Match(mq => mq.Field(f => f.BookContent).Query(input.Filter.QueryString).Operator(Operator.And)) | ||
// , s => s.Match(mq => mq.Field(f => f.Auther).Query(input.Filter.QueryString).Operator(Operator.And)) | ||
// ).MinimumShouldMatch(1))) | ||
// ); | ||
} | ||
} | ||
else | ||
{ | ||
//全字匹配+ 分词查询 double 不能直接用string 丢进去查询 | ||
query = q => q.Term(t => t.BookName, input.Filter.QueryString) || | ||
//q.Term(t => t.Price, "23") || | ||
q.Match(mq => mq.Field(f => f.BookContent).Query(input.Filter.QueryString).Operator(Operator.And)) || | ||
q.Match(mq => mq.Field(f => f.Auther).Query(input.Filter.QueryString).Operator(Operator.And)); | ||
} | ||
|
||
} | ||
else | ||
{ | ||
if (input.PublishDateRange != null) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(input.PublishDateRange[0]) && !string.IsNullOrWhiteSpace(input.PublishDateRange[1])) | ||
{ | ||
var startDate = DateTime.Parse(input.PublishDateRange[0]).ToLocalTime(); | ||
var stopDate = DateTime.Parse(input.PublishDateRange[1]).ToLocalTime(); | ||
query = q => q.DateRange(mq => | ||
mq.Field(f => f.PublishDate).GreaterThanOrEquals(startDate).LessThan(stopDate)); | ||
} | ||
} | ||
} | ||
//多项高亮结果显示 | ||
Func<HighlightDescriptor<Book>, IHighlight> highlight = h => h.PreTags("<b class='key' style='color:red'>") | ||
.PostTags("</b>").Fields(f => f.Field(ff => ff.BookName), f => f.Field(ff => ff.BookContent), | ||
f => f.Field(ff => ff.Auther)); | ||
|
||
var result = await _elasticSearchRepository.GetPageByQueryAsync(query, input.CurrentPage, input.PageSize, null, highlight); | ||
List<Book> list = result.List.ToList(); | ||
long total = result.Total >= 10000 ? 10000 : result.Total;//查询总数,如果大于10000 默认显示10000。这是es深度分页需要处理,或使用searchAfter | ||
#region 高亮数据处理 | ||
if (list.Count > 0) | ||
{ | ||
list.ForEach( | ||
x => | ||
{ | ||
var tempHighlight = result.Hits.Where(t => t.Id.Contains(x.Id)).FirstOrDefault().Highlight; | ||
IReadOnlyCollection<string> bookNameHighlightList; | ||
tempHighlight.TryGetValue("bookName", out bookNameHighlightList); | ||
if (bookNameHighlightList?.Count > 0) | ||
{ | ||
x.BookName = "";//获取值不为空,那么原有的内容用新的替代 | ||
bookNameHighlightList.ToList().ForEach(v => | ||
{ | ||
x.BookName += v; | ||
}); | ||
} | ||
|
||
IReadOnlyCollection<string> bookContentHighlightList; | ||
tempHighlight.TryGetValue("bookContent", out bookContentHighlightList); | ||
if (bookContentHighlightList?.Count > 0) | ||
{ | ||
x.BookContent = "";//获取值不为空,那么原有的内容用新的替代 | ||
bookContentHighlightList.ToList().ForEach(v => | ||
{ | ||
x.BookContent += v; | ||
}); | ||
} | ||
|
||
IReadOnlyCollection<string> autherHighlightList; | ||
tempHighlight.TryGetValue("auther", out autherHighlightList); | ||
if (autherHighlightList?.Count > 0) | ||
{ | ||
x.Auther = "";//获取值不为空,那么原有的内容用新的替代 | ||
autherHighlightList.ToList().ForEach(v => | ||
{ | ||
x.Auther += v; | ||
}); | ||
} | ||
|
||
|
||
} | ||
); | ||
|
||
} | ||
#endregion | ||
//返回数据必须是明确实体,要不然可能存在json映射死循环 | ||
var data = new PageOutput<BookAddOrEditDto>() | ||
{ | ||
List = _mapper.Map<List<BookAddOrEditDto>>(list), | ||
Total = total | ||
}; | ||
|
||
return ApiResult.Ok(data); | ||
} | ||
|
||
|
||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
...cAppLayer/YC.ApplicationService/ApplicationService/BookAppService/Dto/BookAddOrEditDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
|
||
//---------------------------------------------- | ||
//简介:YC.ApplicationService Dto 数据传输对象 | ||
// | ||
// | ||
// | ||
//auther:林宣名 | ||
//---------------------------------------------- | ||
namespace YC.ApplicationService.Dto | ||
{ | ||
/// Dto | ||
public partial class BookAddOrEditDto | ||
{ | ||
public String Id {get;set;} | ||
public String BookName {get;set;} | ||
public String BookContent {get;set;} | ||
public String Auther {get;set;} | ||
public DateTime PublishDate {get;set;} | ||
public Double Price {get;set;} | ||
public DateTime CreateDate {get;set;} | ||
|
||
} | ||
} | ||
|
||
|
32 changes: 32 additions & 0 deletions
32
...tage/BasicAppLayer/YC.ApplicationService/ApplicationService/BookAppService/Dto/BookDto.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
|
||
//---------------------------------------------- | ||
//简介:YC.ApplicationService Dto 数据传输对象 | ||
// | ||
// | ||
// | ||
//auther:林宣名 | ||
//---------------------------------------------- | ||
namespace YC.ApplicationService.Dto | ||
{ | ||
/// Dto | ||
public partial class BookDto | ||
{ | ||
public string TypeName { get; set; } | ||
public String Id {get;set;} | ||
public String BookName {get;set;} | ||
public String BookContent {get;set;} | ||
public String Auther {get;set;} | ||
public DateTime PublishDate {get;set;} | ||
public Double Price {get;set;} | ||
public DateTime CreateDate {get;set;} | ||
|
||
} | ||
} | ||
|
||
|
30 changes: 30 additions & 0 deletions
30
...asicAppLayer/YC.ApplicationService/ApplicationService/BookAppService/Dto/BookPageInput.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace YC.ApplicationService.ApplicationService.BookAppService.Dto | ||
{ | ||
public class BookPageInput<T> | ||
{ | ||
/// <summary> | ||
/// 当前页标 | ||
/// </summary> | ||
public int CurrentPage { get; set; } = 1; | ||
|
||
/// <summary> | ||
/// 每页大小 | ||
/// </summary> | ||
public int PageSize { set; get; } = 50; | ||
|
||
/// <summary> | ||
/// 查询条件 | ||
/// </summary> | ||
public T Filter { get; set; } | ||
|
||
public string[] PublishDateRange { get; set; } | ||
|
||
|
||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
.../BasicAppLayer/YC.ApplicationService/ApplicationService/BookAppService/IBookAppService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
| ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using YC.ApplicationService.ApplicationService.BookAppService.Dto; | ||
using YC.ApplicationService.Dto; | ||
using YC.Core; | ||
using YC.Core.Autofac; | ||
using YC.Core.Domain; | ||
using YC.Core.DynamicApi; | ||
using YC.Model; | ||
|
||
namespace YC.ApplicationService | ||
{ | ||
|
||
/// <summary> | ||
/// 业务接口 | ||
/// </summary> | ||
public interface IBookAppService : IApplicationService, IDependencyInjectionSupport | ||
{ | ||
|
||
|
||
/// <summary> | ||
/// 获取分页数据 | ||
/// </summary> | ||
/// <returns>返回数据集合</returns> | ||
Task<IApiResult> GetPageBookListAsync(BookPageInput<PageInputDto> input); | ||
|
||
|
||
|
||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.