lucene-query query->prefixquery using prefix search

Source: Internet
Author: User
Tags dname

Prefixquery is the use of prefixes to find. Typically, a term is defined first . The entry contains the name of the field to look up and the prefix of the keyword, and then constructs a Prefixquery object from that entry , which can be searched by prefix.

 PackageCh11;ImportOrg.apache.lucene.analysis.standard.StandardAnalyzer;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.document.Field;ImportOrg.apache.lucene.index.IndexWriter;Importorg.apache.lucene.index.Term;Importorg.apache.lucene.search.Hits;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.PrefixQuery;ImportOrg.apache.lucene.search.RangeQuery; Public classPrefixquerytest { Public Static voidMain (string[] args)throwsException {//generate Document object, same asDocument Doc1=NewDocument (); //Add the contents of the "Name" field,Doc1.add (Field.text ("Name", "David")); //Add the contents of the "title" field,Doc1.add (Field.keyword ("title", "Doc1")); Document doc2=NewDocument (); Doc2.add (Field.text ("Name", "Darwen")); Doc2.add (Field.keyword ("title", "Doc2")); Document DOC3=NewDocument (); Doc3.add (Field.text ("Name", "Smith")); Doc3.add (Field.keyword ("title", "DOC3")); Document doc4=NewDocument (); Doc4.add (Field.text ("Name", "Smart")); Doc4.add (Field.keyword ("title", "Doc4")); //Generate index writerIndexWriter writer=NewIndexWriter ("C://index",                 NewStandardAnalyzer (),true); //set to mixed index modeWriter.setusecompoundfile (true); //add a document to the index in turnwriter.adddocument (Doc1);         Writer.adddocument (DOC2);         Writer.adddocument (DOC3);         Writer.adddocument (DOC4); //Close the index writerWriter.close (); //Build Index Finder objectIndexsearcher Searcher=NewIndexsearcher ("C://index"); //Construct EntryTerm pre1=NewTerm ("name", "Da"); Term Pre2=NewTerm ("name", "Da"); Term pre3=NewTerm ("name", "SM"); //to save the results of a searchHits Hits=NULL; //generates an object of type prefixquery, initialized to nullprefixquery Query=NULL; Query=Newprefixquery (Pre1); //starts the first retrieval and returns the results of the searchhits=searcher.search (query); //output The corresponding search resultsPrintresult (hits,"Document prefixed with ' Da '"); Query=Newprefixquery (Pre2); //start a second search and return the results of the searchhits=searcher.search (query); //output The corresponding search resultsPrintresult (hits,"Document prefixed with ' da '"); Query=Newprefixquery (pre3); //start a second search and return the results of the searchhits=searcher.search (query); //output The corresponding search resultsPrintresult (hits,"Document prefixed with ' SM '"); }       Public Static voidPrintresult (Hits Hits, String key)throwsException {System.out.println ("Find/" "+ Key +"/":"); if(hits! =NULL) {             if(hits.length () = = 0) {System.out.println ("No results found");             System.out.println (); } Else{System.out.print (Find);  for(inti = 0; I < hits.length (); i++) {                     //Get DocumentationDocument D=Hits.doc (i); //get the contents of the "title" FieldString dname= D.get ("title"); System.out.print (Dname+ "   ");                 } System.out.println ();             System.out.println (); }         }     }}

In the code, 4 different Document constructs are constructed first . Each document has a field named "Name", which stores the name of the person. The code then constructs 3 different entries, "da", "Da",and "SM", which areexactly theprefixes of the keywords in the "name" field.

lucene-query query->prefixquery using prefix search

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.