/// <summary> /// 根据帖吧搜索查询条件构建Lucene查询条件 /// </summary> /// <param name="Query"></param> /// <returns></returns> private LuceneSearchBuilder BuildLuceneSearchBuilder(WikiFullTextQuery WikiQuery) { LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder(); //范围 Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>(); if (!string.IsNullOrEmpty(WikiQuery.Keyword)) { switch (WikiQuery.Range) { case WikiSearchRange.Title: searchBuilder.WithPhrase(WikiIndexDocument.Title, WikiQuery.Keyword, BoostLevel.Hight, false); break; case WikiSearchRange.Category: searchBuilder.WithPhrase(WikiIndexDocument.CategoryId, WikiQuery.Keyword, BoostLevel.Hight, false); break; case WikiSearchRange.AUTHOR: searchBuilder.WithPhrase(WikiIndexDocument.Author, WikiQuery.Keyword, BoostLevel.Hight, false); break; case WikiSearchRange.TAG: searchBuilder.WithPhrase(WikiIndexDocument.Tag, WikiQuery.Keyword, BoostLevel.Hight, false); break; default: fieldNameAndBoosts.Add(WikiIndexDocument.Title, BoostLevel.Hight); fieldNameAndBoosts.Add(WikiIndexDocument.Tag, BoostLevel.Medium); fieldNameAndBoosts.Add(WikiIndexDocument.Body, BoostLevel.Medium); fieldNameAndBoosts.Add(WikiIndexDocument.Author, BoostLevel.Low); fieldNameAndBoosts.Add(WikiIndexDocument.CategoryId, BoostLevel.Hight); searchBuilder.WithPhrases(fieldNameAndBoosts, WikiQuery.Keyword, BooleanClause.Occur.SHOULD, false); break; } } //某个站点分类 if (WikiQuery.SiteCategoryId != 0) { searchBuilder.WithField(WikiIndexDocument.CategoryId, WikiQuery.SiteCategoryId.ToString(), true, BoostLevel.Hight, true); } return(searchBuilder); }
/// <summary> /// 根据帖吧搜索查询条件构建Lucene查询条件 /// </summary> /// <param name="barQuery"></param> /// <returns></returns> private LuceneSearchBuilder BuildLuceneSearchBuilder(BarFullTextQuery barQuery) { LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder(); //微博搜索词匹配范围 //搜索词匹配范围 Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>(); switch (barQuery.Term) { case BarSearchRange.SUBJECT: searchBuilder.WithPhrase(BarIndexDocument.Subject, barQuery.Keyword, BoostLevel.Hight, false); break; case BarSearchRange.BODY: fieldNameAndBoosts.Add(BarIndexDocument.Body, BoostLevel.Hight); fieldNameAndBoosts.Add(BarIndexDocument.Subject, BoostLevel.Medium); searchBuilder.WithPhrases(fieldNameAndBoosts, barQuery.Keyword, BooleanClause.Occur.SHOULD, false); //searchBuilder.WithPhrase(BarIndexDocument.Body, barQuery.Keyword, BoostLevel.Hight, false); break; case BarSearchRange.AUTHOR: searchBuilder.WithPhrase(BarIndexDocument.Author, barQuery.Keyword, BoostLevel.Hight, false); break; case BarSearchRange.TAG: searchBuilder.WithPhrase(BarIndexDocument.Tag, barQuery.Keyword, BoostLevel.Hight, false); break; default: fieldNameAndBoosts.Add(BarIndexDocument.Subject, BoostLevel.Hight); fieldNameAndBoosts.Add(BarIndexDocument.Body, BoostLevel.Medium); fieldNameAndBoosts.Add(BarIndexDocument.Tag, BoostLevel.Medium); fieldNameAndBoosts.Add(BarIndexDocument.Author, BoostLevel.Low); searchBuilder.WithPhrases(fieldNameAndBoosts, barQuery.Keyword, BooleanClause.Occur.SHOULD, false); break; } //筛选租户类型 if (!string.IsNullOrEmpty(barQuery.TenantTypeId)) { searchBuilder.WithField(BarIndexDocument.TenantTypeId, barQuery.TenantTypeId, true, BoostLevel.Hight, true); } //帖吧搜索条件过滤(全吧、吧内) if (barQuery.Range != "-1") { searchBuilder.WithField(BarIndexDocument.SectionId, barQuery.Range, true, BoostLevel.Hight, true); } //帖吧搜索条件过滤(帖子、回帖) if (barQuery.IsPost == "1") { searchBuilder.WithField(BarIndexDocument.IsPost, "1", true, BoostLevel.Hight, true); } if (barQuery.IsPost == "0") { searchBuilder.WithField(BarIndexDocument.IsPost, "0", true, BoostLevel.Hight, true); } //帖吧排序 searchBuilder.SortByString(BarIndexDocument.DateCreated, true); return(searchBuilder); }
/// <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="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); }
/// <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> /// 搜索“毕业于同一学校”的用户/// </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); }
/// <summary> /// 根据照片搜索条件构建lucene查询条件 /// </summary> /// <param name="query">查询条件</param> /// <returns>lucene条件</returns> private LuceneSearchBuilder BuildLuceneSearchBuilder(PhotoFullTextQuery query) { LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder(); Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>(); if (query.UserId > 0) { searchBuilder.WithField(PhotoIndexDocument.UserId, query.UserId.ToString(), true, BoostLevel.Hight, false); } if (!string.IsNullOrEmpty(query.Keyword)) { switch (query.Filter) { case PhotoSearchRange.DESCRIPTION: searchBuilder.WithPhrase(PhotoIndexDocument.Description, query.Keyword, BoostLevel.Hight, false); break; case PhotoSearchRange.AUTHOR: searchBuilder.WithPhrase(PhotoIndexDocument.Author, query.Keyword, BoostLevel.Hight, false); break; case PhotoSearchRange.TAG: searchBuilder.WithPhrase(PhotoIndexDocument.Tag, query.Keyword, BoostLevel.Hight, false); break; default: fieldNameAndBoosts.Add(PhotoIndexDocument.Tag, BoostLevel.Hight); fieldNameAndBoosts.Add(PhotoIndexDocument.Description, BoostLevel.Hight); fieldNameAndBoosts.Add(PhotoIndexDocument.Author, BoostLevel.Medium); searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false); break; } } //筛选 全部、某人的照片 if (query.UserId > 0) { searchBuilder.WithField(PhotoIndexDocument.UserId, query.UserId.ToString(), true, BoostLevel.Hight, true); } //过滤审核状态和隐私状态 if (!query.IgnoreAuditAndPrivacy) { searchBuilder.NotWithField(PhotoIndexDocument.PrivacyStatus, ((int)PrivacyStatus.Private).ToString()); switch (publiclyAuditStatus) { case PubliclyAuditStatus.Again: case PubliclyAuditStatus.Fail: case PubliclyAuditStatus.Pending: case PubliclyAuditStatus.Success: searchBuilder.WithField(PhotoIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true); break; case PubliclyAuditStatus.Again_GreaterThanOrEqual: case PubliclyAuditStatus.Pending_GreaterThanOrEqual: searchBuilder.WithinRange(PhotoIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true); break; } } return(searchBuilder); }
/// <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="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); }
/// <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); }
/// <summary> /// 构建lucene查询条件 /// </summary> /// <param name="query"></param> /// <returns></returns> private LuceneSearchBuilder BuilderLuceneSearchBuilder(AskFullTextQuery query) { LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder(); Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>(); if (query.IsAlike) { searchBuilder.WithPhrases(AskIndexDocument.Subject, query.Keywords, BoostLevel.Hight, false); } else if (query.IsRelation) { fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight); fieldNameAndBoosts.Add(AskIndexDocument.Tag, BoostLevel.Hight); searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keywords, BooleanClause.Occur.SHOULD, false); } else { //筛选 switch (query.Range) { case AskSearchRange.SUBJECT: fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight); fieldNameAndBoosts.Add(AskIndexDocument.Body, BoostLevel.Medium); searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false); break; case AskSearchRange.TAG: searchBuilder.WithPhrase(AskIndexDocument.Tag, query.Keyword, BoostLevel.Hight, false); break; case AskSearchRange.AUTHOR: searchBuilder.WithPhrase(AskIndexDocument.Author, query.Keyword, BoostLevel.Hight, false); break; case AskSearchRange.ANSWER: searchBuilder.WithPhrase(AskIndexDocument.Answer, query.Keyword, BoostLevel.Hight, false); break; default: fieldNameAndBoosts.Add(AskIndexDocument.Subject, BoostLevel.Hight); fieldNameAndBoosts.Add(AskIndexDocument.Tag, BoostLevel.Hight); fieldNameAndBoosts.Add(AskIndexDocument.Body, BoostLevel.Medium); fieldNameAndBoosts.Add(AskIndexDocument.Answer, BoostLevel.Medium); fieldNameAndBoosts.Add(AskIndexDocument.Author, BoostLevel.Low); searchBuilder.WithPhrases(fieldNameAndBoosts, query.Keyword, BooleanClause.Occur.SHOULD, false); break; } } //过滤可以显示的问题 switch (publiclyAuditStatus) { case PubliclyAuditStatus.Again: case PubliclyAuditStatus.Fail: case PubliclyAuditStatus.Pending: case PubliclyAuditStatus.Success: searchBuilder.WithField(AskIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true); break; case PubliclyAuditStatus.Again_GreaterThanOrEqual: case PubliclyAuditStatus.Pending_GreaterThanOrEqual: searchBuilder.WithinRange(AskIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true); break; } if (query.sortBy == SortBy_AskQuestion.DateCreated_Desc) { searchBuilder.SortByString(AskIndexDocument.DateCreated, true); } return(searchBuilder); }