Various inquiries
Method One: Use Queryparser with query syntax. (Word breaker will be used)
Multifieldqueryparser
Querying strings------------------------> Query objects
For example:
Shanghai and Weather
Shanghai OR Weather
Shanghai News and Site:news.163.com
...
Way two:
Create an instance of query directly (subclass) without using a word breaker
New Termquery (..);
New Booleanquery (..);
PackageCn.itcast.i_query;Importjava.util.ArrayList;Importjava.util.List;Importorg.apache.lucene.document.Document;Importorg.apache.lucene.index.Term;ImportOrg.apache.lucene.search.BooleanQuery;ImportOrg.apache.lucene.search.FuzzyQuery;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.MatchAllDocsQuery;ImportOrg.apache.lucene.search.NumericRangeQuery;ImportOrg.apache.lucene.search.Query;ImportOrg.apache.lucene.search.TermQuery;ImportOrg.apache.lucene.search.TopDocs;ImportOrg.apache.lucene.search.WildcardQuery;Importorg.apache.lucene.search.BooleanClause.Occur;Importorg.junit.Test;ImportCn.itcast._domain. article;ImportCn.itcast._util. Articledocumentutils;ImportCn.itcast._util. Luceneutils; Public classTestApp {//keyword Query@Test Public voidTesttermquery () {//the corresponding query string is: Title:luceneTermquery query =NewTermquery (NewTerm ("title", "Lucene")); Searchandshowresult (query); } //Wildcard Query//? Represents an arbitrary character//* denotes 0 or more arbitrary characters@Test Public voidTestwildcardquery () {//the corresponding query string is: Title:lu*n? //wildcardquery query = new Wildcardquery (New term ("title", "Lu*n?")); //the corresponding query string is: content: Mutual NetworkWildcardquery query =NewWildcardquery (NewTerm ("content", "Mutual Network"))); Searchandshowresult (query); } //Query All@Test Public voidTestmatchalldocsquery () {//the corresponding query string is: *:*Matchalldocsquery query =NewMatchalldocsquery (); Searchandshowresult (query); } //Fuzzy Query@Test Public voidTestfuzzyquery () {//the corresponding query string is: title:lucenx~0.9//The second parameter is the minimum similarity, indicating how much of the correct one is displayed, such as 0.9 indicating that 90% of the correct characters will be displayed. Fuzzyquery query =NewFuzzyquery (NewTerm ("title", "Lucenx"), 0.8F); Searchandshowresult (query); } //Scope Query@Test Public voidTestnumericrangequery () {//the corresponding query string is: Id:[5 to []//numericrangequery query = numericrangequery.newintrange ("id", 5, +, True, true); //the corresponding query string is: Id:{5 to//numericrangequery query = numericrangequery.newintrange ("id", 5, +, FALSE, false); //the corresponding query string is: Id:[5 toNumericrangequery query = Numericrangequery.newintrange ("id", 5, 15,true,false); Searchandshowresult (query); } //Boolean Query@Test Public voidTestbooleanquery () {booleanquery booleanquery=NewBooleanquery (); //booleanquery.add (query, occur.must);//must meet//booleanquery.add (query, occur.should);//a relationship that represents or in conjunction with multiple should//booleanquery.add (query, occur.must_not);//Non-Query Query1=NewTermquery (NewTerm ("title", "Lucene")); Query Query2= Numericrangequery.newintrange ("id", 5, 15,false,true); // //the corresponding query string is: +title:lucene +id:{5 to []// //the corresponding query string is (uppercase and): Title:lucene and id:{5 to]//Booleanquery.add (Query1, occur.must); //Booleanquery.add (Query2, occur.must); //the corresponding query string is: Title:lucene id:{5 to []//the corresponding query string is: Title:lucene OR id:{5 to []//Booleanquery.add (Query1, occur.should); //Booleanquery.add (Query2, occur.should); //the corresponding query string is: +title:lucene-id:{5 to []//the corresponding query string is: Title:lucene (not id:{5 to])Booleanquery.add (Query1, occur.must); Booleanquery.add (Query2, Occur.must_not); Searchandshowresult (Booleanquery); } /*** Tool method for testing search * *@paramQuery*/ Private voidsearchandshowresult (query query) {Try { // //Prepare query Criteria//String queryString = "Content:lucene"; // //1. Convert the query string to the queried object (query from title and content)//Queryparser queryparser = new Multifieldqueryparser (version.lucene_30, new string[] {"title", "Content"}, Luceneut Ils.getanalyzer ()); //query query = queryparser.parse (queryString);System.out.println ("--->//The corresponding query string is:" + query + "\ n"); //2, execute query, get intermediate resultIndexsearcher Indexsearcher =NewIndexsearcher (Luceneutils.getdirectory ());//Specify the index library to useTopdocs Topdocs = indexsearcher.search (query, 100);//returns up to the top n results//3, processing resultslist<article> list =NewArraylist<article>(); for(inti = 0; i < topDocs.scoreDocs.length; i++) { //get the document data by number intDocId = Topdocs.scoredocs[i].doc;//internal number of documentDocument doc =Indexsearcher.doc (docId); //Convert document to articleArticle Article =articledocumentutils.documenttoarticle (DOC); List.add (article); } indexsearcher.close (); //Show ResultsSYSTEM.OUT.PRINTLN ("Total number of results:" +list.size ()); for(article a:list) {System.out.println ("------------------------------"); System.out.println ("id =" +A.getid ()); System.out.println ("title =" +a.gettitle ()); System.out.println ("CONTENT =" +a.getcontent ()); } } Catch(Exception e) {Throw NewRuntimeException (e); } }}
Use of Lucene queries