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 the word breaker
new termquery (..);
New Booleanquery (..);
package cn.itcast.i_query;import java.util.arraylist;import java.util.list;import org.apache.lucene.document.document;import org.apache.lucene.index.term;import org.apache.lucene.search.booleanquery;import org.apache.lucene.search.fuzzyquery;import org.apache.lucene.search.indexsearcher;import org.apache.lucene.search.matchalldocsquery;import org.apache.lucene.search.numericrangequery;import org.apache.lucene.search.query;import org.apache.lucene.search.termquery;import org.apache.lucene.search.topdocs;import org.apache.lucene.search.wildcardquery;import org.apache.lucene.search.booleanclause.occur;import Org.junit.test;import cn.itcast._domain. Article;import cn.itcast._util. Articledocumentutils;import cn.itcast._util. luceneutils;public class testapp { // keyword Query @Test public void testtermquery ()   { // the corresponding query string is:title:lucene termquery query = new termquery (New term ("title", "Lucene")); searchandshowresult (query); } // Wildcard Query // ? represents an arbitrary character // * represents 0 or more arbitrary characters @Test public void testwildcardquery ( ) { // the corresponding query string is:title:lu*n? // wildcardquery query = new wildcardquery (New Term (" Title ", " Lu*n ")); The query string for // is: content: Mutual Network wildcardquery query = new wildcardquery (New Term ("content", &NBSP; " Network "); searchandshowresult (query); } // Search all @Test public void testmatchalldocsquery () { // the corresponding query string is: *:* MatchAllDocsQuery query = new Matchalldocsquery (); searchandshowresult (query); } // fuzzy query @Test public void testfuzzyquery () { // 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 for 90% The correct characters will be displayed. fuzzyquery query = new fuzzyquery (New term ("title", "Lucenx"), 0.8f); searchandshowresult (query); } // Range Search @Test public Void testnumericrangequery () { // the corresponding query string is: id:[ 5 to 15] // numericrangequery query = numericrangequery.newintrange ("id", 5, 15, true, true); // the corresponding query string is:id:{5 to 15} // numericrangequery query = numericrangequery.newintrange ("id", 5, 15, false, false); // The corresponding query string is:id:[5 to 15} numericrangequery query = Numericrangequery.newintrange ("id", 5, 15, true, false); searchandshowresult (query); } // Boolean queries @Test Public void testbooleanquery () { booleanquery booleanquery = new booleanquery (); // booleanquery.add (Query, occur.must); // must meet // booleanquery.add (query, occur.should); // multiple should with or relationship // booleanquery.add (Query, occur.must_not); // non- query query1 = new termquery (New Term ("title", "Lucene"); query query2 = Numericrangequery.newintrange ("id", &NBSP;5,&Nbsp;15, false, true); // // The corresponding query string is: + title:lucene +id:{5 to 15] // // The corresponding query string is (uppercase and):title:lucene and id:{5 to 15] // booleanquery.add (query1, occur.must); // booleanquery.add (Query2, occur.must); // The corresponding query string is:title:lucene id:{5 to 15] // The corresponding query string is: title:lucene or id:{5 to 15] // booleanquery.add (query1, occur.should); // Booleanquery.add (query2, occur.should); // the corresponding query string is: +title:lucene -id:{5 to 15] // the corresponding query string is:title:lucene (NOT id:{5 TO 15] ) booleanquery.add (query1, occur.must); booleanquery.add (Query2, occur.must_not); searchandshowresult (booleanquery); } /** * tool methods for testing search * * @param query */ private void searchandshowresult (query query) { try { // // Preparing query conditions // String queryString = "Content:lucene"; // // 1. Convert query string to query object (queries from title and content) // queryparser queryparser = new multifieldqueryparser (Version.LUCENE_30, new String[] { "title", "Content" }, luceneutils.getanalyzer ()); // query query = queryparser.parse ( queryString); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("--- > // the corresponding query string is: " + query + \ n"); // 2, execute queries, get intermediate results indexsearcher indexsearcher = new indexsearcher ( Luceneutils.getdirectory ()); // specifies the index library used &nbsP; topdocs topdocs = indexsearcher.search (query, ; // back up to top n results // 3, processing results list<article> list = new ArrayList<Article> (); for (int i = 0; i < topdocs.scoredocs.length; i++) { // Get document data by number int docid = topdocs.scoredocs[i].doc; // document's internal number Document doc = Indexsearcher.doc (docId); // Convert Document to article article article = articledocumentutils.documenttoarticle (DOC); list.add (article); } indexsearcher.close (); // Display Results system.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 new runtimeexception (e); } }}
Lucene various query methods