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