Ejemplo n.º 1
0
        /// <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>
        /// <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;
        }
Ejemplo n.º 3
0
        //fixed by wanf
        /// <summary>
        /// 专题分页搜索
        /// </summary>
        /// <param name="groupQuery">搜索条件</param>
        /// <param name="interestTopic">是否是查询可能感兴趣的专题</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <TopicEntity> Search(TopicFullTextQuery groupQuery, bool interestTopic = false)
        {
            if (!interestTopic)
            {
                if (string.IsNullOrWhiteSpace(groupQuery.Keyword) && !groupQuery.KeywordIsNull)
                {
                    return(new PagingDataSet <TopicEntity>(new List <TopicEntity>()));
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(groupQuery, interestTopic);

            //使用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, groupQuery.PageIndex, groupQuery.PageSize);
            IEnumerable <Document>   docs         = searchResult.ToList <Document>();

            //解析出搜索结果中的专题ID
            List <long> groupIds = new List <long>();
            //获取索引中专题的标签
            Dictionary <long, IEnumerable <string> > groupTags = new Dictionary <long, IEnumerable <string> >();
            //获取索引中专题的分类名
            Dictionary <long, string> categoryNames = new Dictionary <long, string>();

            foreach (Document doc in docs)
            {
                long groupId = long.Parse(doc.Get(TopicIndexDocument.TopicId));
                groupIds.Add(groupId);
                groupTags[groupId]     = doc.GetValues(TopicIndexDocument.Tag).ToList <string>();
                categoryNames[groupId] = doc.Get(TopicIndexDocument.CategoryName);
            }

            //根据专题ID列表批量查询专题实例
            IEnumerable <TopicEntity> groupList = topicService.GetTopicEntitiesByIds(groupIds);

            foreach (var group in groupList)
            {
                if (groupTags.ContainsKey(group.TopicId))
                {
                    group.TagNames = groupTags[group.TopicId];
                }
                if (categoryNames.ContainsKey(group.TopicId))
                {
                    group.CategoryName = categoryNames[group.TopicId];
                }
            }

            //组装分页对象
            PagingDataSet <TopicEntity> TopicEntitys = new PagingDataSet <TopicEntity>(groupList)
            {
                TotalRecords  = searchResult.TotalRecords,
                PageSize      = searchResult.PageSize,
                PageIndex     = searchResult.PageIndex,
                QueryDuration = searchResult.QueryDuration
            };

            return(TopicEntitys);
        }
        //fixed by wanf
        /// <summary>
        /// 专题分页搜索
        /// </summary>
        /// <param name="groupQuery">搜索条件</param>
        /// <param name="interestTopic">是否是查询可能感兴趣的专题</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet<TopicEntity> Search(TopicFullTextQuery groupQuery, bool interestTopic = false)
        {
            if (!interestTopic)
            {
                if (string.IsNullOrWhiteSpace(groupQuery.Keyword) && !groupQuery.KeywordIsNull)
                {
                    return new PagingDataSet<TopicEntity>(new List<TopicEntity>());
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(groupQuery, interestTopic);

            //使用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, groupQuery.PageIndex, groupQuery.PageSize);
            IEnumerable<Document> docs = searchResult.ToList<Document>();

            //解析出搜索结果中的专题ID
            List<long> groupIds = new List<long>();
            //获取索引中专题的标签
            Dictionary<long, IEnumerable<string>> groupTags = new Dictionary<long, IEnumerable<string>>();
            //获取索引中专题的分类名
            Dictionary<long, string> categoryNames = new Dictionary<long, string>();

            foreach (Document doc in docs)
            {
                long groupId = long.Parse(doc.Get(TopicIndexDocument.TopicId));
                groupIds.Add(groupId);
                groupTags[groupId]=doc.GetValues(TopicIndexDocument.Tag).ToList<string>();
                categoryNames[groupId]=doc.Get(TopicIndexDocument.CategoryName);
            }

            //根据专题ID列表批量查询专题实例
            IEnumerable<TopicEntity> groupList = topicService.GetTopicEntitiesByIds(groupIds);

            foreach (var group in groupList)
            {
                if (groupTags.ContainsKey(group.TopicId))
                {
                    group.TagNames = groupTags[group.TopicId];
                }
                if (categoryNames.ContainsKey(group.TopicId))
                {
                    group.CategoryName = categoryNames[group.TopicId];
                }
            }

            //组装分页对象
            PagingDataSet<TopicEntity> TopicEntitys = new PagingDataSet<TopicEntity>(groupList)
            {
                TotalRecords = searchResult.TotalRecords,
                PageSize = searchResult.PageSize,
                PageIndex = searchResult.PageIndex,
                QueryDuration = searchResult.QueryDuration
            };

            return TopicEntitys;
        }
 public ActionResult _InterestTopic()
 {
     TagService tagService = new TagService(TenantTypeIds.Instance().User());
     IUser currentUser = UserContext.CurrentUser;
     if (currentUser != null)
     {
         TopicFullTextQuery query = new TopicFullTextQuery();
         query.PageSize = 20;
         query.PageIndex = 1;
         query.Range = TopicSearchRange.TAG;
         query.Tags = tagService.GetTopTagsOfItem(currentUser.UserId, 100).Select(n => n.TagName);
         query.TopicIds = topicService.GetMyJoinedTopics(currentUser.UserId, 100, 1).Select(n => n.TopicId.ToString());
         //调用搜索器进行搜索
         TopicSearcher TopicSearcher = (TopicSearcher)SearcherFactory.GetSearcher(TopicSearcher.CODE);
         IEnumerable<TopicEntity> topicsTag = null;
         if (TopicSearcher.Search(query, true).Count == 0)
         {
             return View();
         }
         else
         {
             topicsTag = TopicSearcher.Search(query, true).AsEnumerable<TopicEntity>();
         }
         if (topicsTag.Count() < 20)
         {
             IEnumerable<TopicEntity> topicsFollow = topicService.FollowedUserAlsoJoinedTopics(currentUser.UserId, 20 - topicsTag.Count());
             return View(topicsTag.Union(topicsFollow));
         }
         else
         {
             return View(topicsTag);
         }
     }
     else
     {
         return View();
     }
 }
        /// <summary>
        /// 专题快捷搜索
        /// </summary>
        public ActionResult _QuickSearch(TopicFullTextQuery query, int topNumber)
        {
            query.PageSize = topNumber;//每页记录数
            query.PageIndex = 1;
            query.Range = TopicSearchRange.TOPICNAME;
            query.Keyword = Server.UrlDecode(query.Keyword);
            //调用搜索器进行搜索
            TopicSearcher TopicSearcher = (TopicSearcher)SearcherFactory.GetSearcher(TopicSearcher.CODE);
            PagingDataSet<TopicEntity> topics = TopicSearcher.Search(query);

            return PartialView(topics);
        }
        /// <summary>
        /// 专题全局搜索
        /// </summary>
        public ActionResult _GlobalSearch(TopicFullTextQuery query, int topNumber)
        {
            query.PageSize = topNumber;//每页记录数
            query.PageIndex = 1;

            //调用搜索器进行搜索
            TopicSearcher topicSearcher = (TopicSearcher)SearcherFactory.GetSearcher(TopicSearcher.CODE);
            PagingDataSet<TopicEntity> topics = topicSearcher.Search(query);

            return PartialView(topics);
        }
        /// <summary>
        /// 专题搜索
        /// </summary>
        public ActionResult Search(TopicFullTextQuery query)
        {
            query.Keyword = WebUtility.UrlDecode(query.Keyword);
            query.PageSize = 20;//每页记录数

            //调用搜索器进行搜索
            TopicSearcher topicSearcher = (TopicSearcher)SearcherFactory.GetSearcher(TopicSearcher.CODE);
            PagingDataSet<TopicEntity> topics = topicSearcher.Search(query);

            //添加到用户搜索历史 
            IUser CurrentUser = UserContext.CurrentUser;
            if (CurrentUser != null)
            {
                if (!string.IsNullOrWhiteSpace(query.Keyword))
                {
                    SearchHistoryService searchHistoryService = new SearchHistoryService();
                    searchHistoryService.SearchTerm(CurrentUser.UserId, TopicSearcher.CODE, query.Keyword);
                }
            }

            //添加到热词
            if (!string.IsNullOrWhiteSpace(query.Keyword))
            {
                SearchedTermService searchedTermService = new SearchedTermService();
                searchedTermService.SearchTerm(TopicSearcher.CODE, query.Keyword);
            }

            //设置页面Meta
            if (string.IsNullOrWhiteSpace(query.Keyword))
            {
                pageResourceManager.InsertTitlePart("专题搜索");//设置页面Title
            }
            else
            {
                pageResourceManager.InsertTitlePart('“' + query.Keyword + '”' + "的相关专题");//设置页面Title
            }

            return View(topics);
        }