分页条件查询并且获取数据,两种方法,Query和Query2是两种方法 另外我用的是最新的lucene.net 2.94版本的写法 是通过TopDocs2Data方法取最终数据的,相对以前版本好处是按需取数据,不用一次加载全部 using System; using System.Collections.Generic; using System.Linq; using System.Text; using XydSoft.Article.Entity; using Lucene.Net.Search; using Lucene.Net.Analysis; using Lucene.Net.QueryParsers; using Lucene.Net.Analysis.Standard; using Lucene.Net.Index; using XydSoft.Article.Common; using System.IO; using Lucene.Net.Documents; namespace XydSoft.Article.Luncene { /// <summary> /// 查询类 /// 重要 /// </summary> public class QueryIndexManager { /// <summary> /// 分页获取列表数据 /// </summary> /// <param name="tag"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="totalRec"></param> /// <returns></returns> public static List<ArticleInfo> Query(string tag, int pageIndex, int pageSize, out int totalRec) { QueryParser parser = new QueryParser("Title", Common.CurrentAnalyzer); Query query = parser.Parse(tag); Sort sort = new Sort(new SortField("AddDate", SortField.DOC, false)); TopDocs topDocs = Common.GenerateSearcher().Search(query, (Filter)null, pageIndex * pageSize, sort); totalRec = topDocs.TotalHits; if (pageIndex == 1) return TopDocs2Data(topDocs.ScoreDocs); return TopDocs2Data(topDocs.ScoreDocs, pageIndex, pageSize, totalRec); } private static List<DataSourceInfo> Query2(string tag, int pageIndex, int pageSize, out int totalRec) { Query query = new WildcardQuery(new Term("Title", "*" + tag + "*")); Sort sort = new Sort(new SortField("AddDate", SortField.AUTO, false)); IndexSearcher searcher = new IndexSearcher(XydSoft.Article.Common.Const.LuceneIndexPath, true); Hits hits = searcher.Search(query, sort); totalRec = hits.Length(); int start = (pageIndex - 1) * pageSize; int end = pageIndex * pageSize; if (end > totalRec) end = totalRec; List<DataSourceInfo> list = new List<DataSourceInfo>(); for (int index = start; index < end; index++) { DataSourceInfo dataSourceInfo = new DataSourceInfo(); dataSourceInfo.Title = hits.Doc(index).Get("Title"); dataSourceInfo.ID = hits.Doc(index).Get("ID").ToInteger(); dataSourceInfo.AddDate = hits.Doc(index).Get("AddDate").ToDateTime(); list.Add(dataSourceInfo); } return list; } #region 获取最终的数据 /// <summary> /// 获取最终的数据 /// </summary> /// <param name="scoreDoc"></param> /// <param name="pageIndex"></param> /// <param name="pageSize"></param> /// <param name="totalRec"></param> /// <returns></returns> private static List<ArticleInfo> TopDocs2Data(ScoreDoc[] scoreDoc, int pageIndex, int pageSize, int totalRec) { int start = (pageIndex - 1) * pageSize; int end = pageIndex * pageSize; if (end > totalRec) end = totalRec; List<ArticleInfo> list = new List<ArticleInfo>(); for (int index = start; index < end; index++) { Document doc = Common.GenerateSearcher().Doc(scoreDoc[index].doc); ArticleInfo articleInfo = new ArticleInfo(); articleInfo.Title = doc.Get("Title"); articleInfo.ID = doc.Get("ID").ToInteger(); articleInfo.AddDate = doc.Get("AddDate").ToDateTime(); list.Add(articleInfo); } return list; } /// <summary> /// 获取最终的数据 /// </summary> /// <param name="docs"></param> /// <returns></returns> private static List<ArticleInfo> TopDocs2Data(ScoreDoc[] docs) { if (docs == null || docs.Length == 0) return null; List<ArticleInfo> list = new List<ArticleInfo>(); foreach (ScoreDoc sd in docs) { Document doc = Common.GenerateSearcher().Doc(sd.doc); ArticleInfo articleInfo = new ArticleInfo(); articleInfo.Title = doc.Get("Title"); articleInfo.ID = doc.Get("ID").ToInteger(); articleInfo.AddDate = doc.Get("AddDate").ToDateTime(); list.Add(articleInfo); } return list; } #endregion } }