コード例 #1
0
ファイル: UserSearcher.cs プロジェクト: ClaytonWang/Dw3cSNS
        /// <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;
        }
コード例 #2
0
ファイル: FollowUserSearcher.cs プロジェクト: hbulzy/SYS
        /// <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;
        }
コード例 #3
0
ファイル: UserSearcher.cs プロジェクト: ClaytonWang/Dw3cSNS
        /// <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);
        }
コード例 #4
0
ファイル: FollowUserSearcher.cs プロジェクト: hbulzy/SYS
        /// <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;
        }