Esempio n. 1
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);
        }