Two kinds of paging operations for Lucene

Source: Internet
Author: User

Lucene-based paging is available in two ways:

lucene3.5 before paging is provided in the way of re-query (all records each query, and then take some of the records, this way with the most), Lucene official explanation: Because we are fast enough. memory is prone to memory overflow when processing massive amounts of data.

lucene3.5 later provide a searchafter, this is in the large amount of data use (billion-level data volume), relatively slow, like Google search images, click More, and then out of a batch. The way to do this is to keep the data in the cache. And then go fetch it.
The following is the query part of the code:

/** * This is to query all the data first, then go to paging data     * Note that this way of handling massive amounts of data, easy memory Overflow * @param query * @param pageindex--page * @param pagesize--how much data per page */public void Searchpage (String query,int pageindex,int pageSize) {try {Directory dir = fileindexutils.getdirectory (); in Dexsearcher searcher = Getsearcher (dir); Queryparser parser = new Queryparser (version.lucene_35, "content", new StandardAnalyzer (version.lucene_35)); Query q = parser.parse (query); Topdocs TDS = Searcher.search (q, 500);//note here put 500 data in memory. scoredoc[] SDS = tds.scoredocs;int start = (pageIndex-1) *pagesize;int end = pageindex*pagesize;for (int i=start;i<end; i++) {Document doc = Searcher.doc (sds[i].doc); System.out.println (sds[i].doc+ ":" +doc.get ("path") + "--" +doc.get ("filename");} Searcher.close ();} catch (Org.apache.lucene.queryParser.ParseException e) {e.printstacktrace ();} catch (IOException e) { E.printstacktrace ();}}


Finally, let's take a look at paging using Searcherafter, the code is as follows (This method is not supported before lucene3.5 ):

/** * gets last Scoredocs * @param pageIndex * @param pageSize * @param query * @param searcher * @return * @thro based on page number and paging size WS IOException */private scoredoc getlastscoredoc (int pageindex,int pagesize,query Query,indexsearcher searcher) throws IOException {if (pageindex==1) return null;//If the first page returns an empty int num = pagesize* (pageIndex-1);//Gets the last number of previous page Topdocs TDS = Searcher.search (query, num); return tds.scoredocs[num-1];} public void Searchpagebyafter (String query,int pageindex,int pageSize) {try {Directory dir = fileindexutils.getdirectory (); Indexsearcher searcher = Getsearcher (dir); Queryparser parser = new Queryparser (version.lucene_35, "content", new StandardAnalyzer (version.lucene_35)); Query q = parser.parse (query);//Gets the last element of the previous page Scoredoc LASTSD = Getlastscoredoc (PageIndex, PageSize, Q, searcher);// Go through the last element to search for the next page of elements topdocs tds = Searcher.searchafter (Lastsd,q, pageSize); for (Scoredoc Sd:tds.scoreDocs) {Document doc = Searcher.doc (Sd.doc); System.out.println (sd.doc+ ":" +doc.get ("path") + "-" +doc.get ("FileName ")); Searcher.close ();} catch (Org.apache.lucene.queryParser.ParseException e) {e.printstacktrace ();} catch (IOException e) { E.printstacktrace ();}}



Two kinds of paging operations for Lucene

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.