2. 指定数字范围查询NumericRangeQuery
3. 指定字符串开头搜索PrefixQuery
4. 组合查询BooleanQuery
New maven project -> Create a simple project -> Group Id: com.andrew.lucene Artifact Id: Lucene04 Version: 0.0.1-SNAPSHOT Packaging: jar
pom.xml4.0.0 com.andrew.lucene Lucene04 0.0.1-SNAPSHOT org.apache.lucene lucene-core 5.3.1 org.apache.lucene lucene-queryparser 5.3.1 org.apache.lucene lucene-analyzers-common 5.3.1 junit junit 4.12
Indexer.java代码 package com.andrew.lucene; import java.nio.file.Paths; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.IntField; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; public class Indexer { private Integer ids[] = { 1, 2, 3 }; private String citys[] = { "aingdao", "banjing", "changhai" }; private String descs[] = { "Qingdao is b beautiful city.", "Nanjing is c city of culture.", "Shanghai is d bustling city." }; private Directory dir; // 获取IndexWriter实例 private IndexWriter getWriter() throws Exception { Analyzer analyzer = new StandardAnalyzer(); // 标准分词器 IndexWriterConfig iwc = new IndexWriterConfig(analyzer); IndexWriter writer = new IndexWriter(dir, iwc); return writer; } // 生成索引 private void index(String indexDir) throws Exception { dir = FSDirectory.open(Paths.get(indexDir)); IndexWriter writer = getWriter(); for (int i = 0; i < ids.length; i++) { Document doc = new Document(); doc.add(new IntField("id", ids[i], Field.Store.YES)); doc.add(new StringField("city", citys[i], Field.Store.YES)); doc.add(new TextField("desc", descs[i], Field.Store.YES)); writer.addDocument(doc); // 添加文档 } writer.close(); } public static void main(String[] args) throws Exception { new Indexer().index("E:\\lucene5"); } }
SearchTest.java代码 package com.andrew.lucene; import java.nio.file.Paths; 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.search.BooleanClause; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.NumericRangeQuery; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.TopDocs; 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; public class SearchTest { private Directory dir; private IndexReader reader; private IndexSearcher is; @Before public void setUp() throws Exception { dir = FSDirectory.open(Paths.get("E:\\lucene5")); reader = DirectoryReader.open(dir); is = new IndexSearcher(reader); } @After public void tearDown() throws Exception { reader.close(); } // 指定项范围搜索 @Test public void testTermRangeQuery() throws Exception { TermRangeQuery query = new TermRangeQuery("desc", new BytesRef("b".getBytes()), new BytesRef("c".getBytes()), true, true); TopDocs hits = is.search(query, 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } // 指定数字范围 @Test public void testNumericRangeQuery() throws Exception { NumericRangeQueryquery = NumericRangeQuery.newIntRange("id", 1, 2, true, true); TopDocs hits = is.search(query, 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } // 指定字符串开头搜索 @Test public void testPrefixQuery() throws Exception { PrefixQuery query = new PrefixQuery(new Term("city", "a")); TopDocs hits = is.search(query, 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } // 多条件查询 @Test public void testBooleanQuery() throws Exception { NumericRangeQuery query1 = NumericRangeQuery.newIntRange("id", 1, 2, true, true); PrefixQuery query2 = new PrefixQuery(new Term("city", "a")); BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder(); booleanQuery.add(query1, BooleanClause.Occur.MUST); booleanQuery.add(query2, BooleanClause.Occur.MUST); TopDocs hits = is.search(booleanQuery.build(), 10); for (ScoreDoc scoreDoc : hits.scoreDocs) { Document doc = is.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } } } 运行结果: 1 aingdao Qingdao is b beautiful city. 2 banjing Nanjing is c city of culture. 3 changhai Shanghai is d bustling city. 1 aingdao Qingdao is b beautiful city. 2 banjing Nanjing is c city of culture. 1 aingdao Qingdao is b beautiful city. 1 aingdao Qingdao is b beautiful city.