Esempio n. 1
0
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query">搜索条件</param>
        /// <param name="interestTopic">是否是查询可能感兴趣的专题</param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(TopicFullTextQuery groupQuery, bool interestTopic = false)
        {
            LuceneSearchBuilder             searchBuilder      = new LuceneSearchBuilder();
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            //关键字为空就是在搜地区时关键字为空
            if (groupQuery.KeywordIsNull)
            {
                if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
                {
                    searchBuilder.WithField(TopicIndexDocument.AreaCode, groupQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                }
                else
                {
                    searchBuilder.WithFields(TopicIndexDocument.AreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                }
            }

            if (!string.IsNullOrEmpty(groupQuery.Keyword))
            {
                //范围
                switch (groupQuery.Range)
                {
                case TopicSearchRange.TOPICNAME:
                    searchBuilder.WithPhrase(TopicIndexDocument.TopicName, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case TopicSearchRange.DESCRIPTION:
                    searchBuilder.WithPhrase(TopicIndexDocument.Description, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case TopicSearchRange.TAG:
                    searchBuilder.WithPhrase(TopicIndexDocument.Tag, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case TopicSearchRange.CATEGORYNAME:
                    searchBuilder.WithPhrase(TopicIndexDocument.CategoryName, groupQuery.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(TopicIndexDocument.TopicName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(TopicIndexDocument.Description, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(TopicIndexDocument.Tag, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(TopicIndexDocument.CategoryName, BoostLevel.Medium);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, groupQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

            //根据标签搜索可能感兴趣的专题
            if (interestTopic)
            {
                searchBuilder.WithPhrases(TopicIndexDocument.Tag, groupQuery.Tags, BoostLevel.Hight, false);
                searchBuilder.NotWithFields(TopicIndexDocument.TopicId, groupQuery.TopicIds);
            }

            //筛选
            //某地区
            if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
            {
                searchBuilder.WithField(TopicIndexDocument.AreaCode, groupQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //某分类
            if (groupQuery.CategoryId != 0)
            {
                //fixed by wanf:发现专题已经不再用全文检索了

                CategoryService        categoryService = new CategoryService();
                IEnumerable <Category> categories      = categoryService.GetDescendants(groupQuery.CategoryId);
                List <string>          categoryIds     = new List <string> {
                    groupQuery.CategoryId.ToString()
                };
                if (categories != null && categories.Count() > 0)
                {
                    categoryIds.AddRange(categories.Select(n => n.CategoryId.ToString()));
                }

                searchBuilder.WithFields(TopicIndexDocument.CategoryId, categoryIds, true, BoostLevel.Hight, true);
            }

            //公开的专题
            searchBuilder.WithField(TopicIndexDocument.IsPublic, "1", true, BoostLevel.Hight, true);

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

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

            if (groupQuery.sortBy.HasValue)
            {
                switch (groupQuery.sortBy.Value)
                {
                case SortBy_Topic.DateCreated_Desc:
                    searchBuilder.SortByString(TopicIndexDocument.DateCreated, true);
                    break;

                case SortBy_Topic.MemberCount_Desc:
                    searchBuilder.SortByString(TopicIndexDocument.MemberCount, true);
                    break;

                case SortBy_Topic.GrowthValue_Desc:
                    searchBuilder.SortByString(TopicIndexDocument.GrowthValue, true);
                    break;
                }
            }
            else
            {
                //时间倒序排序
                searchBuilder.SortByString(TopicIndexDocument.DateCreated, true);
            }

            return(searchBuilder);
        }
Esempio n. 2
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);
        }
Esempio n. 3
0
        /// <summary>
        /// 根据用户搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(UserFullTextQuery userQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();

            //搜索词匹配范围
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            switch (userQuery.SearchRange)
            {
            case UserSearchRange.NAME:
                fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Low);
                fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                break;

            case UserSearchRange.TAG:
                searchBuilder.WithPhrase(UserIndexDocument.TagName, userQuery.Keyword, BoostLevel.Hight, false);
                break;

            case UserSearchRange.SCHOOL:
                searchBuilder.WithPhrase(UserIndexDocument.School, userQuery.Keyword, BoostLevel.Hight, false);
                break;

            case UserSearchRange.COMPANY:
                searchBuilder.WithPhrase(UserIndexDocument.CompanyName, userQuery.Keyword, BoostLevel.Hight, false);
                break;

            case UserSearchRange.NOWAREACODE:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                if (!string.IsNullOrEmpty(userQuery.NowAreaCode))
                {
                    searchBuilder.WithField(UserIndexDocument.NowAreaCode, userQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                }
                else
                {
                    searchBuilder.WithFields(UserIndexDocument.NowAreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                }
                break;

            case UserSearchRange.HOMEAREACODE:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                if (!string.IsNullOrEmpty(userQuery.HomeAreaCode))
                {
                    searchBuilder.WithField(UserIndexDocument.HomeAreaCode, userQuery.HomeAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                }
                else
                {
                    searchBuilder.WithFields(UserIndexDocument.HomeAreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
                }
                break;

            case UserSearchRange.Gender:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                searchBuilder.WithField(UserIndexDocument.Gender, ((int)userQuery.Gender).ToString(), false, BoostLevel.Hight, false);
                break;

            case UserSearchRange.Age:
                if (!string.IsNullOrEmpty(userQuery.Keyword))
                {
                    fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                    fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                    searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                }
                if (userQuery.AgeMin > userQuery.AgeMax)
                {
                    int temp = userQuery.AgeMin;
                    userQuery.AgeMin = userQuery.AgeMax;
                    userQuery.AgeMax = temp;
                }
                if (userQuery.AgeMin < 0)
                {
                    userQuery.AgeMin = 0;
                }
                if (userQuery.AgeMax > 200)
                {
                    userQuery.AgeMin = 200;
                }

                string yearMax = (DateTime.Now.Year - userQuery.AgeMin).ToString().PadLeft(3, '0');
                string yearMin = (DateTime.Now.Year - userQuery.AgeMax).ToString().PadLeft(3, '0');

                searchBuilder.WithinRange(UserIndexDocument.BirthdayYear, yearMin, yearMax, false);
                break;

            default:
                fieldNameAndBoosts.Add(UserIndexDocument.TrueName, BoostLevel.Hight);
                fieldNameAndBoosts.Add(UserIndexDocument.PinyinName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.NickName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.UserName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.TagName, BoostLevel.Medium);
                fieldNameAndBoosts.Add(UserIndexDocument.School, BoostLevel.Low);
                fieldNameAndBoosts.Add(UserIndexDocument.CompanyName, BoostLevel.Low);
                fieldNameAndBoosts.Add(UserIndexDocument.ShortPinyinName, BoostLevel.Low);

                searchBuilder.WithPhrases(fieldNameAndBoosts, userQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                break;
            }

            //所在地区过滤
            if (!string.IsNullOrWhiteSpace(userQuery.NowAreaCode) && userQuery.SearchRange != UserSearchRange.NOWAREACODE)
            {
                searchBuilder.WithField(UserIndexDocument.NowAreaCode, userQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //家乡地区过滤
            if (!string.IsNullOrWhiteSpace(userQuery.HomeAreaCode) && userQuery.SearchRange != UserSearchRange.HOMEAREACODE)
            {
                searchBuilder.WithField(UserIndexDocument.HomeAreaCode, userQuery.HomeAreaCode.TrimEnd('0'), false, BoostLevel.Hight, true);
            }

            //性别过滤
            if (userQuery.Gender != GenderType.NotSet)
            {
                searchBuilder.WithField(UserIndexDocument.Gender, ((int)userQuery.Gender).ToString(), true, BoostLevel.Hight, true);
            }

            //年龄过滤
            if (userQuery.AgeMin > 0 || userQuery.AgeMax > 0)
            {
                if (userQuery.AgeMin > userQuery.AgeMax)
                {
                    int temp = userQuery.AgeMin;
                    userQuery.AgeMin = userQuery.AgeMax;
                    userQuery.AgeMax = temp;
                }
                if (userQuery.AgeMin < 0)
                {
                    userQuery.AgeMin = 0;
                }
                if (userQuery.AgeMax > 200)
                {
                    userQuery.AgeMin = 200;
                }

                string yearMax = (DateTime.Now.Year - userQuery.AgeMin).ToString().PadLeft(3, '0');
                string yearMin = (DateTime.Now.Year - userQuery.AgeMax).ToString().PadLeft(3, '0');

                searchBuilder.WithinRange(UserIndexDocument.BirthdayYear, yearMin, yearMax, true);
            }

            //按最后活动时间倒排序
            searchBuilder.SortByString(UserIndexDocument.LastActivityTime, true);

            return(searchBuilder);
        }
Esempio n. 4
0
        /// <summary>
        /// 根据照片搜索条件构建lucene查询条件
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>lucene条件</returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(PhotoFullTextQuery query)
        {
            LuceneSearchBuilder             searchBuilder      = new LuceneSearchBuilder();
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            if (query.UserId > 0)
            {
                searchBuilder.WithField(PhotoIndexDocument.UserId, query.UserId.ToString(), true, BoostLevel.Hight, false);
            }

            if (!string.IsNullOrEmpty(query.Keyword))
            {
                switch (query.Filter)
                {
                case PhotoSearchRange.DESCRIPTION:
                    searchBuilder.WithPhrase(PhotoIndexDocument.Description, query.Keyword, BoostLevel.Hight, false);
                    break;

                case PhotoSearchRange.AUTHOR:
                    searchBuilder.WithPhrase(PhotoIndexDocument.Author, query.Keyword, BoostLevel.Hight, false);
                    break;

                case PhotoSearchRange.TAG:
                    searchBuilder.WithPhrase(PhotoIndexDocument.Tag, query.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(PhotoIndexDocument.Tag, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(PhotoIndexDocument.Description, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(PhotoIndexDocument.Author, BoostLevel.Medium);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

            //筛选 全部、某人的照片
            if (query.UserId > 0)
            {
                searchBuilder.WithField(PhotoIndexDocument.UserId, query.UserId.ToString(), true, BoostLevel.Hight, true);
            }

            //过滤审核状态和隐私状态
            if (!query.IgnoreAuditAndPrivacy)
            {
                searchBuilder.NotWithField(PhotoIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Private).ToString());
                switch (publiclyAuditStatus)
                {
                case PubliclyAuditStatus.Again:
                case PubliclyAuditStatus.Fail:
                case PubliclyAuditStatus.Pending:
                case PubliclyAuditStatus.Success:
                    searchBuilder.WithField(PhotoIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                    break;

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

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

            if (!string.IsNullOrEmpty(cmsQuery.Keyword))
            {
                switch (cmsQuery.Range)
                {
                case CmsSearchRange.TITLE:
                    searchBuilder.WithPhrase(CmsIndexDocument.Title, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case CmsSearchRange.BODY:
                    searchBuilder.WithPhrase(CmsIndexDocument.Body, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case CmsSearchRange.AUTHOR:
                    searchBuilder.WithPhrase(CmsIndexDocument.Author, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case CmsSearchRange.TAG:
                    searchBuilder.WithPhrase(CmsIndexDocument.Tag, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(CmsIndexDocument.Title, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Summary, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Tag, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Body, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Author, BoostLevel.Low);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, cmsQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

            //某个栏目
            if (cmsQuery.ContentFolderId > 0)
            {
                searchBuilder.WithField(CmsIndexDocument.ContentFolderId, cmsQuery.ContentFolderId.ToString(), true, BoostLevel.Hight, true);
            }

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

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

            return(searchBuilder);
        }
Esempio n. 6
0
        /// <summary>
        /// 构建lucene查询条件
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuilderLuceneSearchBuilder(AskFullTextQuery query)
        {
            LuceneSearchBuilder             searchBuilder      = new LuceneSearchBuilder();
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            if (query.IsAlike)
            {
                searchBuilder.WithPhrases(AskIndexDocument.Subject, query.Keywords, BoostLevel.Hight, false);
            }
            else if (query.IsRelation)
            {
                fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight);
                fieldNameAndBoosts.Add(AskIndexDocument.Tag, BoostLevel.Hight);
                searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keywords, BooleanClause.Occur.SHOULD, false);
            }
            else
            {
                //筛选
                switch (query.Range)
                {
                case AskSearchRange.SUBJECT:
                    fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(AskIndexDocument.Body, BoostLevel.Medium);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false);

                    break;

                case AskSearchRange.TAG:
                    searchBuilder.WithPhrase(AskIndexDocument.Tag, query.Keyword, BoostLevel.Hight, false);
                    break;

                case AskSearchRange.AUTHOR:
                    searchBuilder.WithPhrase(AskIndexDocument.Author, query.Keyword, BoostLevel.Hight, false);
                    break;

                case AskSearchRange.ANSWER:
                    searchBuilder.WithPhrase(AskIndexDocument.Answer, query.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(AskIndexDocument.Tag, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(AskIndexDocument.Body, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(AskIndexDocument.Answer, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(AskIndexDocument.Author, BoostLevel.Low);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

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

            case PubliclyAuditStatus.Again_GreaterThanOrEqual:
            case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                searchBuilder.WithinRange(AskIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                break;
            }
            if (query.sortBy == SortBy_AskQuestion.DateCreated_Desc)
            {
                searchBuilder.SortByString(AskIndexDocument.DateCreated, true);
            }

            return(searchBuilder);
        }