Hibernate-search Site Search Page Solution

Source: Internet
Author: User

First look at the core class of paging

Package go.derek.util;
Import java.io.Serializable;
Import java.util.ArrayList;

Import java.util.List;

    public class Pager implements Serializable {private static final long serialversionuid = 2875790409419632498L;
    
    private static final int page_default_limit = 20;
    
    private static final int page_bar_size = 9;
    
    Private String query;

    private int currentpage;
    
    private int totalcount;
    
    private int totalpage;
    
    private int pagelimit;
    
    Private Boolean hasnextpage;
    
    Private Boolean hasprevpage;

    Private list<string> pagebarlist = new arraylist<string> ();
    Public Pager (int currentpage) {this.currentpage = CurrentPage;
        } public void Settotalcount (int totalcount) {if (totalcount <= 0) {return;

        } this.totalcount = TotalCount;
        if (this.pagelimit <= 0) {this.pagelimit = Page_default_limit; } if (this.tOtalcount% This.pagelimit = = 0) {this.totalpage = This.totalcount/this.pagelimit;
        } else {this.totalpage = this.totalcount/this.pagelimit + 1;
        } if (This.currentpage > This.totalpage) {this.currentpage = This.totalpage;
        } else if (this.currentpage <= 0) {this.currentpage = 1;

        } this.setpagebar ();
        This.hasprevpage = this.currentpage! = 1;
        
    This.hasnextpage = this.currentpage! = this.totalpage;  }//Set the paging button under the page public void Setpagebar () {if (this.totalpage <= page_bar_size) {for (int i = 1; I <= this.totalpage;
            i++) {Pagebarlist.add (integer.tostring (i)); 
                }} else if (This.currentpage <= page_bar_size/2) {for (int i = 1; I <= page_bar_size; i++) {
            Pagebarlist.add (integer.tostring (i)); }} else if (this.currentpage >= thIs.totalpage-(page_bar_size-1)/2) {for (int i = this.totalpage-page_bar_size + 1; I <= This.totalp Age
            i++) {Pagebarlist.add (integer.tostring (i)); }} else {for (int i = THIS.CURRENTPAGE-PAGE_BAR_SIZE/2; I < this.currentpage-page_bar_size /2 + page_bar_size;
            i++) {Pagebarlist.add (integer.tostring (i)); }}}//Omit setter and getter methods for each property

And then look at the core code of the search.

QueryBuilder QueryBuilder = Fulltextsession.getsearchfactory (). Buildquerybuilder (). forentity (Question.class). Get ( );
		Here you modify the field you want to query from query		
		lucenequery = Querybuilder.keyword (). Onfields ("title", "Content"). Matching (queryString). CreateQuery ();
		FullTextQuery fulltextquery = Fulltextsession.createfulltextquery (Lucenequery, question.class);
		Pager.settotalcount (Fulltextquery.getresultsize ());
		Pager.setquery (queryString);		
		Fulltextquery.setfirstresult ((Pager.getcurrentpage ()-1) * PAGER.GETPAGELIMIT ());
		Fulltextquery.setmaxresults (Pager.getpagelimit ());
		list<question> rslist = Fulltextquery.list ();	

where the Fulltextquery.getresultsize () method can get the total amount of the result set, if the cluster is used, the return is an approximate, probably not the most accurate number, if there is only one server, then the number returned must be accurate.

Then Setfirstresult () and Setmaxresult () set the starting subscript for each page, and the number of records to display on each page, respectively.

The following call to the list () method, the return is just that page results, that is, the pager class inside the set of 20, and then when you click on the next page, will search for 20, and so on.

This is very efficient when the amount of data is very large. Assuming that the table has thousands in accordance with the keyword records, all of a sudden put into a list inside, the results can be imagined.

The above is a relatively simple and efficient search paging scheme.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.