Пример #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);
        }
Пример #2
0
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(CmsFullTextQuery cmsQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            //范围
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            if (!string.IsNullOrEmpty(cmsQuery.Keyword))
            {
                switch (cmsQuery.Range)
                {
                case CmsSearchRange.TITLE:
                    searchBuilder.WithPhrase(CmsIndexDocument.Title, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case CmsSearchRange.BODY:
                    searchBuilder.WithPhrase(CmsIndexDocument.Body, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case CmsSearchRange.AUTHOR:
                    searchBuilder.WithPhrase(CmsIndexDocument.Author, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case CmsSearchRange.TAG:
                    searchBuilder.WithPhrase(CmsIndexDocument.Tag, cmsQuery.Keyword, BoostLevel.Hight, false);
                    break;

                default:
                    fieldNameAndBoosts.Add(CmsIndexDocument.Title, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Summary, BoostLevel.Hight);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Tag, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Body, BoostLevel.Medium);
                    fieldNameAndBoosts.Add(CmsIndexDocument.Author, BoostLevel.Low);
                    searchBuilder.WithPhrases(fieldNameAndBoosts, cmsQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                    break;
                }
            }

            //某个栏目
            if (cmsQuery.ContentFolderId > 0)
            {
                searchBuilder.WithField(CmsIndexDocument.ContentFolderId, cmsQuery.ContentFolderId.ToString(), true, BoostLevel.Hight, true);
            }

            //过滤可以显示的资讯
            switch (publiclyAuditStatus)
            {
            case PubliclyAuditStatus.Again:
            case PubliclyAuditStatus.Fail:
            case PubliclyAuditStatus.Pending:
            case PubliclyAuditStatus.Success:
                searchBuilder.WithField(CmsIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                break;

            case PubliclyAuditStatus.Again_GreaterThanOrEqual:
            case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                searchBuilder.WithinRange(CmsIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                break;
            }

            return(searchBuilder);
        }
Пример #3
0
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(CmsFullTextQuery cmsQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            //范围
            Dictionary<string, BoostLevel> fieldNameAndBoosts = new Dictionary<string, BoostLevel>();

            if (!string.IsNullOrEmpty(cmsQuery.Keyword))
            {
                switch (cmsQuery.Range)
                {
                    case CmsSearchRange.TITLE:
                        searchBuilder.WithPhrase(CmsIndexDocument.Title, cmsQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case CmsSearchRange.BODY:
                        searchBuilder.WithPhrase(CmsIndexDocument.Body, cmsQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case CmsSearchRange.AUTHOR:
                        searchBuilder.WithPhrase(CmsIndexDocument.Author, cmsQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    case CmsSearchRange.TAG:
                        searchBuilder.WithPhrase(CmsIndexDocument.Tag, cmsQuery.Keyword, BoostLevel.Hight, false);
                        break;
                    default:
                        fieldNameAndBoosts.Add(CmsIndexDocument.Title, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(CmsIndexDocument.Summary, BoostLevel.Hight);
                        fieldNameAndBoosts.Add(CmsIndexDocument.Tag, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(CmsIndexDocument.Body, BoostLevel.Medium);
                        fieldNameAndBoosts.Add(CmsIndexDocument.Author, BoostLevel.Low);
                        searchBuilder.WithPhrases(fieldNameAndBoosts, cmsQuery.Keyword, BooleanClause.Occur.SHOULD, false);
                        break;
                }
            }

            //某个栏目
            if (cmsQuery.ContentFolderId > 0)
            {
                searchBuilder.WithField(CmsIndexDocument.ContentFolderId, cmsQuery.ContentFolderId.ToString(), true, BoostLevel.Hight, true);
            }

            //过滤可以显示的资讯
            switch (publiclyAuditStatus)
            {
                case PubliclyAuditStatus.Again:
                case PubliclyAuditStatus.Fail:
                case PubliclyAuditStatus.Pending:
                case PubliclyAuditStatus.Success:
                    searchBuilder.WithField(CmsIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), true, BoostLevel.Hight, true);
                    break;
                case PubliclyAuditStatus.Again_GreaterThanOrEqual:
                case PubliclyAuditStatus.Pending_GreaterThanOrEqual:
                    searchBuilder.WithinRange(CmsIndexDocument.AuditStatus, ((int)publiclyAuditStatus).ToString(), ((int)PubliclyAuditStatus.Success).ToString(), true);
                    break;
            }

            return searchBuilder;
        }
Пример #4
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;
        }