lucene-query query->wildcardquery using wildcard characters search

Source: Internet
Author: User
Tags dname

Lucene also provides a wildcard query, which is wildcardquery.

 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.WildcardQuery; Public classWildcardquerytest { Public Static voidMain (string[] args)throwsException {//generate Document object, same asDocument Doc1=NewDocument (); //Add the contents of the "Content" field,Doc1.add (Field.text ("Content", "whatever")); //Add the contents of the "title" field,Doc1.add (Field.keyword ("title", "Doc1")); Document doc2=NewDocument (); Doc2.add (Field.text ("Content", "whoever")); Doc2.add (Field.keyword ("title", "Doc2")); Document DOC3=NewDocument (); Doc3.add (Field.text ("Content", "however")); Doc3.add (Field.keyword ("title", "DOC3")); Document doc4=NewDocument (); Doc4.add (Field.text ("Content", "Everest")); Doc4.add (Field.keyword ("title", "Doc4")); //Generate index writerIndexWriter writer=NewIndexWriter ("C://index",                 NewStandardAnalyzer (),true); //to add a Document object to the indexwriter.adddocument (Doc1);         Writer.adddocument (DOC2);         Writer.adddocument (DOC3);         Writer.adddocument (DOC4); //Close the index writerWriter.close (); //Generate index writerIndexsearcher Searcher=NewIndexsearcher ("C://index"); //Construct EntryTerm word1=NewTerm ("content", "*ever"); Term Word2=NewTerm ("content", "Wh?ever"); Term Word3=NewTerm ("content", "H??") Ever "); Term Word4=NewTerm ("content", "ever*"); //generates a Wildcardquery object, initialized to nullwildcardquery Query=NULL; //to save the results of a searchHits Hits=NULL; Query=Newwildcardquery (WORD1); //starts the first retrieval and returns the results of the searchhits=searcher.search (query); //output information about the results of the searchPrintresult (hits,"*ever"); Query=Newwildcardquery (WORD2); //start a second search and return the results of the searchhits=searcher.search (query); //output information about the results of the searchPrintresult (hits,"Wh?ever"); Query=Newwildcardquery (WORD3); //starts a third search and returns the results of the searchhits=searcher.search (query); //output information about the results of the searchPrintresult (hits,"H??" Ever "); Query=Newwildcardquery (WORD4); //starts the fourth retrieval and returns the results of the searchhits=searcher.search (query); //output information about the results of the searchPrintresult (hits,"Ever*"); }         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 Document ObjectDocument 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 (); }         }     }}

As you can see from the above code, the wildcard character "?" Represents 1 characters, while "*" represents 0 characters or more. However, wildcard retrieval and fuzzyquery above are subject to some impact on the performance of the search because of the need for string matching of field keywords.

lucene-query query->wildcardquery using wildcard characters 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.