Examples of Lucene collector

Source: Internet
Author: User

Lucene's collector is an advanced function that involves detailed steps in the search process. By customizing collector, you can change the default search, or collect information about hit documents at this stage.

 

The following example shows how to customize a collector and how to collect data in a custom collector.

 

Package test; <br/> Import Java. io. ioexception; <br/> Import Java. util. map; <br/> Import Java. util. set; <br/> Import Java. util. treemap; <br/> Import org.apache.e.doc ument. document; <br/> Import org.apache.e.doc ument. field; <br/> Import org.apache.e.doc ument. field. index; <br/> Import org.apache.e.doc ument. field. store; <br/> Import Org. apache. lucene. index. corruptindexexception; <br/> Import Org. apache. lucene. index. indexreader; <br/> Import Org. apache. lucene. index. indexwriter; <br/> Import Org. apache. lucene. search. collector; <br/> Import Org. apache. lucene. search. hitcollector; <br/> Import Org. apache. lucene. search. indexsearcher; <br/> Import Org. apache. lucene. search. matchalldocsquery; <br/> Import Org. apache. lucene. search. parallelmultisearcher; <br/> Import Org. apache. lucene. search. rangefilter; <br/> Import Org. apache. lucene. search. scorer; <br/> Import Org. apache. lucene. search. searcher; <br/> Import Org. apache. lucene. store. lockobtainfailedexception; <br/> Import Org. apache. lucene. store. ramdirectory; <br/> Import Casia. isiteam. plgroup. indexer. commons. analyzerfactory; <br/>/** <br/> * Custom collector <br/> * @ author cn.mingyuan@foxmail.com <br/> */<br/> class mycollector extends collector {<br/> indexreader reader = NULL; // reader reads the map that obtains the document <br/> // the collected information <br/> Public Map <integer, integer> map = new treemap <integer, integer> (); <br/> @ override <br/> Public Boolean acceptsdocsoutoforder () {<br/> // todo auto-generated method stub <br/> return true; <br/>}< br/>/** <br/> * collect <br/> */<br/> @ override <br/> Public void collect (INT Doc) throws ioexception {<br/> system. out. println ("Doc:" + DOC); <br/> document = reader.doc ument (DOC); <br/> int id = integer. parseint (document. get ("ID"); <br/> int COUNT = integer. parseint (document. get ("count"); <br/> map. put (ID, count); <br/> system. out. println ("put:" + ID + "" + count); <br/>}< br/> @ override <br/> Public void setnextreader (indexreader reader, int docbase) throws ioexception {<br/> This. reader = reader; // assuming that the reader is composed of multiple subreader, this method will be called the same number of times as the subreader <br/> system. out. println ("set reader"); <br/>}< br/> @ override <br/> Public void setscorer (scorer) throws ioexception {<br/> // do nothing <br/>}< br/> public class hitcollectortest {<br/> @ suppresswarnings ("deprecation ") <br/> Public static void test () throws corruptindexexception, lockobtainfailedexception, ioexception {<br/> // create a memory index <br/> ramdirectory directory = new ramdirectory (); <br/> ramdirectory directory2 = new ramdirectory (); <br/> indexwriter writer = new indexwriter (directory, analyzerfactory. getmminstance (); <br/> indexwriter writer2 = new indexwriter (directory2, analyzerfactory. getmminstance (); <br/> document = new document (); <br/> Field idfield = new field ("ID", "1", store. yes, index. analyzed); <br/> Field countfiled = new field ("count", "33", store. yes, index. analyzed); <br/> field range = new field ("range", "1", store. yes, index. analyzed); // used to filter data <br/> document. add (idfield); <br/> document. add (countfiled); <br/> document. add (range); <br/> writer. adddocument (document); <br/> writer. close (); <br/> document = new document (); <br/> idfield = new field ("ID", "2", store. yes, index. analyzed); <br/> countfiled = new field ("count", "211", store. yes, index. analyzed); <br/> range = new field ("range", "2", store. yes, index. analyzed); <br/> document. add (idfield); <br/> document. add (countfiled); <br/> document. add (range); <br/> writer2.adddocument (document); <br/> writer2.close (); <br/> // use matchalldocsquery and filter to search, use custom collector to collect data <br/> searcher = new indexsearcher (directory); <br/> searcher searcher2 = new indexsearcher (directory2 ); <br/> searcher searcher3 = new parallelmultisearcher (New searcher [] {searcher, searcher2}); <br/> mycollector collector = new mycollector (); <br/> rangefilter filter = new rangefilter ("range", "0", "4", true, true); <br/> searcher3.search (New matchalldocsquery (), filter, collector); <br/> searcher3.close (); <br/> directory. close (); <br/> // fetch data <br/> Map <integer, integer> map = collector. map; <br/> set <integer> keyset = map. keyset (); <br/> for (int I: keyset) {<br/> system. out. println ("<" + I + "," + map. get (I) + ">"); <br/>}< br/> Public static void main (string [] ARGs) throws corruptindexexception, lockobtainfailedexception, ioexception {<br/> test (); <br/>}< br/> 

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.