Ejemplo n.º 1
0
        /// <summary>
        /// 资讯分页搜索
        /// </summary>
        /// <param name="cmsQuery">搜索条件</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <ContentItem> Search(CmsFullTextQuery cmsQuery)
        {
            if (cmsQuery.ContentFolderId == 0)
            {
                if (string.IsNullOrWhiteSpace(cmsQuery.Keyword))
                {
                    return(new PagingDataSet <ContentItem>(new List <ContentItem>()));
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(cmsQuery);

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

            //解析出搜索结果中的资讯ID
            List <long> cmsContentItemIds = new List <long>();
            //获取索引中资讯的标签
            Dictionary <long, IEnumerable <string> > cmsTags = new Dictionary <long, IEnumerable <string> >();

            foreach (Document doc in docs)
            {
                long cmsContentItemId = long.Parse(doc.Get(CmsIndexDocument.ContentItemId));
                cmsContentItemIds.Add(cmsContentItemId);
                cmsTags[cmsContentItemId] = doc.GetValues(CmsIndexDocument.Tag).ToList <string>();
            }

            //根据资讯ID列表批量查询资讯实例
            IEnumerable <ContentItem> contentItemList = contentItemService.GetContentItems(cmsContentItemIds);

            foreach (var contentItem in contentItemList)
            {
                if (cmsTags.ContainsKey(contentItem.ContentItemId))
                {
                    contentItem.TagNames = cmsTags[contentItem.ContentItemId];
                }
            }

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

            return(contentItems);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 日志分页搜索
        /// </summary>
        /// <param name="WikiQuery">搜索条件</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <WikiPage> Search(WikiFullTextQuery WikiQuery)
        {
            if (WikiQuery.SiteCategoryId == 0)
            {
                if (string.IsNullOrWhiteSpace(WikiQuery.Keyword))
                {
                    return(new PagingDataSet <WikiPage>(new List <WikiPage>()));
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(WikiQuery);

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

            //解析出搜索结果中的日志ID
            List <long> WikiPageIds = new List <long>();
            //获取索引中百科的标签
            Dictionary <long, IEnumerable <string> > WikiTags = new Dictionary <long, IEnumerable <string> >();

            foreach (Document doc in docs)
            {
                long WikiPageId = long.Parse(doc.Get(WikiIndexDocument.PageId));
                WikiPageIds.Add(WikiPageId);
                WikiTags[WikiPageId] = doc.GetValues(WikiIndexDocument.Tag).ToList <string>();
            }

            //根据日志ID列表批量查询日志实例
            IEnumerable <WikiPage> WikiPageList = wikiService.GetWikiPages(WikiPageIds);

            foreach (var WikiPage in WikiPageList)
            {
                if (WikiTags.ContainsKey(WikiPage.PageId))
                {
                    WikiPage.TagNames = WikiTags[WikiPage.PageId];
                }
            }

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

            return(WikiPages);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 搜索我和TA共同的内容
        /// </summary>
        /// <param name="myUserId">我的用户ID</param>
        /// <param name="taUserId">TA的用户ID</param>
        public void SearchInterested(long myUserId, long taUserId, out IEnumerable <string> sameTagNames, out IEnumerable <string> sameCompanyNames, out IEnumerable <string> sameSchoolNames)
        {
            sameTagNames     = new List <string>();
            sameCompanyNames = new List <string>();
            sameSchoolNames  = new List <string>();

            //无效用户ID,直接返回空列表
            if (myUserId <= 0 || taUserId <= 0)
            {
                return;
            }

            //搜索出我和TA的Document,使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();

            searchBuilder.WithFields(UserIndexDocument.UserId, new List <string> {
                myUserId.ToString(), taUserId.ToString()
            });
            searchBuilder.BuildQuery(out query, out filter, out sort);

            IEnumerable <Document> docs = searchEngine.Search(query, filter, sort, 2);

            //应该返回两条Document,分别对应我和TA的UserId,否则直接返回空列表
            if (docs == null || docs.Count() != 2)
            {
                return;
            }

            string[] myTagNames     = docs.ElementAt(0).GetValues(UserIndexDocument.TagName);
            string[] taTagNames     = docs.ElementAt(1).GetValues(UserIndexDocument.TagName);
            string[] myCompanyNames = docs.ElementAt(0).GetValues(UserIndexDocument.CompanyName);
            string[] taCompanyNames = docs.ElementAt(1).GetValues(UserIndexDocument.CompanyName);
            string[] mySchoolNames  = docs.ElementAt(0).GetValues(UserIndexDocument.School);
            string[] taSchoolNames  = docs.ElementAt(1).GetValues(UserIndexDocument.School);

            //比较相同的内容
            sameTagNames     = myTagNames.Intersect(taTagNames);
            sameCompanyNames = myCompanyNames.Intersect(taCompanyNames);
            sameSchoolNames  = mySchoolNames.Intersect(taSchoolNames);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 微博分页搜索
        /// </summary>
        /// <param name="query">搜索条件</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <MicroblogEntity> Search(MicroblogFullTextQuery microBlogQuery)
        {
            if (microBlogQuery.Keywords == null && string.IsNullOrWhiteSpace(microBlogQuery.Keyword) && microBlogQuery.SearchTerm == MicroblogSearchTerm.ALL)
            {
                return(new PagingDataSet <MicroblogEntity>(new List <MicroblogEntity>()));
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(microBlogQuery);

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

            //解析出搜索结果中的微博ID
            List <long> microblogIds = new List <long>();

            foreach (Document doc in docs)
            {
                long microblogId = long.Parse(doc.Get(MicroblogIndexDocument.MicroblogId));
                microblogIds.Add(microblogId);
            }

            //根据微博ID列表批量查询微博实例
            IEnumerable <MicroblogEntity> microblogList = microBlogService.GetMicroblogs(microblogIds);

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

            return(microblogs);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 用户搜索
        /// </summary>
        /// <param name="userQuery">搜索条件</param>
        /// <returns>符合搜索条件的User分页集合</returns>
        public PagingDataSet <User> Search(UserFullTextQuery userQuery)
        {
            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(userQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;

            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchEngine.Search(),执行搜索
            PagingDataSet <Document> searchResult = searchEngine.Search(query, filter, sort, userQuery.PageIndex, userQuery.PageSize);
            IEnumerable <Document>   docs         = searchResult.ToList <Document>();

            //解析出搜索结果中的用户ID
            List <long> userIds = new List <long>();

            foreach (Document doc in docs)
            {
                long userId = long.Parse(doc.Get(UserIndexDocument.UserId));
                userIds.Add(userId);
            }

            //根据用户ID列表批量查询用户实例
            IEnumerable <User> userList = userService.GetFullUsers(userIds);

            //根据用户ID列表批量查询UserProfile实例,利用Repository的自动缓存机制,减少页面加载时的关联数据库查询次数
            //对于UserProfile的Tag、WorkExperience、EducationExperience,目前页面列表不显示,暂不处理
            userProfileService.GetUserProfiles(userIds);

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

            return(users);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 问答分页搜索
        /// </summary>
        /// <param name="askQuery">搜索条件</param>
        /// <returns>符合条件的分页集合</returns>
        public PagingDataSet <AskQuestion> Search(AskFullTextQuery askQuery)
        {
            if (string.IsNullOrWhiteSpace(askQuery.Keyword) && askQuery.IsRelation == false && askQuery.IsAlike == false)
            {
                return(new PagingDataSet <AskQuestion>(new List <AskQuestion>()));
            }
            LuceneSearchBuilder searchBuilder = BuilderLuceneSearchBuilder(askQuery);

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

            //问题ids
            List <long> questionIds = new List <long>();

            foreach (Document doc in docs)
            {
                long questionId = long.Parse(doc.Get(AskIndexDocument.QuestionId));
                questionIds.Add(questionId);
            }

            //根据ids批量获取问题实体
            IEnumerable <AskQuestion> questionList = askService.GetQuestions(questionIds);

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

            return(questions);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 标签搜索
        /// </summary>
        public IEnumerable <string> Search(string tagName, int topNumber, long ownerId = 0)
        {
            if (string.IsNullOrWhiteSpace(tagName))
            {
                return(new List <string>());
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(tagName, ownerId);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;

            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchEngine.Search(),执行搜索
            IEnumerable <Document> searchResult = searchEngine.Search(query, filter, sort, topNumber);

            return(searchResult.Select(d => d.Get(TagIndexDocument.TagName)));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 搜索我和TA共同关注的用户
        /// </summary>
        /// <param name="myUserId">我的用户ID</param>
        /// <param name="taUserId">TA的用户列表</param>
        /// <returns>我和TA共同关注的用户列表</returns>
        public IEnumerable <User> SearchInterestedWithFollows(long myUserId, long taUserId)
        {
            //无效用户ID,直接返回空列表
            if (myUserId <= 0 || taUserId <= 0)
            {
                return(new List <User>());
            }

            //搜索出我和TA的Document,使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();

            searchBuilder.WithFields(UserId, new List <string> {
                myUserId.ToString(), taUserId.ToString()
            });
            searchBuilder.BuildQuery(out query, out filter, out sort);

            IEnumerable <Document> docs = searchEngine.Search(query, filter, sort, 2);

            //应该返回两条Document,分别对应我和TA的UserId,否则直接返回空列表
            if (docs == null || docs.Count() != 2)
            {
                return(new List <User>());
            }

            string[] myFollowedUserIds = docs.ElementAt(0).GetValues(FollowedUserIds);
            string[] taFollowedUserIds = docs.ElementAt(1).GetValues(FollowedUserIds);

            //比较相同的关注用户
            IEnumerable <string> sameFollowedUserIds = myFollowedUserIds.Intersect(taFollowedUserIds);

            //批量查询“共同关注的用户”列表
            IEnumerable <User> sameFollowedUsers = userService.GetFullUsers(sameFollowedUserIds.Select(n => Convert.ToInt64(n)));

            return(sameFollowedUsers);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 自动完成搜索
        /// </summary>
        /// <param name="userQuery"></param>
        /// <returns></returns>
        public IEnumerable <User> AutoCompleteSearch(UserFullTextQuery userQuery)
        {
            if (string.IsNullOrWhiteSpace(userQuery.Keyword))
            {
                return(new List <User>());
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(userQuery);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;

            searchBuilder.BuildQuery(out query, out filter, out sort);

            //调用SearchService.Search(),执行搜索
            IEnumerable <Document> docs = searchEngine.Search(query, filter, sort, userQuery.PageSize);

            //解析出搜索结果中的用户ID
            List <long> userIds = new List <long>();

            foreach (Document doc in docs)
            {
                long userId = long.Parse(doc.Get(UserIndexDocument.UserId));
                userIds.Add(userId);
            }

            //根据用户ID列表批量查询用户实例
            IEnumerable <User> users = userService.GetFullUsers(userIds);

            //根据用户ID列表批量查询用户相关实例,利用Repository的自动缓存机制,减少页面加载时的关联数据库查询次数
            IEnumerable <UserProfile> userProfiles = userProfileService.GetUserProfiles(userIds);

            return(users);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 照片搜索
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public PagingDataSet <Photo> Search(PhotoFullTextQuery query)
        {
            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(query);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  photoQuery = null;
            Filter filter     = null;
            Sort   sort       = null;

            searchBuilder.BuildQuery(out photoQuery, out filter, out sort);

            //调用SearchEngine.Search(),执行搜索
            PagingDataSet <Document> searchResult = searchEngine.Search(photoQuery, filter, sort, query.PageIndex, query.PageSize);
            IEnumerable <Document>   docs         = searchResult.ToList <Document>();

            //解析出PhotoId,组装Photo实体
            List <long> photoIds = new List <long>();

            foreach (var doc in docs)
            {
                long photoId = long.Parse(doc.Get(PhotoIndexDocument.PhotoId));
                photoIds.Add(photoId);
            }
            IEnumerable <Photo> photoList = photoService.GetPhotos(photoIds);

            //组装分页集合
            PagingDataSet <Photo> photos = new PagingDataSet <Photo>(photoList)
            {
                TotalRecords  = searchResult.TotalRecords,
                PageIndex     = searchResult.PageIndex,
                PageSize      = searchResult.PageSize,
                QueryDuration = searchResult.QueryDuration
            };

            return(photos);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// 搜索共同关注的人
        /// </summary>
        /// <param name="userId">粉丝的用户ID</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="followedUserIdDic">每个相同关注用户中共同关注的用户ID列表</param>
        /// <param name="followedUserDic">每个共同关注的用户的ID与User的映射</param>
        /// <returns>符合搜索条件的User分页集合</returns>
        public PagingDataSet <User> SearchInterestedWithFollows(long userId, int pageIndex, int pageSize, out Dictionary <long, IEnumerable <long> > followedUserIdDic, out Dictionary <long, User> followedUserDic)
        {
            followedUserIdDic = new Dictionary <long, IEnumerable <long> >();
            followedUserDic   = new Dictionary <long, User>();

            if (userId <= 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            //先查询当前用户关注的人(包括“悄悄关注”的用户),此处需要调用数据库查询,因为索引中没有存储“是否悄悄关注”属性
            IEnumerable <long> myFollowedUserIds = followService.GetPortionFollowedUserIds(userId);

            if (myFollowedUserIds != null && myFollowedUserIds.Count() == 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            //黑名单用户
            IEnumerable <long> stopUserIds = new PrivacyService().GetStopedUsers(userId).Select(n => n.Key);

            //搜索“我”关注的人中包含“共同关注的人”的用户
            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();

            searchBuilder.WithFields(FollowedUserIds, myFollowedUserIds.Select(n => n.ToString()), true)
            .NotWithField(UserId, userId.ToString())                                         //排除掉当前用户
            .NotWithFields(UserId, myFollowedUserIds.Select(n => n.ToString()))              //排除掉已关注用户
            .NotWithFields(UserIndexDocument.UserId, stopUserIds.Select(n => n.ToString())); //排除掉黑名单用户
            searchBuilder.BuildQuery(out query, out filter, out sort);

            PagingDataSet <Document> searchResult = searchEngine.Search(query, filter, sort, pageIndex, pageSize);
            IEnumerable <Document>   docs         = searchResult.ToList <Document>();

            if (docs == null || docs.Count() == 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            List <long> sameFollowedUserIdList = new List <long>();

            //解析出搜索结果中的用户ID
            List <long> followerUserIds = new List <long>();

            foreach (Document doc in docs)
            {
                long followerUserId = long.Parse(doc.Get(UserId));
                followerUserIds.Add(followerUserId);

                //“我”关注的人关注的人
                string[] followedUserIds = doc.GetValues(FollowedUserIds);

                //比较获取“共同关注的人”
                IEnumerable <long> sameFollowedUserIds = myFollowedUserIds.Intersect <long>(followedUserIds.Select(n => Convert.ToInt64(n)));
                if (!followedUserIdDic.ContainsKey(followerUserId))
                {
                    followedUserIdDic.Add(followerUserId, sameFollowedUserIds);
                }

                sameFollowedUserIdList.AddRange(sameFollowedUserIds);
            }

            //批量查询“共同关注的用户”列表
            IEnumerable <User> followerUserList = userService.GetFullUsers(followerUserIds).Where(n => n.IsCanbeFollow == true && n.IsActivated == true && n.IsBanned == false);

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

            //批量查询“共同关注的用户”关注的“共同关注用户”列表
            IEnumerable <User> sameFollowedUserList = userService.GetFullUsers(sameFollowedUserIdList.Distinct());

            followedUserDic = sameFollowedUserList.ToDictionary(n => n.UserId);

            return(users);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
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);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// 日志分页搜索
        /// </summary>
        /// <param name="blogQuery">搜索条件</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <BlogThread> Search(BlogFullTextQuery blogQuery)
        {
            if (blogQuery.SiteCategoryId == 0 && blogQuery.LoginId == 0 && blogQuery.UserId == 0)
            {
                if (string.IsNullOrWhiteSpace(blogQuery.Keyword) && blogQuery.IsRelationBlog == false)
                {
                    return(new PagingDataSet <BlogThread>(new List <BlogThread>()));
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(blogQuery);

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

            //解析出搜索结果中的日志ID
            List <long> blogThreadIds = new List <long>();
            //获取索引中日志的标签
            Dictionary <long, IEnumerable <string> > blogTags = new Dictionary <long, IEnumerable <string> >();
            //获取索引中日志的用户分类名
            Dictionary <long, IEnumerable <string> > blogOwnerCategoryNames = new Dictionary <long, IEnumerable <string> >();

            foreach (Document doc in docs)
            {
                long blogThreadId = long.Parse(doc.Get(BlogIndexDocument.ThreadId));
                blogThreadIds.Add(blogThreadId);
                blogTags[blogThreadId] = doc.GetValues(BlogIndexDocument.Tag).ToList <string>();
                blogOwnerCategoryNames[blogThreadId] = doc.GetValues(BlogIndexDocument.OwnerCategoryName).ToList <string>();
            }

            //根据日志ID列表批量查询日志实例
            IEnumerable <BlogThread> blogThreadList = blogThreadService.GetBlogThreads(blogThreadIds);

            foreach (var blogThread in blogThreadList)
            {
                blogThread.Body = blogThread.GetBody();
                if (blogTags.ContainsKey(blogThread.ThreadId))
                {
                    blogThread.TagNames = blogTags[blogThread.ThreadId];
                }
                if (blogOwnerCategoryNames.ContainsKey(blogThread.ThreadId))
                {
                    blogThread.OwnerCategoryNames = blogOwnerCategoryNames[blogThread.ThreadId];
                }
            }

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

            return(blogThreads);
        }
Ejemplo n.º 15
0
        /// <summary>
        /// 搜索“毕业于同一学校”的用户/// </summary>
        /// <param name="userId">当前用户的ID</param>
        /// <param name="pageIndex">分页页码</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="schoolNameDic">存储用户ID到学校名称列表的映射,用于页面列表输出</param>
        /// <returns></returns>
        public PagingDataSet <User> SearchInterestedWithSchools(long userId, int pageIndex, int pageSize, out Dictionary <long, IEnumerable <string> > schoolNameDic)
        {
            //Dictionary,用于页面列表输出
            schoolNameDic = new Dictionary <long, IEnumerable <string> >();

            //无效用户ID,直接返回空列表
            if (userId <= 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            Query  query  = null;
            Filter filter = null;
            Sort   sort   = null;

            //先搜索出当前用户的学校名称
            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();

            searchBuilder.WithField(UserIndexDocument.UserId, userId.ToString(), true);
            searchBuilder.BuildQuery(out query, out filter, out sort);

            IEnumerable <Document> docs = searchEngine.Search(query, filter, sort, 1);

            //索引中无此用户,直接返回空列表
            if (docs == null || docs.Count() == 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            string[] mySchoolNames = docs.First <Document>().GetValues(UserIndexDocument.School);

            //当前用户无学校,直接返回空列表
            if (mySchoolNames != null && mySchoolNames.Count() == 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            //查找有相同学校名称的用户
            //先查询当前用户关注的人(包括“悄悄关注”的用户),此处需要调用数据库查询,因为索引中没有存储“是否悄悄关注”属性
            IEnumerable <long> myFollowedUserIds = followService.GetPortionFollowedUserIds(userId);
            //黑名单用户
            IEnumerable <long> stopUserIds = new PrivacyService().GetStopedUsers(userId).Select(n => n.Key);

            //使用LuceneSearchBuilder构建Lucene需要Query、Filter、Sort
            searchBuilder = new LuceneSearchBuilder(1);
            //搜索条件需要排除掉当前用户本身
            searchBuilder.WithPhrases(UserIndexDocument.School, mySchoolNames.Distinct().Take(3).ToList())
            .NotWithField(UserIndexDocument.UserId, userId.ToString())                            //排除掉当前用户
            .NotWithFields(UserIndexDocument.UserId, myFollowedUserIds.Select(n => n.ToString())) //排除掉已关注用户
            .NotWithFields(UserIndexDocument.UserId, stopUserIds.Select(n => n.ToString()));      //排除掉黑名单用户
            searchBuilder.BuildQuery(out query, out filter, out sort);

            PagingDataSet <Document> searchResult = searchEngine.Search(query, filter, sort, pageIndex, pageSize);

            docs = searchResult.ToList <Document>();

            //如果没有使用相同学校名称的用户,直接返回空列表
            if (docs == null || docs.Count() == 0)
            {
                return(new PagingDataSet <User>(new List <User>()));
            }

            //“相同学校名称”的用户ID列表
            List <long> sameUserIds = new List <long>();

            foreach (Document doc in docs)
            {
                //比较获取“相同的学校名称”
                List <string> sameSchoolNames = new List <string>();
                string[]      schoolNames     = doc.GetValues(UserIndexDocument.School);
                foreach (string mySchoolName in mySchoolNames)
                {
                    foreach (string schoolName in schoolNames)
                    {
                        if (schoolName.Equals(mySchoolName) || schoolName.Contains(mySchoolName))
                        {
                            sameSchoolNames.Add(mySchoolName);
                            break;
                        }
                    }
                }

                long sameUserId = long.Parse(doc.Get(UserIndexDocument.UserId));
                sameUserIds.Add(sameUserId);

                //加入Dictionary,用于页面列表输出
                if (!schoolNameDic.ContainsKey(sameUserId))
                {
                    schoolNameDic.Add(sameUserId, sameSchoolNames);
                }
            }

            //批量查询“相同学校名称”的用户列表
            IEnumerable <User> sameUsers = userService.GetFullUsers(sameUserIds).Where(n => n.IsCanbeFollow == true && n.IsActivated == true && n.IsBanned == false);

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

            return(users);
        }
        /// <summary>
        /// 日志分页搜索
        /// </summary>
        /// <param name="journalQuery">搜索条件</param>
        /// <returns>符合搜索条件的分页集合</returns>
        public PagingDataSet <Journal> Search(JournalFullTextQuery journalQuery)
        {
            if (journalQuery.SiteCategoryId == 0 && journalQuery.LoginId == 0 && journalQuery.UserId == 0)
            {
                if (string.IsNullOrWhiteSpace(journalQuery.Keyword) && journalQuery.IsRelationJournal == false)
                {
                    return(new PagingDataSet <Journal>(new List <Journal>()));
                }
            }

            LuceneSearchBuilder searchBuilder = BuildLuceneSearchBuilder(journalQuery);

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

            //解析出搜索结果中的日志ID
            List <long> journalIds = new List <long>();
            //获取索引中日志的标签
            Dictionary <long, IEnumerable <string> > journalTags = new Dictionary <long, IEnumerable <string> >();
            //获取索引中日志的用户分类名
            Dictionary <long, IEnumerable <string> > journalOwnerCategoryNames = new Dictionary <long, IEnumerable <string> >();

            foreach (Document doc in docs)
            {
                long journalid = long.Parse(doc.Get(JournalIndexDocument.id));
                journalIds.Add(journalid);
                //journalTags[journalid] = doc.GetValues(JournalIndexDocument.Tag).ToList<string>();
                //journalOwnerCategoryNames[journalid] = doc.GetValues(JournalIndexDocument.OwnerCategoryName).ToList<string>();
            }

            //根据日志ID列表批量查询日志实例
            IEnumerable <Journal> journalList = journalService.GetJournalsByIds(journalIds);

            /*foreach (var journal in journalList)
             * {
             *  journal.Body = journal.GetBody();
             *  if (blogTags.ContainsKey(journal.ThreadId))
             *  {
             *      journal.TagNames = blogTags[journal.ThreadId];
             *  }
             *  if (journalOwnerCategoryNames.ContainsKey(journal.ThreadId))
             *  {
             *      journal.OwnerCategoryNames = journalOwnerCategoryNames[journal.ThreadId];
             *  }
             * }
             */
            //组装分页对象
            PagingDataSet <Journal> journals = new PagingDataSet <Journal>(journalList)
            {
                TotalRecords  = searchResult.TotalRecords,
                PageSize      = searchResult.PageSize,
                PageIndex     = searchResult.PageIndex,
                QueryDuration = searchResult.QueryDuration
            };

            return(journals);
        }