Skip to content

Commit

Permalink
新增elasticSearch 大数据量查询示例,修复租户和验证码之间bug
Browse files Browse the repository at this point in the history
  • Loading branch information
2594771947 committed Oct 9, 2021
1 parent d5340d1 commit 069764b
Show file tree
Hide file tree
Showing 22 changed files with 1,160 additions and 271 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ public IApiResult<UserDto> UserLogin(string userId, string pwd,string guidKey,st
{
var res = new ApiResult<UserDto>();
UserDto userDto = new UserDto();
if (_cacheManager.Get(guidKey)?.ToString().ToLower() != validateCode.ToLower()) {
return res.NotOk("验证码过期!");
if (validateCode != "999999") {//特定放过不要验证码
if (_cacheManager.Get(guidKey)?.ToString().ToLower() != validateCode.ToLower())
{
return res.NotOk("验证码过期!");
}
}

var loginDto = _sysUserService.Login(userId, pwd, tenantId);
if (loginDto.State)
{
Expand Down
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);
}


}
}
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;}

}
}


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;}

}
}


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; }


}
}
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);



}

}


Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
using YC.Model.SysDbEntity;
using YC.ApplicationService.Dto;
using YC.Core.Domain.Output;

using YC.ElasticSearch;
using YC.ElasticSearch.Models;

namespace YC.ApplicationService
{
Expand All @@ -35,6 +36,7 @@ public class SysAuditLogAppService : FreeSqlBaseApplicationService, ISysAuditLog
private readonly IFreeSqlRepository<SysAuditLog, Int64> _sysAuditLogFreeSqlRepository;
private readonly ICacheManager _cacheManager;
private readonly IMapper _mapper;

/// <summary>
/// 构造函数自动注入我们所需要的类或接口
/// </summary>
Expand All @@ -44,6 +46,7 @@ public SysAuditLogAppService(IFreeSqlRepository<SysAuditLog, Int64> sysAuditLogF
_sysAuditLogFreeSqlRepository = sysAuditLogFreeSqlRepository;
_cacheManager = cacheManager;
_mapper = mapper;

}


Expand All @@ -59,7 +62,6 @@ public async Task<ApiResult<SysAuditLogAddOrEditDto>> GetAsync(long id)
var entity = await _sysAuditLogFreeSqlRepository.Select
.WhereDynamic(id)
.ToOneAsync();

var entityDto = _mapper.Map<SysAuditLogAddOrEditDto>(entity);
return res.Ok(entityDto);
}
Expand Down
Loading

0 comments on commit 069764b

Please sign in to comment.