Example #1
0
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(BlogFullTextQuery blogQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            //范围
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            //如果查的是相关日志
            if (blogQuery.IsRelationBlog)
            {
                fieldNameAndBoosts.Add(BlogIndexDocument.Subject, BoostLevel.Hight);
                fieldNameAndBoosts.Add(BlogIndexDocument.Tag, BoostLevel.Medium);
                fieldNameAndBoosts.Add(BlogIndexDocument.OwnerCategoryName, BoostLevel.Low);
                searchBuilder.WithPhrases(fieldNameAndBoosts, blogQuery.Keywords, BooleanClause.Occur.SHOULD, false);
                searchBuilder.WithField(BlogIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Public).ToString(), true, BoostLevel.Hight, true);
                searchBuilder.NotWithField(BlogIndexDocument.ThreadId.ToString(), blogQuery.CurrentThreadId.ToString());
            }
            else
            {
                switch (blogQuery.BlogRange)
                {
                case BlogRange.MYBlOG:
                    searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.LoginId.ToString(), true, BoostLevel.Hight, false);
                    break;

                case BlogRange.SOMEONEBLOG:
                    searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.UserId.ToString(), true, BoostLevel.Hight, false);
                    break;

                case BlogRange.SITECATEGORY:
                    if (blogQuery.LoginId != 0)
                    {
                        searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.LoginId.ToString(), true, BoostLevel.Hight, false);
                    }
                    if (blogQuery.UserId != 0)
                    {
                        searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.UserId.ToString(), true, BoostLevel.Hight, false);
                    }
                    if (blogQuery.SiteCategoryId != 0)
                    {
                        searchBuilder.WithField(BlogIndexDocument.SiteCategoryId, blogQuery.SiteCategoryId.ToString(), true, BoostLevel.Hight, false);
                    }
                    break;

                default:
                    break;
                }
                if (!string.IsNullOrEmpty(blogQuery.Keyword))
                {
                    switch (blogQuery.Range)
                    {
                    case BlogSearchRange.SUBJECT:
                        searchBuilder.WithPhrase(BlogIndexDocument.Subject, blogQuery.Keyword, BoostLevel.Hight, false);
                        break;

                    case BlogSearchRange.BODY:
                        searchBuilder.WithPhrase(BlogIndexDocument.Body, blogQuery.Keyword, BoostLevel.Hight, false);
                        break;

                    case BlogSearchRange.AUTHOR:
                        searchBuilder.WithPhrase(BlogIndexDocument.Author, blogQuery.Keyword, BoostLevel.Hight, false);
                        break;

                    case BlogSearchRange.TAG:
                        searchBuilder.WithPhrase(BlogIndexDocument.Tag, blogQuery.Keyword, BoostLevel.Hight, false);
                        break;

                    case BlogSearchRange.OWNERCATEGORYNAME:
                        searchBuilder.WithPhrase(BlogIndexDocument.OwnerCategoryName, blogQuery.Keyword, BoostLevel.Hight, false);
                        break;

                    default:
                        fieldNameAndBoosts.Add(BlogIndexDocument.Subject, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(BlogIndexDocument.Keywords, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(BlogIndexDocument.Tag, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(BlogIndexDocument.Summary, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(BlogIndexDocument.Body, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(BlogIndexDocument.OwnerCategoryName, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(BlogIndexDocument.Author, BoostLevel.Low);
                        searchBuilder.WithPhrases(fieldNameAndBoosts, blogQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                        break;
                    }
                }

                //某个站点分类
                if (blogQuery.SiteCategoryId != 0)
                {
                    searchBuilder.WithField(BlogIndexDocument.SiteCategoryId, blogQuery.SiteCategoryId.ToString(), true, BoostLevel.Hight, true);
                }

                if (!(UserContext.CurrentUser != null && UserContext.CurrentUser.UserId == blogQuery.LoginId && blogQuery.AllId != 0))
                {
                    searchBuilder.NotWithField(BlogIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Private).ToString());
                }
            }

            //筛选
            //全部、某人的日志
            if (blogQuery.AllId != 0)
            {
                if (blogQuery.LoginId != 0)
                {
                    searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.LoginId.ToString(), true, BoostLevel.Hight, true);
                }
                else if (blogQuery.UserId != 0)
                {
                    searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.UserId.ToString(), true, BoostLevel.Hight, true);
                }
            }

            //过滤可以显示的日志
            switch (publiclyAuditStatus)
            {
            case PubliclyAuditStatus.Again:
            case PubliclyAuditStatus.Fail:
            case PubliclyAuditStatus.Pending:
            case PubliclyAuditStatus.Success:
                searchBuilder.WithField(BlogIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                break;

            case PubliclyAuditStatus.Again_GreaterThanOrEqual:
            case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                searchBuilder.WithinRange(BlogIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                break;
            }

            return(searchBuilder);
        }
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(BlogFullTextQuery blogQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            //范围
            Dictionary<string, BoostLevel> fieldNameAndBoosts = new Dictionary<string, BoostLevel>();

            //如果查的是相关日志
            if (blogQuery.IsRelationBlog)
            {
                fieldNameAndBoosts.Add(BlogIndexDocument.Subject, BoostLevel.Hight);
                fieldNameAndBoosts.Add(BlogIndexDocument.Tag, BoostLevel.Medium);
                fieldNameAndBoosts.Add(BlogIndexDocument.OwnerCategoryName, BoostLevel.Low);
                searchBuilder.WithPhrases(fieldNameAndBoosts, blogQuery.Keywords, BooleanClause.Occur.SHOULD, false);
                searchBuilder.WithField(BlogIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Public).ToString(), true, BoostLevel.Hight, true);
                searchBuilder.NotWithField(BlogIndexDocument.ThreadId.ToString(), blogQuery.CurrentThreadId.ToString());
            }
            else
            {
                switch (blogQuery.BlogRange)
                {
                    case BlogRange.MYBlOG:
                        searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.LoginId.ToString(), true, BoostLevel.Hight, false);
                        break;
                    case BlogRange.SOMEONEBLOG:
                        searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.UserId.ToString(), true, BoostLevel.Hight, false);
                        break;
                    case BlogRange.SITECATEGORY:
                        if (blogQuery.LoginId != 0)
                            searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.LoginId.ToString(), true, BoostLevel.Hight, false);
                        if (blogQuery.UserId != 0)
                            searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.UserId.ToString(), true, BoostLevel.Hight, false);
                        if (blogQuery.SiteCategoryId != 0)
                            searchBuilder.WithField(BlogIndexDocument.SiteCategoryId, blogQuery.SiteCategoryId.ToString(), true, BoostLevel.Hight, false);
                        break;
                    default:
                        break;
                }
                if (!string.IsNullOrEmpty(blogQuery.Keyword))
                {
                    switch (blogQuery.Range)
                    {
                        case BlogSearchRange.SUBJECT:
                            searchBuilder.WithPhrase(BlogIndexDocument.Subject, blogQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case BlogSearchRange.BODY:
                            searchBuilder.WithPhrase(BlogIndexDocument.Body, blogQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case BlogSearchRange.AUTHOR:
                            searchBuilder.WithPhrase(BlogIndexDocument.Author, blogQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case BlogSearchRange.TAG:
                            searchBuilder.WithPhrase(BlogIndexDocument.Tag, blogQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case BlogSearchRange.OWNERCATEGORYNAME:
                            searchBuilder.WithPhrase(BlogIndexDocument.OwnerCategoryName, blogQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        default:
                            fieldNameAndBoosts.Add(BlogIndexDocument.Subject, BoostLevel.Hight);
                            fieldNameAndBoosts.Add(BlogIndexDocument.Keywords, BoostLevel.Hight);
                            fieldNameAndBoosts.Add(BlogIndexDocument.Tag, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(BlogIndexDocument.Summary, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(BlogIndexDocument.Body, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(BlogIndexDocument.OwnerCategoryName, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(BlogIndexDocument.Author, BoostLevel.Low);
                            searchBuilder.WithPhrases(fieldNameAndBoosts, blogQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                            break;
                    }
                }

                //某个站点分类
                if (blogQuery.SiteCategoryId != 0)
                {
                    searchBuilder.WithField(BlogIndexDocument.SiteCategoryId, blogQuery.SiteCategoryId.ToString(), true, BoostLevel.Hight, true);
                }

                if (!(UserContext.CurrentUser != null && UserContext.CurrentUser.UserId == blogQuery.LoginId && blogQuery.AllId != 0))
                {
                    searchBuilder.NotWithField(BlogIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Private).ToString());
                }
            }

            //筛选
            //全部、某人的日志
            if (blogQuery.AllId != 0)
            {
                if (blogQuery.LoginId != 0)
                {
                    searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.LoginId.ToString(), true, BoostLevel.Hight, true);
                }
                else if (blogQuery.UserId != 0)
                {
                    searchBuilder.WithField(BlogIndexDocument.UserId, blogQuery.UserId.ToString(), true, BoostLevel.Hight, true);
                }
            }

            //过滤可以显示的日志
            switch (publiclyAuditStatus)
            {
                case PubliclyAuditStatus.Again:
                case PubliclyAuditStatus.Fail:
                case PubliclyAuditStatus.Pending:
                case PubliclyAuditStatus.Success:
                    searchBuilder.WithField(BlogIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                    break;
                case PubliclyAuditStatus.Again_GreaterThanOrEqual:
                case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                    searchBuilder.WithinRange(BlogIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                    break;
            }

            return searchBuilder;
        }
Example #3
0
        /// <summary>
        /// 日志分页搜索
        /// </summary>
        /// <param name="blogQuery">搜索条件</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <BlogThread> Search(BlogFullTextQuery blogQuery)
        {
            if (blogQuery.SiteCategoryId == 0 && blogQuery.LoginId == 0 && blogQuery.UserId == 0)
            {
                if (string.IsNullOrWhiteSpace(blogQuery.Keyword) && blogQuery.IsRelationBlog == false)
                {
                    return(new PagingDataSet <BlogThread>(new List <BlogThread>()));
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(blogQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;

            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchService.Search(),执行搜索
            PagingDataSet <Document> searchResult = searchEngine.Search(query, filter, sort, blogQuery.PageIndex, blogQuery.PageSize);
            IEnumerable <Document>   docs         = searchResult.ToList <Document>();

            //解析出搜索结果中的日志ID
            List <long> blogThreadIds = new List <long>();
            //获取索引中日志的标签
            Dictionary <long, IEnumerable <string> > blogTags = new Dictionary <long, IEnumerable <string> >();
            //获取索引中日志的用户分类名
            Dictionary <long, IEnumerable <string> > blogOwnerCategoryNames = new Dictionary <long, IEnumerable <string> >();

            foreach (Document doc in docs)
            {
                long blogThreadId = long.Parse(doc.Get(BlogIndexDocument.ThreadId));
                blogThreadIds.Add(blogThreadId);
                blogTags[blogThreadId] = doc.GetValues(BlogIndexDocument.Tag).ToList <string>();
                blogOwnerCategoryNames[blogThreadId] = doc.GetValues(BlogIndexDocument.OwnerCategoryName).ToList <string>();
            }

            //根据日志ID列表批量查询日志实例
            IEnumerable <BlogThread> blogThreadList = blogThreadService.GetBlogThreads(blogThreadIds);

            foreach (var blogThread in blogThreadList)
            {
                blogThread.Body = blogThread.GetBody();
                if (blogTags.ContainsKey(blogThread.ThreadId))
                {
                    blogThread.TagNames = blogTags[blogThread.ThreadId];
                }
                if (blogOwnerCategoryNames.ContainsKey(blogThread.ThreadId))
                {
                    blogThread.OwnerCategoryNames = blogOwnerCategoryNames[blogThread.ThreadId];
                }
            }

            //组装分页对象
            PagingDataSet <BlogThread> blogThreads = new PagingDataSet <BlogThread>(blogThreadList)
            {
                TotalRecords  = searchResult.TotalRecords,
                PageSize      = searchResult.PageSize,
                PageIndex     = searchResult.PageIndex,
                QueryDuration = searchResult.QueryDuration
            };

            return(blogThreads);
        }
        /// <summary>
        /// 日志分页搜索
        /// </summary>
        /// <param name="blogQuery">搜索条件</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet<BlogThread> Search(BlogFullTextQuery blogQuery)
        {
            if (blogQuery.SiteCategoryId == 0 && blogQuery.LoginId == 0 && blogQuery.UserId == 0)
            {
                if (string.IsNullOrWhiteSpace(blogQuery.Keyword) && blogQuery.IsRelationBlog == false)
                {
                    return new PagingDataSet<BlogThread>(new List<BlogThread>());
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(blogQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query query = null;
            Filter filter = null;
            Sort sort = null;
            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchService.Search(),执行搜索
            PagingDataSet<Document> searchResult = searchEngine.Search(query, filter, sort, blogQuery.PageIndex, blogQuery.PageSize);
            IEnumerable<Document> docs = searchResult.ToList<Document>();

            //解析出搜索结果中的日志ID
            List<long> blogThreadIds = new List<long>();
            //获取索引中日志的标签
            Dictionary<long, IEnumerable<string>> blogTags = new Dictionary<long, IEnumerable<string>>();
            //获取索引中日志的用户分类名
            Dictionary<long, IEnumerable<string>> blogOwnerCategoryNames = new Dictionary<long, IEnumerable<string>>();

            foreach (Document doc in docs)
            {
                long blogThreadId = long.Parse(doc.Get(BlogIndexDocument.ThreadId));
                blogThreadIds.Add(blogThreadId);
                blogTags[blogThreadId] = doc.GetValues(BlogIndexDocument.Tag).ToList<string>();
                blogOwnerCategoryNames[blogThreadId] = doc.GetValues(BlogIndexDocument.OwnerCategoryName).ToList<string>();
            }

            //根据日志ID列表批量查询日志实例
            IEnumerable<BlogThread> blogThreadList = blogThreadService.GetBlogThreads(blogThreadIds);

            foreach (var blogThread in blogThreadList)
            {
                blogThread.Body = blogThread.GetBody();
                if (blogTags.ContainsKey(blogThread.ThreadId))
                {
                    blogThread.TagNames = blogTags[blogThread.ThreadId];
                }
                if (blogOwnerCategoryNames.ContainsKey(blogThread.ThreadId))
                {
                    blogThread.OwnerCategoryNames = blogOwnerCategoryNames[blogThread.ThreadId];
                }
            }

            //组装分页对象
            PagingDataSet<BlogThread> blogThreads = new PagingDataSet<BlogThread>(blogThreadList)
            {
                TotalRecords = searchResult.TotalRecords,
                PageSize = searchResult.PageSize,
                PageIndex = searchResult.PageIndex,
                QueryDuration = searchResult.QueryDuration
            };

            return blogThreads;
        }