Ejemplo n.º 1
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.º 2
0
        /// <summary>
        /// 根据帖吧搜索查询条件构建Lucene查询条件
        /// </summary>
        /// <param name="Query"></param>
        /// <returns></returns>
        private LuceneSearchBuilder BuildLuceneSearchBuilder(WikiFullTextQuery WikiQuery)
        {
            LuceneSearchBuilder searchBuilder = new LuceneSearchBuilder();
            //范围
            Dictionary <string, BoostLevel> fieldNameAndBoosts = new Dictionary <string, BoostLevel>();

            if (!string.IsNullOrEmpty(WikiQuery.Keyword))
            {
                switch (WikiQuery.Range)
                {
                case WikiSearchRange.Title:
                    searchBuilder.WithPhrase(WikiIndexDocument.Title, WikiQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case WikiSearchRange.Category:
                    searchBuilder.WithPhrase(WikiIndexDocument.CategoryId, WikiQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case WikiSearchRange.AUTHOR:
                    searchBuilder.WithPhrase(WikiIndexDocument.Author, WikiQuery.Keyword, BoostLevel.Hight, false);
                    break;

                case WikiSearchRange.TAG:
                    searchBuilder.WithPhrase(WikiIndexDocument.Tag, WikiQuery.Keyword, BoostLevel.Hight, false);
                    break;

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

            //某个站点分类
            if (WikiQuery.SiteCategoryId != 0)
            {
                searchBuilder.WithField(WikiIndexDocument.CategoryId, WikiQuery.SiteCategoryId.ToString(), true, BoostLevel.Hight, true);
            }

            return(searchBuilder);
        }