Ejemplo n.º 1
0
        /// <summary>
        /// 私有方法,构造高亮查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageParams"></param>
        /// <param name="searchDescriptor"></param>
        private void BuildHighLightQuery <T>(IPageParam pageParams, ref SearchDescriptor <T> searchDescriptor) where T : class
        {
            int keysLength = (pageParams.Highlight?.Keys?.Length).Value;

            Func <HighlightFieldDescriptor <T>, IHighlightField>[] filedDesciptor = new Func <HighlightFieldDescriptor <T>, IHighlightField> [keysLength];
            int keysIndex = 0;

            foreach (string key in pageParams.Highlight?.Keys)
            {
                filedDesciptor[keysIndex] = hf => hf.Field(key)//简介高亮
                                            .HighlightQuery(q => q
                                                            .Match(m => m
                                                                   .Field(key)
                                                                   .Query(pageParams.KeyWord)));
                keysIndex++;
            }
            //构造hightlight
            IHighlight highLight = new HighlightDescriptor <T>()
                                   .PreTags(pageParams.Highlight.PreTags)
                                   .PostTags(pageParams.Highlight.PostTags)
                                   .Fields(filedDesciptor);

            //设置高亮
            searchDescriptor = searchDescriptor.Highlight(s => highLight);
        }
Ejemplo n.º 2
0
        public IEnumerable <User> Query(UserDTO user, IPageParam pageParam)
        {
            Guard.ArgumentNotNull(user, "user");
            Guard.ArgumentNotNull(pageParam, "pageParam");
            var expression = ExpressionBuilder.True <User>();

            expression = expression.AndIf(t => t.Name.Contains(user.Name), !string.IsNullOrEmpty(user.Name));
            return(_userRepo.Query(expression, t => t.CreatedOn, true, pageParam));
        }
Ejemplo n.º 3
0
        public ApiResult <PagedData <BaseDictionaryGroup> > Index([FromBody] IPageParam m)
        {
            Expression <Func <BaseDictionaryGroup, bool> > condition = x => x.DictionaryGroupStatus > -1;
            var result = _context.BaseDictionaryGroups.Where(condition)
                         .OrderByDescending(c => c.DictionaryGroupId).Skip((m.offset) * m.limit).Take(m.limit).ToList();

            return(new ApiResult <PagedData <BaseDictionaryGroup> >(ApiCode.Success, "OK", new PagedData <BaseDictionaryGroup>
            {
                total = _context.BaseDictionaryGroups.Count(condition),
                rows = result
            }));
        }
Ejemplo n.º 4
0
        public ApiResult <PagedData <DynamicFormInfo> > Index([FromBody] IPageParam m)
        {
            Expression <Func <DynamicFormInfo, bool> > condition = x => x.FormStatus > -1;
            var result = _context
                         .DynamicFormInfos.OrderByDescending(c => c.FormId).Where(condition).Skip((m.offset) * m.limit).Take(m.limit).ToList();

            return(new ApiResult <PagedData <DynamicFormInfo> >(ApiCode.Success, "OK", new PagedData <DynamicFormInfo>
            {
                total = _context.DynamicFormInfos.Count(condition),
                rows = _context.DynamicFormInfos.OrderByDescending(c => c.FormId).Where(condition).Skip((m.offset) * m.limit).Take(m.limit).ToList()
            }));
        }
Ejemplo n.º 5
0
        public IEnumerable <Role> Query(RoleDTO role, IPageParam pageParam)
        {
            var expression = ExpressionBuilder.True <Role>();

            if (role != null)
            {
                expression = expression.AndIf(t => t.Name == role.Name, !string.IsNullOrEmpty(role.Name));
            }
            if (pageParam == null)
            {
                return(_roleRepo.Query(expression));
            }
            return(_roleRepo.Query(expression, t => t.CreatedOn, true, pageParam));
        }
Ejemplo n.º 6
0
        public IEnumerable <App> Query(AppDTO app, IPageParam pageParam)
        {
            var expression = ExpressionBuilder.True <App>();

            if (app != null)
            {
                expression = expression.AndIf(t => t.Name == app.Name, !string.IsNullOrEmpty(app.Name));
                expression = expression.AndIf(t => t.ClientID == app.ClientID, app.ClientID != 0);
            }
            if (pageParam == null)
            {
                return(_appRepo.Query(expression));
            }
            return(_appRepo.Query(expression, t => t.CreatedOn, true, pageParam));
        }
