lucenenet-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Leonardo Azize Martins <laz...@gmail.com>
Subject TopDocs for pagging
Date Mon, 24 May 2010 17:20:54 GMT
Hi,

How about code below? Would be a contrib?


    public class PaggedTopDocs
    {
        private TopDocs hits;
        public PaggedTopDocs(Searcher searcher, Query query, int
currentPage, int documentsPerPage)
        {
            this.CurrentPage = currentPage;
            this.DocumentsPerPage = documentsPerPage;
            hits = searcher.Search(query, documentsPerPage * currentPage);
        }
        public int CurrentPage { get; private set; }
        public int DocumentsPerPage { get; private set; }

        public int Total
        {
            get { return hits.totalHits; }
        }
        public int Pages
        {
            get { return (int)Math.Ceiling((double)hits.totalHits /
(double)DocumentsPerPage); }
        }
        public bool HasPreview
        {
            get { return (CurrentPage > 1); }
        }
        public bool HasNext
        {
            get { return (CurrentPage < Pages); }
        }
        public ScoreDoc[] GetDocuments()
        {
            List<ScoreDoc> docs = new List<ScoreDoc>(this.DocumentsPerPage);
            int start = (DocumentsPerPage * CurrentPage) - DocumentsPerPage;
            int end = (DocumentsPerPage * CurrentPage) > Total ? Total :
(DocumentsPerPage * CurrentPage);
            for (int i = start; i < end; i++)
            {
                docs.Add(hits.scoreDocs[i]);
            }
            return docs.ToArray();
        }
    }
Regards,
Leo

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message