示例#1
0
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query">搜索条件</param>
        /// <param name="interestGroup">是否是查询可能感兴趣的群组</param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(GroupFullTextQuery groupQuery, bool interestGroup = false)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            Dictionary<string, BoostLevel> fieldNameAndBoosts = new Dictionary<string, BoostLevel>();
            //关键字为空就是在搜地区时关键字为空
            if (groupQuery.KeywordIsNull)
            {
                if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
                    searchBuilder.WithField(GroupIndexDocument.AreaCode, groupQuery.NowAreaCode.TrimEnd('0'), false, BoostLevel.Hight, false);
                else
                    searchBuilder.WithFields(GroupIndexDocument.AreaCode, new string[] { "1", "2", "3" }, false, BoostLevel.Hight, false);
            }

            if (!string.IsNullOrEmpty(groupQuery.Keyword))
            {
                //范围
                switch (groupQuery.Range)
                {
                    case GroupSearchRange.GROUPNAME:
                        searchBuilder.WithPhrase(GroupIndexDocument.GroupName, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case GroupSearchRange.DESCRIPTION:
                        searchBuilder.WithPhrase(GroupIndexDocument.Description, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case GroupSearchRange.TAG:
                        searchBuilder.WithPhrase(GroupIndexDocument.Tag, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case GroupSearchRange.CATEGORYNAME:
                        searchBuilder.WithPhrase(GroupIndexDocument.CategoryName, groupQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    default:
                            fieldNameAndBoosts.Add(GroupIndexDocument.GroupName, BoostLevel.Hight);
                            fieldNameAndBoosts.Add(GroupIndexDocument.Description, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(GroupIndexDocument.Tag, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(GroupIndexDocument.CategoryName, BoostLevel.Medium);
                            searchBuilder.WithPhrases(fieldNameAndBoosts, groupQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                        break;
                }
            }

            //根据标签搜索可能感兴趣的群组
            if (interestGroup)
            {
                searchBuilder.WithPhrases(GroupIndexDocument.Tag, groupQuery.Tags, BoostLevel.Hight, false);
                searchBuilder.NotWithFields(GroupIndexDocument.GroupId, groupQuery.GroupIds);
            }

            //筛选
            //某地区
            if (!string.IsNullOrEmpty(groupQuery.NowAreaCode))
            {
                searchBuilder.WithField(GroupIndexDocument.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(GroupIndexDocument.CategoryId, categoryIds, true, BoostLevel.Hight, true);
            }

            //公开的群组
            searchBuilder.WithField(GroupIndexDocument.IsPublic, "1", true, BoostLevel.Hight, true);

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

            if (groupQuery.sortBy.HasValue)
            {
                switch (groupQuery.sortBy.Value)
                {
                    case SortBy_Group.DateCreated_Desc:
                        searchBuilder.SortByString(GroupIndexDocument.DateCreated, true);
                        break;
                    case SortBy_Group.MemberCount_Desc:
                        searchBuilder.SortByString(GroupIndexDocument.MemberCount, true);
                        break;
                    case SortBy_Group.GrowthValue_Desc:
                        searchBuilder.SortByString(GroupIndexDocument.GrowthValue, true);
                        break;
                }
            }
            else
            {
                //时间倒序排序
                searchBuilder.SortByString(GroupIndexDocument.DateCreated, true);
            }

            return searchBuilder;
        }
示例#2
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;
        }
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(JournalFullTextQuery journalQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            //范围
            Dictionary<string, BoostLevel> fieldNameAndBoosts = new Dictionary<string, BoostLevel>();

            //如果查的是相关日志
            if (journalQuery.IsRelationJournal)
            {
                fieldNameAndBoosts.Add(JournalIndexDocument.journalname, BoostLevel.Hight);
                fieldNameAndBoosts.Add(JournalIndexDocument.subcat, BoostLevel.Medium);
                fieldNameAndBoosts.Add(JournalIndexDocument.publisher, BoostLevel.Low);
                searchBuilder.WithPhrases(fieldNameAndBoosts, journalQuery.Keywords, BooleanClause.Occur.SHOULD, false);
                //searchBuilder.WithField(JournalIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Public).ToString(), true, BoostLevel.Hight, true);
                searchBuilder.NotWithField(JournalIndexDocument.id.ToString(), journalQuery.CurrentThreadId.ToString());
            }
            else
            {
                switch (journalQuery.JournalRange)
                {
                    /*case JournalRange.MYBlOG:
                        searchBuilder.WithField(JournalIndexDocument.UserId, journalQuery.LoginId.ToString(), true, BoostLevel.Hight, false);
                        break;
                    case JournalRange.SOMEONEBLOG:
                        searchBuilder.WithField(JournalIndexDocument.UserId, journalQuery.UserId.ToString(), true, BoostLevel.Hight, false);
                        break;
                    case JournalRange.SITECATEGORY:
                        if (journalQuery.LoginId != 0)
                            searchBuilder.WithField(JournalIndexDocument.UserId, journalQuery.LoginId.ToString(), true, BoostLevel.Hight, false);
                        if (journalQuery.UserId != 0)
                            searchBuilder.WithField(JournalIndexDocument.UserId, journalQuery.UserId.ToString(), true, BoostLevel.Hight, false);
                        if (journalQuery.SiteCategoryId != 0)
                            searchBuilder.WithField(JournalIndexDocument.SiteCategoryId, journalQuery.SiteCategoryId.ToString(), true, BoostLevel.Hight, false);
                        break;**/
                    default:
                        break;
                }
                if (!string.IsNullOrEmpty(journalQuery.Keyword))
                {
                    switch (journalQuery.Range)
                    {
                        /*case JournalSearchRange.SUBJECT:
                            searchBuilder.WithPhrase(JournalIndexDocument.Subject, journalQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case JournalSearchRange.BODY:
                            searchBuilder.WithPhrase(JournalIndexDocument.Body, journalQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case JournalSearchRange.AUTHOR:
                            searchBuilder.WithPhrase(JournalIndexDocument.Author, journalQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case JournalSearchRange.TAG:
                            searchBuilder.WithPhrase(JournalIndexDocument.Tag, journalQuery.Keyword, BoostLevel.Hight, false);
                            break;
                        case JournalSearchRange.OWNERCATEGORYNAME:
                            searchBuilder.WithPhrase(JournalIndexDocument.OwnerCategoryName, journalQuery.Keyword, BoostLevel.Hight, false);
                            break;*/
                        default:
                            fieldNameAndBoosts.Add(JournalIndexDocument.journalname, BoostLevel.Hight);
                            fieldNameAndBoosts.Add(JournalIndexDocument.publisher, BoostLevel.Hight);
                            fieldNameAndBoosts.Add(JournalIndexDocument.subcat, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(JournalIndexDocument.jdescrip, BoostLevel.Medium);
                            fieldNameAndBoosts.Add(JournalIndexDocument.abbr, BoostLevel.Medium);
                            //fieldNameAndBoosts.Add(JournalIndexDocument.OwnerCategoryName, BoostLevel.Medium);
                            //fieldNameAndBoosts.Add(JournalIndexDocument.Author, BoostLevel.Low);
                            searchBuilder.WithPhrases(fieldNameAndBoosts, journalQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                            break;
                    }
                }

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

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

            //筛选
            //全部、某人的日志
            /*if (journalQuery.AllId != 0)
            {
                if (journalQuery.LoginId != 0)
                {
                    searchBuilder.WithField(JournalIndexDocument.UserId, journalQuery.LoginId.ToString(), true, BoostLevel.Hight, true);
                }
                else if (journalQuery.UserId != 0)
                {
                    searchBuilder.WithField(JournalIndexDocument.UserId, journalQuery.UserId.ToString(), true, BoostLevel.Hight, true);
                }
            }
            */
            //过滤可以显示的日志
            /*switch (publiclyAuditStatus)
            {
                case PubliclyAuditStatus.Again:
                case PubliclyAuditStatus.Fail:
                case PubliclyAuditStatus.Pending:
                case PubliclyAuditStatus.Success:
                    searchBuilder.WithField(JournalIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                    break;
                case PubliclyAuditStatus.Again_GreaterThanOrEqual:
                case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                    searchBuilder.WithinRange(JournalIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                    break;
            }*/

            return searchBuilder;
        }
示例#4
0
        /// <summary>
        /// 搜索我和TA共同的内容
        /// </summary>
        /// <param name="myUserId">我的用户ID</param>
        /// <param name="taUserId">TA的用户ID</param>
        public void SearchInterested(long myUserId, long taUserId, out IEnumerable<string> sameTagNames, out IEnumerable<string> sameCompanyNames, out IEnumerable<string> sameSchoolNames)
        {
            sameTagNames = new List<string>();
            sameCompanyNames = new List<string>();
            sameSchoolNames = new List<string>();

            //无效用户ID,直接返回空列表
            if (myUserId <= 0 || taUserId <= 0)
            {
                return;
            }

            //搜索出我和TA的Document,使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query query = null;
            Filter filter = null;
            Sort sort = null;
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            searchBuilder.WithFields(UserIndexDocument.UserId, new List<string> { myUserId.ToString(), taUserId.ToString() });
            searchBuilder.BuildQuery(out query, out filter, out sort);

            IEnumerable<Document> docs = searchEngine.Search(query, filter, sort, 2);

            //应该返回两条Document,分别对应我和TA的UserId,否则直接返回空列表
            if (docs == null || docs.Count() != 2)
            {
                return;
            }

            string[] myTagNames = docs.ElementAt(0).GetValues(UserIndexDocument.TagName);
            string[] taTagNames = docs.ElementAt(1).GetValues(UserIndexDocument.TagName);
            string[] myCompanyNames = docs.ElementAt(0).GetValues(UserIndexDocument.CompanyName);
            string[] taCompanyNames = docs.ElementAt(1).GetValues(UserIndexDocument.CompanyName);
            string[] mySchoolNames = docs.ElementAt(0).GetValues(UserIndexDocument.School);
            string[] taSchoolNames = docs.ElementAt(1).GetValues(UserIndexDocument.School);

            //比较相同的内容
            sameTagNames = myTagNames.Intersect(taTagNames);
            sameCompanyNames = myCompanyNames.Intersect(taCompanyNames);
            sameSchoolNames = mySchoolNames.Intersect(taSchoolNames);
        }
示例#5
0
        /// <summary>
        /// 搜索“使用了相同标签”的用户
        /// </summary>
        /// <param name="userId">当前用户的ID(浏览者)</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="tagNameDic">存储用户ID到标签列表的映射,用于页面列表输出</param>
        /// <returns></returns>
        public PagingDataSet<User> SearchInterestedWithTags(long userId, int pageIndex, int pageSize, out Dictionary<long, IEnumerable<string>> tagNameDic)
        {
            //Dictionary,用于页面列表输出
            tagNameDic = new Dictionary<long, IEnumerable<string>>();

            //无效用户ID,直接返回空列表
            if (userId <= 0)
            {
                return new PagingDataSet<User>(new List<User>());
            }

            Query query = null;
            Filter filter = null;
            Sort sort = null;

            //先搜索出当前用户的标签
            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            searchBuilder.WithField(UserIndexDocument.UserId, userId.ToString(), true);
            searchBuilder.BuildQuery(out query, out filter, out sort);

            IEnumerable<Document> docs = searchEngine.Search(query, filter, sort, 1);

            //索引中无此用户,直接返回空列表
            if (docs == null || docs.Count<Document>() == 0)
            {
                return new PagingDataSet<User>(new List<User>());
            }

            string[] myTagNames = docs.First<Document>().GetValues(UserIndexDocument.TagName);

            //当前用户无标签,直接返回空列表
            if (myTagNames != null && myTagNames.Count() == 0)
            {
                return new PagingDataSet<User>(new List<User>());
            }

            //查找有相同标签的用户
            //先查询当前用户关注的人(包括“悄悄关注”的用户),此处需要调用数据库查询,因为索引中没有存储“是否悄悄关注”属性
            IEnumerable<long> myFollowedUserIds = followService.GetPortionFollowedUserIds(userId);
            //黑名单用户
            IEnumerable<long> stopUserIds = new PrivacyService().GetStopedUsers(userId).Select(n => n.Key);
            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            searchBuilder = new LuceneSearchBuilder(0);
            //搜索条件需要排除掉当前用户本身
            searchBuilder.WithPhrases(UserIndexDocument.TagName, myTagNames.ToList<string>())
                         .NotWithField(UserIndexDocument.UserId, userId.ToString()) //排除掉当前用户
                         .NotWithFields(UserIndexDocument.UserId, myFollowedUserIds.Select(n => n.ToString()))//排除掉已关注用户
                         .NotWithFields(UserIndexDocument.UserId, stopUserIds.Select(n => n.ToString()));//排除掉黑名单用户
            searchBuilder.BuildQuery(out query, out filter, out sort);

            PagingDataSet<Document> searchResult = searchEngine.Search(query, filter, sort, pageIndex, pageSize);
            docs = searchResult.ToList<Document>();

            //如果没有使用相同标签的用户,直接返回空列表
            if (docs == null || docs.Count<Document>() == 0)
            {
                return new PagingDataSet<User>(new List<User>());
            }

            //“使用了相同标签”的用户ID列表
            List<long> sameUserIds = new List<long>();
            foreach (Document doc in docs)
            {
                long sameUserId = long.Parse(doc.Get(UserIndexDocument.UserId));
                sameUserIds.Add(sameUserId);

                string[] tagNames = doc.GetValues(UserIndexDocument.TagName);

                //比较获取“相同的标签”
                IEnumerable<string> sameTagNames = myTagNames.Intersect<string>(tagNames);

                //加入Dictionary,用于页面列表输出
                if (!tagNameDic.ContainsKey(sameUserId))
                {
                    tagNameDic.Add(sameUserId, sameTagNames);
                }
            }

            //批量查询“使用了相同标签”的用户列表
            IEnumerable<User> sameUsers = userService.GetFullUsers(sameUserIds).Where(n => n.IsCanbeFollow == true && n.IsActivated == true && n.IsBanned == false);

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

            return users;
        }
示例#6
0
        /// <summary>
        /// 根据微博搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(MicroblogFullTextQuery microblogQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            //微博搜索词匹配范围
            string fieldName = null;
            switch (microblogQuery.SearchRange)
            {
                case MicroblogSearchRange.TOPIC:
                    fieldName = MicroblogIndexDocument.Topic;
                    break;
                default:
                    fieldName = MicroblogIndexDocument.Body;
                    break;
            }
            if (microblogQuery.Keywords != null && microblogQuery.Keywords.Count() != 0)
            {
                //是否模糊查询
                if (microblogQuery.IsFuzzy)
                {
                    searchBuilder.WithPhrases(fieldName, microblogQuery.Keywords, BoostLevel.Hight, false);
                }
                else
                {
                    searchBuilder.WithFields(fieldName, microblogQuery.Keywords, true, BoostLevel.Hight, false);
                }
            }
            else
            {
                searchBuilder.WithPhrase(fieldName, microblogQuery.Keyword, BoostLevel.Hight);

            }

            //微博搜索条件过滤
            switch (microblogQuery.SearchTerm)
            {
                case MicroblogSearchTerm.ISORIGINALITY:
                    searchBuilder.WithField(MicroblogIndexDocument.IsOriginality, 1, true, BoostLevel.Hight, true);
                    break;
                case MicroblogSearchTerm.HASPHOTO:
                    searchBuilder.WithField(MicroblogIndexDocument.HasPhoto, 1, true, BoostLevel.Hight, true);
                    break;
                case MicroblogSearchTerm.HASMUSIC:
                    searchBuilder.WithField(MicroblogIndexDocument.HasMusic, 1, true, BoostLevel.Hight, true);
                    break;
                case MicroblogSearchTerm.HASVIDEO:
                    searchBuilder.WithField(MicroblogIndexDocument.HasVideo, 1, true, BoostLevel.Hight, true);
                    break;
                default:
                    break;
            }

            ////筛选租户类型
            //if (!string.IsNullOrEmpty(microblogQuery.TenantTypeId))
            //{
            //    searchBuilder.WithField(MicroblogIndexDocument.TenantTypeId, microblogQuery.TenantTypeId, true, BoostLevel.Hight, true);
            //}
            if (!microblogQuery.IsGroup)
            {
                searchBuilder.NotWithField(MicroblogIndexDocument.TenantTypeId, TenantTypeIds.Instance().Group());
            }

            //微博排序
            searchBuilder.SortByString(MicroblogIndexDocument.DateCreated, true);

            return searchBuilder;
        }
示例#7
0
        /// <summary>
        /// 搜索我和TA共同关注的用户
        /// </summary>
        /// <param name="myUserId">我的用户ID</param>
        /// <param name="taUserId">TA的用户列表</param>
        /// <returns>我和TA共同关注的用户列表</returns>
        public IEnumerable<User> SearchInterestedWithFollows(long myUserId, long taUserId)
        {
            //无效用户ID,直接返回空列表
            if (myUserId <= 0 || taUserId <= 0)
            {
                return new List<User>();
            }

            //搜索出我和TA的Document,使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query query = null;
            Filter filter = null;
            Sort sort = null;
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            searchBuilder.WithFields(UserId, new List<string> { myUserId.ToString(), taUserId.ToString() });
            searchBuilder.BuildQuery(out query, out filter, out sort);

            IEnumerable<Document> docs = searchEngine.Search(query, filter, sort, 2);

            //应该返回两条Document,分别对应我和TA的UserId,否则直接返回空列表
            if (docs == null || docs.Count() != 2)
            {
                return new List<User>();
            }

            string[] myFollowedUserIds = docs.ElementAt(0).GetValues(FollowedUserIds);
            string[] taFollowedUserIds = docs.ElementAt(1).GetValues(FollowedUserIds);

            //比较相同的关注用户
            IEnumerable<string> sameFollowedUserIds = myFollowedUserIds.Intersect(taFollowedUserIds);

            //批量查询“共同关注的用户”列表
            IEnumerable<User> sameFollowedUsers = userService.GetFullUsers(sameFollowedUserIds.Select(n => Convert.ToInt64(n)));

            return sameFollowedUsers;
        }
示例#8
0
        /// <summary>
        /// 搜索共同关注的人
        /// </summary>
        /// <param name="userId">粉丝的用户ID</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="followedUserIdDic">每个相同关注用户中共同关注的用户ID列表</param>
        /// <param name="followedUserDic">每个共同关注的用户的ID与User的映射</param>
        /// <returns>符合搜索条件的User分页集合</returns>
        public PagingDataSet<User> SearchInterestedWithFollows(long userId, int pageIndex, int pageSize, out Dictionary<long, IEnumerable<long>> followedUserIdDic, out Dictionary<long, User> followedUserDic)
        {
            followedUserIdDic = new Dictionary<long, IEnumerable<long>>();
            followedUserDic = new Dictionary<long, User>();

            if (userId <= 0)
            {
                return new PagingDataSet<User>(new List<User>());
            }

            //先查询当前用户关注的人(包括“悄悄关注”的用户),此处需要调用数据库查询,因为索引中没有存储“是否悄悄关注”属性
            IEnumerable<long> myFollowedUserIds = followService.GetPortionFollowedUserIds(userId);
            if (myFollowedUserIds != null && myFollowedUserIds.Count() == 0)
            {
                return new PagingDataSet<User>(new List<User>());
            }

            //黑名单用户
            IEnumerable<long> stopUserIds = new PrivacyService().GetStopedUsers(userId).Select(n => n.Key);

            //搜索“我”关注的人中包含“共同关注的人”的用户
            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query query = null;
            Filter filter = null;
            Sort sort = null;
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            searchBuilder.WithFields(FollowedUserIds, myFollowedUserIds.Select(n => n.ToString()), true)
                         .NotWithField(UserId, userId.ToString())//排除掉当前用户
                         .NotWithFields(UserIndexDocument.UserId, myFollowedUserIds.Select(n => n.ToString()))//排除掉已关注用户
                         .NotWithFields(UserIndexDocument.UserId, stopUserIds.Select(n => n.ToString()));//排除掉黑名单用户
            searchBuilder.BuildQuery(out query, out filter, out sort);

            PagingDataSet<Document> searchResult = searchEngine.Search(query, filter, sort, pageIndex, pageSize);
            IEnumerable<Document> docs = searchResult.ToList<Document>();

            if (docs == null || docs.Count() == 0)
            {
                return new PagingDataSet<User>(new List<User>());
            }

            List<long> sameFollowedUserIdList = new List<long>();

            //解析出搜索结果中的用户ID
            List<long> followerUserIds = new List<long>();
            foreach (Document doc in docs)
            {
                long followerUserId = long.Parse(doc.Get(UserId));
                followerUserIds.Add(followerUserId);

                //“我”关注的人关注的人
                string[] followedUserIds = doc.GetValues(FollowedUserIds);

                //比较获取“共同关注的人”
                IEnumerable<long> sameFollowedUserIds = myFollowedUserIds.Intersect<long>(followedUserIds.Select(n => Convert.ToInt64(n)));
                if (!followedUserIdDic.ContainsKey(followerUserId))
                {
                    followedUserIdDic.Add(followerUserId, sameFollowedUserIds);
                }

                sameFollowedUserIdList.AddRange(sameFollowedUserIds);
            }

            //批量查询“共同关注的用户”列表
            IEnumerable<User> followerUserList = userService.GetFullUsers(followerUserIds).Where(n => n.IsCanbeFollow == true && n.IsActivated == true && n.IsBanned == false);

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

            //批量查询“共同关注的用户”关注的“共同关注用户”列表
            IEnumerable<User> sameFollowedUserList = userService.GetFullUsers(sameFollowedUserIdList.Distinct());
            followedUserDic = sameFollowedUserList.ToDictionary(n => n.UserId);

            return users;
        }
示例#9
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;
        }
        /// <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;
        }