private static void Main(string[] args) { #region 初始化 var paths = new List <string> { "Config/moduleConfig.json", "Config/commonConfig.json" }; BootstrapWarpper.InitiateConfig(paths); #endregion 初始化 //初始化service var service = BootstrapWarpper.IocManager.Resolve <IContentService>(); //初始化预置数据 Contents = InitData(); //赋值 //service.BlukIndex(Contents, "content_test"); //复杂查询 var request = new RequestContentDto { PageSize = 5, SearchKey = "赛乐" }; #region 高亮 var highlightConfig = new HighlightConfig <Content> { Tag = "i", HighlightConfigExpression = new List <Expression <Func <Content, object> > > { x => x.Title } }; request.HighlightConfigEntity = highlightConfig; var data = service.Search(request.CurrentIndex, request.PageSize, request); //删除 service.DeleteByQuery("123852"); //更新 service.UpdateByKey("123754", new Content { Key = "123" }); #endregion 高亮 }
/// <summary> /// Search /// </summary> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="requestContentDto"></param> public ContentResponse Search(int pageIndex, int pageSize, RequestContentDto requestContentDto) { var elasticsearchPage = new ElasticsearchPage <Content>("content_test") { PageIndex = pageIndex, PageSize = pageSize }; #region terms 分组 var terms = new List <IFieldTerms>(); var classificationGroupBy = "searchKey_classification"; var brandGroupBy = "searchKey_brand"; #endregion var searchRequest = elasticsearchPage.InitSearchRequest(); var predicateList = new List <IPredicate>(); //分类ID if (requestContentDto.CategoryId != null) { predicateList.Add(Predicates.Field <Content>(x => x.ClassificationCode, ExpressOperator.Like, requestContentDto.CategoryId)); } else { terms.Add(Predicates.FieldTerms <Content>(x => x.ClassificationGroupBy, classificationGroupBy, 200)); } //品牌 if (string.IsNullOrWhiteSpace(requestContentDto.Brand)) { terms.Add(Predicates.FieldTerms <Content>(x => x.BrandGroupBy, brandGroupBy, 200)); } //供应商名称 if (!string.IsNullOrWhiteSpace(requestContentDto.BaseType)) { predicateList.Add(Predicates.Field <Content>(x => x.BaseType, ExpressOperator.Like, requestContentDto.BaseType)); } //是否自营 if (requestContentDto.IsSelfSupport == 1) { predicateList.Add(Predicates.Field <Content>(x => x.IsSelfSupport, ExpressOperator.Eq, requestContentDto.IsSelfSupport)); } //最大价格 if (requestContentDto.MaxPrice != null) { predicateList.Add(Predicates.Field <Content>(x => x.UnitPrice, ExpressOperator.Le, requestContentDto.MaxPrice)); } //最小价格 if (requestContentDto.MinPrice != null) { predicateList.Add(Predicates.Field <Content>(x => x.UnitPrice, ExpressOperator.Ge, requestContentDto.MinPrice)); } //关键词 if (!string.IsNullOrWhiteSpace(requestContentDto.SearchKey)) { predicateList.Add(Predicates.Field <Content>(x => x.Title, ExpressOperator.Like, requestContentDto.SearchKey)); } //规整排序 var sortConfig = SortOrderRule(requestContentDto.SortKey); var sorts = new List <ISort> { Predicates.Sort <Content>(sortConfig.Key, sortConfig.SortOrder) }; var predicate = Predicates.Group(GroupOperator.And, predicateList.ToArray()); //构建或查询 var predicateListOr = new List <IPredicate>(); if (!string.IsNullOrWhiteSpace(requestContentDto.Brand)) { var array = requestContentDto.Brand.Split(',').ToList(); predicateListOr .AddRange(array.Select (item => Predicates.Field <Content>(x => x.Brand, ExpressOperator.Like, item))); } var predicateOr = Predicates.Group(GroupOperator.Or, predicateListOr.ToArray()); var predicatecCombination = new List <IPredicate> { predicate, predicateOr }; var pgCombination = Predicates.Group(GroupOperator.And, predicatecCombination.ToArray()); searchRequest.InitQueryContainer(pgCombination) .InitSort(sorts) .InitHighlight(requestContentDto.HighlightConfigEntity) .InitGroupBy(terms); var data = _searchProvider.SearchPage(searchRequest); #region terms 分组赋值 var classificationResponses = requestContentDto.CategoryId != null ? null : data.Aggregations.Terms(classificationGroupBy).Buckets .Select(x => new ClassificationResponse { Key = x.Key.ToString(), DocCount = x.DocCount }).ToList(); var brandResponses = !string.IsNullOrWhiteSpace(requestContentDto.Brand) ? null : data.Aggregations.Terms(brandGroupBy).Buckets .Select(x => new BrandResponse { Key = x.Key.ToString(), DocCount = x.DocCount }).ToList(); #endregion //初始化 #region 高亮 var titlePropertySearchName = (PropertySearchNameAttribute) LoadAttributeHelper.LoadAttributeByType <Content, PropertySearchNameAttribute>(x => x.Title); var list = data.Hits.Select(c => new Content { Key = c.Source.Key, Title = (string)c.Highlights.Highlight(c.Source.Title, titlePropertySearchName.Name), ImgUrl = c.Source.ImgUrl, BaseType = c.Source.BaseType, BelongMemberName = c.Source.BelongMemberName, Brand = c.Source.Brand, Code = c.Source.Code, BrandFirstLetters = c.Source.BrandFirstLetters, ClassificationName = c.Source.ClassificationName, ResourceStatus = c.Source.ResourceStatus, BrandGroupBy = c.Source.BrandGroupBy, ClassificationGroupBy = c.Source.ClassificationGroupBy, ClassificationCode = c.Source.ClassificationCode, IsSelfSupport = c.Source.IsSelfSupport, UnitPrice = c.Source.UnitPrice }).ToList(); #endregion var contentResponse = new ContentResponse { Records = (int)data.Total, PageIndex = elasticsearchPage.PageIndex, PageSize = elasticsearchPage.PageSize, Contents = list, BrandResponses = brandResponses, ClassificationResponses = classificationResponses }; return(contentResponse); }