/// <summary> /// 帖吧搜索 /// </summary> /// <param name="query"></param> /// <returns></returns> public ActionResult _GlobalSearch(BarFullTextQuery query, int topNumber) { query.PageSize = topNumber;//每页记录数 query.PageIndex = 1; query.TenantTypeId = TenantTypeIds.Instance().Bar(); //调用搜索器进行搜索 BarSearcher barSearcher = (BarSearcher)SearcherFactory.GetSearcher(BarSearcher.CODE); PagingDataSet<BarEntity> BarEntities = barSearcher.Search(query); return PartialView(BarEntities); }
/// <summary> /// 帖吧快捷搜索 /// </summary> /// <param name="query"></param> /// <returns></returns> public ActionResult _QuickSearch(BarFullTextQuery query, int topNumber) { query.PageSize = topNumber;//每页记录数 query.PageIndex = 1; query.TenantTypeId = TenantTypeIds.Instance().Bar(); query.Term = BarSearchRange.SUBJECT; query.Keyword = Server.UrlDecode(query.Keyword); //调用搜索器进行搜索 BarSearcher barSearcher = (BarSearcher)SearcherFactory.GetSearcher(BarSearcher.CODE); PagingDataSet<BarEntity> BarEntities = barSearcher.Search(query); return PartialView(BarEntities); }
/// <summary> /// 帖吧搜索 /// </summary> /// <param name="query"></param> /// <returns></returns> public ActionResult Search(BarFullTextQuery query) { query.Keyword = WebUtility.UrlDecode(query.Keyword); query.PageSize = 20;//每页记录数 //默认搜公共贴吧(非群组)的帖子 query.TenantTypeId = TenantTypeIds.Instance().Bar(); //根据帖吧id查询帖吧名字 string barId = Request.QueryString["barId"]; if (barId != "" && barId != null) { long id = 0; if (long.TryParse(barId, out id)) { BarSection section = barSectionService.Get(id); if (section != null) { query.TenantTypeId = section.TenantTypeId; ViewData["barname"] = section.Name; ViewData["TenantTypeId"] = section.TenantTypeId; } } } //调用搜索器进行搜索 BarSearcher BarSearcher = (BarSearcher)SearcherFactory.GetSearcher(BarSearcher.CODE); PagingDataSet<BarEntity> BarEntities = BarSearcher.Search(query); //添加到用户搜索历史 IUser CurrentUser = UserContext.CurrentUser; if (CurrentUser != null) { SearchHistoryService searchHistoryService = new SearchHistoryService(); searchHistoryService.SearchTerm(CurrentUser.UserId, BarSearcher.CODE, query.Keyword); } //添加到热词 if (!string.IsNullOrWhiteSpace(query.Keyword)) { SearchedTermService searchedTermService = new SearchedTermService(); searchedTermService.SearchTerm(BarSearcher.CODE, query.Keyword); } //设置页面Meta if (string.IsNullOrWhiteSpace(query.Keyword)) { pageResourceManager.InsertTitlePart("帖吧搜索");//设置页面Title } else { pageResourceManager.InsertTitlePart('“' + query.Keyword + '”' + "的相关帖吧");//设置页面Title } pageResourceManager.SetMetaOfKeywords("帖吧搜索");//设置Keyords类型的Meta pageResourceManager.SetMetaOfDescription("帖吧搜索");//设置Description类型的Meta if (Request.IsAjaxRequest()) return View("_ListSearchThread", BarEntities); return View(BarEntities); }
/// <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> /// 微博分页搜索 /// </summary> /// <param name="query">搜索条件</param> /// <returns>符合搜索条件的分页集合</returns> public PagingDataSet <BarEntity> Search(BarFullTextQuery barQuery) { if (string.IsNullOrWhiteSpace(barQuery.Keyword)) { return(new PagingDataSet <BarEntity>(new List <BarEntity>())); } LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(barQuery); //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort Query query = null; Filter filter = null; Sort sort = null; searchBuilder.BuildQuery(out query, out filter, out sort); //调用SearchService.Search(),执行搜索 PagingDataSet <Document> searchResult = searchEngine.Search(query, filter, sort, barQuery.PageIndex, barQuery.PageSize); IEnumerable <Document> docs = searchResult.ToList <Document>(); //解析出搜索结果中的发帖ID List <long> barthreadIds = new List <long>(); //解析出搜索结果中的回帖ID List <long> barpostIds = new List <long>(); //获取索引中发帖的标签 Dictionary <long, List <string> > barTags = new Dictionary <long, List <string> >(); //获取帖吧名 Dictionary <long, string> barSectionNames = new Dictionary <long, string>(); //获取索引中回帖的标题 Dictionary <long, string> barPostSubjects = new Dictionary <long, string>(); foreach (Document doc in docs) { //是回帖 if (doc.Get(BarIndexDocument.IsPost) == "1") { long postId = long.Parse(doc.Get(BarIndexDocument.PostId)); barpostIds.Add(postId); string subject = doc.Get(BarIndexDocument.Subject); barPostSubjects[postId] = subject; } else { long threadId = long.Parse(doc.Get(BarIndexDocument.ThreadId)); barthreadIds.Add(threadId); //if (!string.IsNullOrWhiteSpace(doc.Get(BarIndexDocument.Tag))) //{ barTags[threadId] = doc.GetValues(BarIndexDocument.Tag).ToList <string>(); //} } long sectionId = long.Parse(doc.Get(BarIndexDocument.SectionId)); if (!barSectionNames.ContainsKey(sectionId)) { string sectionName = barSectionService.Get(sectionId).Name; barSectionNames[sectionId] = sectionName; } } //根据发帖ID列表批量查询发帖实例 IEnumerable <BarThread> barThreadList = barThreadService.GetBarThreads(barthreadIds); //根据回帖ID列表批量查询回帖实例 IEnumerable <BarPost> barPostList = barPostService.GetBarPosts(barpostIds); //组装帖子列表 List <BarEntity> barEntityList = new List <BarEntity>(); foreach (var barThread in barThreadList) { BarEntity barEntity = new BarEntity(); barEntity.SectionId = barThread.SectionId; barEntity.ThreadId = barThread.ThreadId; barEntity.PostId = 0; barEntity.Subject = barThread.Subject; barEntity.Body = barThread.GetBody(); barEntity.UserId = barThread.UserId; barEntity.Author = barThread.Author; if (barTags.Keys.Contains(barEntity.ThreadId)) { barEntity.Tag = barTags.Count == 0 ? new List <string>() : barTags[barEntity.ThreadId]; } else { barEntity.Tag = new List <string>(); } if (barSectionNames.Keys.Contains(barEntity.SectionId)) { barEntity.SectionName = barSectionNames.Count == 0 ? "" : barSectionNames[barEntity.SectionId]; } else { barEntity.SectionName = ""; } barEntity.DateCreated = barThread.DateCreated; barEntity.IsPost = false; barEntityList.Add(barEntity); } foreach (var barPost in barPostList) { BarEntity barEntity = new BarEntity(); barEntity.SectionId = barPost.SectionId; barEntity.ThreadId = barPost.ThreadId; barEntity.PostId = barPost.PostId; barEntity.Subject = barPostSubjects[barPost.PostId]; barEntity.UserId = barPost.UserId; barEntity.Body = barPost.GetBody(); barEntity.Author = barPost.Author; barEntity.Tag = null; if (barSectionNames.Keys.Contains(barEntity.SectionId)) { barEntity.SectionName = barSectionNames.Count == 0 ? "" : barSectionNames[barEntity.SectionId]; } else { barEntity.SectionName = ""; } barEntity.DateCreated = barPost.DateCreated; barEntity.IsPost = true; barEntityList.Add(barEntity); } //组装分页对象 PagingDataSet <BarEntity> barEntities = new PagingDataSet <BarEntity>(barEntityList.OrderByDescending(b => b.DateCreated)) { TotalRecords = searchResult.TotalRecords, PageSize = searchResult.PageSize, PageIndex = searchResult.PageIndex, QueryDuration = searchResult.QueryDuration }; return(barEntities); }
/// <summary> /// Ⱥ������������ /// </summary> /// <param name="spaceKey">Ⱥ����</param> /// <param name="keyword">�ؼ���</param> /// <param name="pageIndex">ҳ��</param> /// <returns>Ⱥ������������</returns> public ActionResult Search(string spaceKey, string keyword, BarSearchRange term = BarSearchRange.ALL, int pageIndex = 1) { long barSectionId = GroupIdToGroupKeyDictionary.GetGroupId(spaceKey); var group = groupService.Get(spaceKey); if (group == null) return HttpNotFound(); ViewData["group"] = group; BarSection section = barSectionService.Get(barSectionId); if (section == null) return HttpNotFound(); ViewData["section"] = section; BarFullTextQuery query = new BarFullTextQuery(); query.Term = term; query.PageIndex = pageIndex; query.PageSize = 20;//ÿҳ��¼�� query.Keyword = keyword; query.Range = section.SectionId.ToString(); //��ȡȺ�����ɵ����� query.TenantTypeId = TenantTypeIds.Instance().Group(); //��������id��ѯ�������� query.TenantTypeId = section.TenantTypeId; ViewData["barname"] = section.Name; ViewData["TenantTypeId"] = section.TenantTypeId; //������������������ BarSearcher BarSearcher = (BarSearcher)SearcherFactory.GetSearcher(BarSearcher.CODE); PagingDataSet<BarEntity> BarEntities = BarSearcher.Search(query); if (Request.IsAjaxRequest()) return View("~/Applications/Bar/Views/Bar/_ListSearchThread.cshtml", BarEntities); //����ҳ��Meta if (string.IsNullOrWhiteSpace(query.Keyword)) { pageResourceManager.InsertTitlePart("Ⱥ����������");//����ҳ��Title } else { pageResourceManager.InsertTitlePart('��' + query.Keyword + '��' + "���������");//����ҳ��Title } pageResourceManager.SetMetaOfKeywords("��������");//����Keyords���͵�Meta pageResourceManager.SetMetaOfDescription("��������");//����Description���͵�Meta return View(BarEntities); }
/// <summary> /// 群组内搜索帖子 /// </summary> /// <param name="spaceKey">群组名</param> /// <param name="keyword">关键字</param> /// <param name="pageIndex">页码</param> /// <returns>群组内搜索帖子</returns> public ActionResult Search(string spaceKey, string keyword, BarSearchRange term = BarSearchRange.ALL, int pageIndex = 1) { long barSectionId = TopicIdToTopicKeyDictionary.GetTopicId(spaceKey); var topic = topicService.Get(spaceKey); if (topic == null) return HttpNotFound(); ViewData["topic"] = topic; BarSection section = barSectionService.Get(barSectionId); if (section == null) return HttpNotFound(); ViewData["section"] = section; BarFullTextQuery query = new BarFullTextQuery(); query.Term = term; query.PageIndex = pageIndex; query.PageSize = 20;//每页记录数 query.Keyword = keyword; query.Range = section.SectionId.ToString(); //获取群组贴吧的帖子 query.TenantTypeId = TenantTypeIds.Instance().Topic(); //根据帖吧id查询帖吧名字 query.TenantTypeId = section.TenantTypeId; ViewData["barname"] = section.Name; ViewData["TenantTypeId"] = section.TenantTypeId; //调用搜索器进行搜索 BarSearcher BarSearcher = (BarSearcher)SearcherFactory.GetSearcher(BarSearcher.CODE); PagingDataSet<BarEntity> BarEntities = BarSearcher.Search(query); if (Request.IsAjaxRequest()) return View("~/Applications/Bar/Views/Bar/_ListSearchThread.cshtml", BarEntities); //设置页面Meta if (string.IsNullOrWhiteSpace(query.Keyword)) { pageResourceManager.InsertTitlePart("群组帖子搜索");//设置页面Title } else { pageResourceManager.InsertTitlePart('“' + query.Keyword + '”' + "的相关帖子");//设置页面Title } pageResourceManager.SetMetaOfKeywords("帖吧搜索");//设置Keyords类型的Meta pageResourceManager.SetMetaOfDescription("帖吧搜索");//设置Description类型的Meta return View(BarEntities); }