/// <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); }
/// <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); }
/// <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> /// 根据帖吧搜索查询条件构建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); }
/// <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="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); }