public void Test1() { var keys = new string[] { "address", "name" }; int page = 1; int size = 20; //查询参数构造 IPageParam pageParams = new PageParamWithSearch { PageIndex = page, PageSize = size, KeyWord = "重庆", Operator = Nest.Operator.Or, //拼接条件 SearchKeys = keys, Highlight = new HighlightParam { Keys = keys, PostTags = "</strong>", PreTags = "<strong>", PrefixOfKey = "h_"//替换字段前缀 } }; //返回查询结果 var select = dotnetsearch.Query <BuildingBaseInfo>(pageParams); var list = select.List; Assert.True(list.Count() > 0, "构建查询成功"); }
/// <summary> /// 配置指定字段的分页请求 /// </summary> private void ConfigPageRequest <T>(PageParamWithSearch param, ref SearchDescriptor <T> searchRequest) where T : class { searchRequest = searchRequest.Query(q => q.QueryString(qs => qs.Fields(param.SearchKeys) .Query(param.Keyword) .DefaultOperator(param.Operator))); }
/// <summary> /// 搜索 /// </summary> /// <returns></returns> public IActionResult Search(string name) { var keys = new string[] { "address", "name" }; int page = 1; int size = 20; //查询参数构造 IPageParam pageParams = new PageParamWithSearch { PageIndex = page, PageSize = size, KeyWord = name, Operator = Nest.Operator.Or, //拼接条件 SearchKeys = keys, Highlight = new HighlightParam { Keys = keys, PostTags = "</h1>", PreTags = "<h1>", // PrefixOfKey = "h_"//替换字段前缀 } }; //返回查询结果 var select = dotnetsearch.Query <BuildingBaseInfo>(pageParams); var list = select.List.Where(u => !u.IsDeleted).ToList(); list.ForEach(y => { if (y.Summary != null && y?.Summary?.Length > 30) { y.Summary = y.Summary.Substring(0, 30) + "......"; } }); ViewData["data"] = list; //var ids = select.List.Select(x=>x.Id).ToList(); //在mysql 查询出来 //var list = _dbContext.buildingBaseInfos.Where(h => ids.Contains(h.Id)).ToList(); //var list = _dbContext.buildingBaseInfos.FirstOrDefault(); //var frist = new List<BuildingBaseInfo> { list }; // dotnetsearch.Query<BuildingBaseInfo>(); return(View("BuildShopView")); }
public ElasticPager <Blog> QueryByPage(int?pageIndex = 1, int pageSize = 20, string keyWord = null) { var keys = new string[] { "title", "summary" }; int page = pageIndex.HasValue ? pageIndex.Value : 1; //查询参数构造 IPageParam pageParams = new PageParamWithSearch { PageIndex = page, PageSize = pageSize, KeyWord = keyWord, Operator = Nest.Operator.Or, SearchKeys = keys, Highlight = new HighlightParam { Keys = keys, PostTags = "</strong>", PreTags = "<strong>", PrefixOfKey = "h_"//替换字段前缀 } }; //返回查询结果 IQueryResult <Blog> result = search.Query <Blog>(pageParams); //返回客户端想要的结果 return(new ElasticPager <Blog> { KeyWord = keyWord, Took = result.Took, List = result.List, PageData = new Entities.Page.PageParam { PageIndex = page, PageSize = pageSize, TotalCount = result.Total } }); }
/// <summary> /// 方法有点长,需要重构 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="pageParams"></param> /// <param name="index"></param> /// <returns></returns> public IQueryResult <T> Query <T>(IPageParam pageParams, string index = null) where T : class { if (pageParams == null) { pageParams = new PageParam { PageIndex = 1, PageSize = 20 }; } SearchDescriptor <T> searchDescriptor = new SearchDescriptor <T>() .Type(typeof(T).SearchName()) .Index(index ?? _defaultIndex) .From(pageParams.From) .Size(pageParams.PageSize); if (pageParams is PageParamWithSearch) { PageParamWithSearch pageParamsSearch = pageParams as PageParamWithSearch; searchDescriptor = searchDescriptor.Query(q => q.QueryString(qs => qs.Fields(pageParamsSearch.SearchKeys) .Query(pageParamsSearch.KeyWord) .DefaultOperator(pageParamsSearch.Operator))); } else if (pageParams is PageParam) { searchDescriptor = searchDescriptor.Query(q => q.QueryString(qs => qs.Query(pageParams.KeyWord) .DefaultOperator(pageParams.Operator))); } //是否需要高亮 bool hasHighlight = pageParams.Highlight?.Keys?.Length > 0; if (hasHighlight) { //TODO BuildHighLightQuery <T>(pageParams, ref searchDescriptor); } //所有条件配置完成之后执行查询 ISearchResponse <T> response = _builder?.Client.Search <T>(s => searchDescriptor); var list = response.Documents; if (hasHighlight) { var listWithHightlight = new List <T>(); response.Hits.ToList().ForEach(x => { if (x.Highlights?.Count > 0) { PropertyInfo[] properties = typeof(T).GetProperties(); foreach (string key in pageParams.Highlight?.Keys) { //先得到要替换的内容 if (x.Highlights.ContainsKey(key)) { string value = string.Join("", x.Highlights[key]?.Highlights); PropertyInfo info = properties.FirstOrDefault(p => p.Name == pageParams.Highlight.PrefixOfKey + key); //没找到带前缀的属性,则替换之前的 if (info == null && pageParams.Highlight.ReplaceAuto) { info = properties.FirstOrDefault(p => p.Name == key); } if (info?.CanWrite == true) { if (!string.IsNullOrEmpty(value)) { //如果高亮字段不为空,才赋值,否则就赋值成空 info.SetValue(x.Source, value); } } } } } listWithHightlight.Add(x.Source); }); } IQueryResult <T> result = new CustomQueryResult <T> { List = list, Took = response.Took, Total = response.Total }; return(result); }