Lucene代碼備份之分頁條件查詢

分頁條件查詢並且獲取數據,兩種方法,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

 

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章