Ejemplo n.º 1
0
        /// <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);
        }