/// <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); }
/// <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); }