예제 #1
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(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);
        }
예제 #2
0
        /// <summary>
        /// 搜索“毕业于同一学校”的用户/// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="schoolNameDic">存储用户ID到学校名称列表的映射,用于页面列表输出</param>
        /// <returns></returns>
        public PagingDataSet <User> SearchInterestedWithSchools(long userId, int pageIndex, int pageSize, out Dictionary <long, IEnumerable <string> > schoolNameDic)
        {
            //Dictionary,用于页面列表输出
            schoolNameDic = 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() == 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            string[] mySchoolNames = docs.First <Document>().GetValues(UserIndexDocument.School);

            //当前用户无学校,直接返回空列表
            if (mySchoolNames != null && mySchoolNames.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(1);
            //搜索条件需要排除掉当前用户本身
            searchBuilder.WithPhrases(UserIndexDocument.School, mySchoolNames.Distinct().Take(3).ToList())
            .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() == 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            //“相同学校名称”的用户ID列表
            List <long> sameUserIds = new List <long>();

            foreach (Document doc in docs)
            {
                //比较获取“相同的学校名称”
                List <string> sameSchoolNames = new List <string>();
                string[]      schoolNames     = doc.GetValues(UserIndexDocument.School);
                foreach (string mySchoolName in mySchoolNames)
                {
                    foreach (string schoolName in schoolNames)
                    {
                        if (schoolName.Equals(mySchoolName) || schoolName.Contains(mySchoolName))
                        {
                            sameSchoolNames.Add(mySchoolName);
                            break;
                        }
                    }
                }

                long sameUserId = long.Parse(doc.Get(UserIndexDocument.UserId));
                sameUserIds.Add(sameUserId);

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

            //批量查询“相同学校名称”的用户列表
            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);
        }