Java tutorial
import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.BytesRef; import org.junit.After; import org.junit.Before; import org.junit.Test; import java.nio.file.Paths; public class SearcherTest { private Directory dir; private IndexReader reader; private IndexSearcher is; @Before public void setUp() throws Exception { dir = FSDirectory.open(Paths.get("E:\\lucene\\idx")); reader = DirectoryReader.open(dir); is = new IndexSearcher(reader); } @After public void tearDown() throws Exception { reader.close(); } /** * ? * ???TermQuery * TermQuery??QueryTermQuery??????? * ??????TermQuery?? * Lucene??????????/ * ?????????? * * @throws Exception */ @Test public void testTermQuery() throws Exception { String searchField = "contents"; String q = "xxxxxxxxx$"; Term t = new Term(searchField, q); Query query = new TermQuery(t); TopDocs hits = is.search(query, 10); System.out.println("? '" + q + "'" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } /** * ???BooleanQuery * BooleanQuery???Query * ?Query???Query * ?BooleanQuery?? * BooleanQuery?????API?? * ?BooleanQuery????API? * * @throws Exception */ @Test public void testBooleanQuery() throws Exception { String searchField = "contents"; String q1 = "xxxxxxxxx"; String q2 = "oooooooooooooooo"; Query query1 = new TermQuery(new Term(searchField, q1)); Query query2 = new TermQuery(new Term(searchField, q2)); BooleanQuery.Builder builder = new BooleanQuery.Builder(); // 1MUSTMUST??? // 2MUSTMUST_NOT??MUST_NOT?? // 3SHOULDMUST_NOT?MUSTMUST_NOT // 4SHOULDMUSTMUST??,SHOULD??? // 5SHOULDSHOULD??? // 6MUST_NOTMUST_NOT? builder.add(query1, BooleanClause.Occur.MUST); builder.add(query2, BooleanClause.Occur.MUST); BooleanQuery booleanQuery = builder.build(); TopDocs hits = is.search(booleanQuery, 10); System.out.println("? " + q1 + "And" + q2 + "" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } /** * TermRangeQuery * TermRangeQuery??? * ?ASC??ASC? * ?ASC?? * ASC??TermRangeQuery * ?NumericRangeQuery * ????? * * @throws Exception */ @Test public void testTermRangeQuery() throws Exception { String searchField = "contents"; String q = "1000001----1000002"; String lowerTermString = "1000001"; String upperTermString = "1000003"; /** * field * lowerterm - *upperterm -? *includelower -lowerterm *includeupper -upperterm *https://yq.aliyun.com/articles/45353 */ Query query = new TermRangeQuery(searchField, new BytesRef(lowerTermString), new BytesRef(upperTermString), true, true); TopDocs hits = is.search(query, 10); System.out.println("? '" + q + "'" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } /** * PrefixQuery PrefixQuery?xxx% * * @throws Exception */ @Test public void testPrefixQuery() throws Exception { String searchField = "contents"; String q = "1license"; Term t = new Term(searchField, q); Query query = new PrefixQuery(t); TopDocs hits = is.search(query, 10); System.out.println("? '" + q + "'" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } /** * PhraseQuery?big car? * ?document?"big car" * document???????big black car? * ????slop * slopslop??? * * @throws Exception */ @Test public void testPhraseQuery() throws Exception { String searchField = "contents"; String q1 = "xxxx"; String q2 = "bbb"; Term t1 = new Term(searchField, q1); Term t2 = new Term(searchField, q2); PhraseQuery.Builder builder = new PhraseQuery.Builder(); builder.add(t1); builder.add(t2); builder.setSlop(0); PhraseQuery query = builder.build(); TopDocs hits = is.search(query, 10); System.out.println("? '" + q1 + q2 + "" + "" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } /** * ??FuzzyQuery * FuzzyQuery???? * * @throws Exception */ @Test public void testFuzzyQuery() throws Exception { String searchField = "contents"; String q = "ljlxx"; Term t = new Term(searchField, q); Query query = new FuzzyQuery(t); TopDocs hits = is.search(query, 10); System.out.println("? '" + q + "'" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } /** * ??WildcardQuery * Lucene???WildcardQuery * ???1*?0 * * @throws Exception */ @Test public void testWildcardQuery() throws Exception { String searchField = "contents"; String q = "bb??qq"; Term t = new Term(searchField, q); Query query = new WildcardQuery(t); TopDocs hits = is.search(query, 10); System.out.println("? '" + q + "'" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } /** * ?? * QueryParser?????Query? * * @throws Exception */ @Test public void testQueryParser() throws Exception { Analyzer analyzer = new StandardAnalyzer(); // ? String searchField = "contents"; String q = "xxxxxxxxx$"; //?? QueryParser parser = new QueryParser(searchField, analyzer); // Query query = parser.parse(q); TopDocs hits = is.search(query, 100); System.out.println("? " + q + "" + hits.totalHits + ""); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("fullPath")); } } }