Ejemplo n.º 7
0
        /// <exception cref="ArgumentNullException"><paramref name="viewModelParams"/> or
        /// <paramref name="defaultPageSize"/> or
        /// <paramref name="sizeList"/> is <see langword="null" />.</exception>
        public PageableViewModel(IPageParam viewModelParams, string defaultPageSize, params string[] sizeList)
        {
            if (viewModelParams == null)
            {
                throw new ArgumentNullException("viewModelParams");
            }

            if (viewModelParams == null)
            {
                throw new ArgumentNullException("defaultPageSize");
            }

            if (viewModelParams == null)
            {
                throw new ArgumentNullException("sizeList");
            }

            ViewModelParams = viewModelParams;

            var pageSizeList = sizeList.OrderBy(x => x.Length).ThenBy(x => x).ToList();

            var pageSizeString = pageSizeList.Contains(viewModelParams.PageSize ?? string.Empty)
            ? viewModelParams.PageSize
            : defaultPageSize;

            _selectedPageSize = Convert.ToInt32(pageSizeString);

            var positiveIntegerNumberStyle = NumberStyles.Integer ^ NumberStyles.AllowLeadingSign;
            var parseSuccess = int.TryParse(viewModelParams.Page, positiveIntegerNumberStyle, NumberFormatInfo.InvariantInfo,
                                            out _selectedPageNumber);

            if (!parseSuccess)
            {
                _selectedPageNumber = 1;
            }

            FixPageNumberAndPageSize();

            PageSizeSelectList = pageSizeList.Select(pageSize => new SelectListItem
            {
                Text     = pageSize,
                Value    = pageSize,
                Selected = pageSize == pageSizeString
            }).ToList();
        }
        /// <summary>
        /// 构造高亮查询
        /// </summary>
        private void BuildHighLightQuery <T>(IPageParam param, ref SearchDescriptor <T> searchRequest) where T : class
        {
            var keysLength      = param.Highlight?.Keys?.Length ?? 0;
            var fieldDescriptor = new Func <HighlightFieldDescriptor <T>, IHighlightField> [keysLength];
            var keysIndex       = 0;

            foreach (var key in param.Highlight?.Keys)
            {
                fieldDescriptor[keysIndex] = hf => hf.Field(key)
                                             .HighlightQuery(q => q.Match(m => m.Field(key).Query(param.Keyword)));
                keysIndex++;
            }

            IHighlight highlight = new HighlightDescriptor <T>()
                                   .PreTags(param.Highlight.PreTags)
                                   .PostTags(param.Highlight.PostTags)
                                   .Fields(fieldDescriptor);

            searchRequest = searchRequest.Highlight(s => highlight);
        }
Ejemplo n.º 9
0
        /// <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);
        }
Ejemplo n.º 10
0
        public virtual IList <TEntity> Query <TKey>(IList <string> paths, Expression <Func <TEntity, bool> > expression,
                                                    Expression <Func <TEntity, TKey> > keySelector, bool isAscending, IPageParam pageParam)
        {
            Guard.ArgumentNotNull(pageParam, "pageParam");
            pageParam.TotalRecordCount = 0;

            DbQuery <TEntity> entities = Entities;

            if (paths != null)
            {
                paths.ToList().ForEach(p => entities = entities.Include(p));
            }

            var query = entities.AsExpandable().Where(expression);

            if (isAscending)
            {
                query = query.OrderBy(keySelector);
            }
            else
            {
                query = query.OrderByDescending(keySelector);
            }

            if (pageParam.Limit > 0) //分页查询
            {
                pageParam.Offset--;
                if (pageParam.Offset < 0)
                {
                    pageParam.Offset = 0;
                }
                pageParam.TotalRecordCount = query.Count();
                query = query.Skip(pageParam.Offset).Take(pageParam.Limit);
            }

            return(query.ToList());
        }
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T">实体类型</typeparam>
        /// <param name="param">分页参数</param>
        /// <param name="indexName">索引名</param>
        /// <returns></returns>
        public async Task <IQueryResult <T> > PageQueryAsync <T>(IPageParam param, string indexName) where T : class
        {
            if (param == null)
            {
                param = new PageParam()
                {
                    Page     = 1,
                    PageSize = 20
                };
            }

            var searchRequest = new SearchDescriptor <T>()
                                .Index(indexName)
                                .From(param.GetSkipCount())
                                .Size(param.PageSize);

            if (param is PageParamWithSearch pageSearch)
            {
                ConfigPageRequest(pageSearch, ref searchRequest);
            }
            else if (param is PageParam pageParam)
            {
                ConfigPageRequest(pageParam, ref searchRequest);
            }

            // 是否需要高亮
            bool hasHighlight = param.Highlight?.Keys?.Length > 0;

            if (hasHighlight)
            {
                BuildHighLightQuery(param, ref searchRequest);
            }

            var client = await _builder.GetClientAsync();

            var response = await client.SearchAsync <T>(x => searchRequest);

            //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);
            //    });
            //}
            return(new CustomQueryResult <T>()
            {
                Data = response.Documents,
                Took = response.Took,
                TotalCount = response.Total
            });
        }
Ejemplo n.º 12
0
 /// <exception cref="ArgumentNullException"><paramref name="viewModelParams"/> is <see langword="null" />.</exception>
 public PageableViewModel(IPageParam viewModelParams, string defaultPageSize = "10")
     : this(viewModelParams, defaultPageSize, "5", "10", "25", "50", "100")
 {
 }