- 本章使用的是lucene5.3.0
-
指定数字范围查询
package com.shyroke.test; import java.io.IOException; 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.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; 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.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.junit.Before; import org.junit.Test; public class IndexTest { private Integer ids[]={1,2,3}; private String citys[]={"aingdao","banjing","changhai"}; private String descs[]={ "Qingdao is a beautiful city.", "Nanjing is a city of culture.", "Shanghai is a bustling city." }; private IndexReader reader; private IndexSearcher searcher; private String indexDir="E:\lucene5\index"; @Before public void setUp() throws IOException { IndexWriter indexWriter = getIndexWiter(); for (int i = 0; i < ids.length; i++) { Document document = new Document(); document.add(new IntField("id", ids[i], Field.Store.YES)); document.add(new StringField("city", citys[i], Field.Store.YES)); document.add(new StringField("desc", descs[i], Field.Store.NO)); indexWriter.addDocument(document); } indexWriter.close(); Directory dir=FSDirectory.open(Paths.get(indexDir)); reader=DirectoryReader.open(dir); searcher=new IndexSearcher(reader); } /** * 实例化IndexWiter * * @return * @throws IOException */ private IndexWriter getIndexWiter() throws IOException { Directory dir = FSDirectory.open(Paths.get(indexDir)); Analyzer analyzer = new StandardAnalyzer(); IndexWriterConfig conf = new IndexWriterConfig(analyzer); IndexWriter indexWriter = new IndexWriter(dir, conf); return indexWriter; } /** * 指定数字范围,其中id必须是整型的,如:document.add(new IntField("id", ids[i], Field.Store.YES)); * newIntRange方法第二、三个参数是查询的起点值和终点值,即区间,最后两个参数是指是否包含起点值和终点值。 * @throws Exception */ @Test public void testNumericRangeQuery()throws Exception{ NumericRangeQuery<Integer> query=NumericRangeQuery.newIntRange("id", 1, 2, true, true); TopDocs hits=searcher.search(query, 10); for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc=searcher.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); System.out.println("---------------------"); } } }
- 结果:
解析:newIntRange("id", 1, 2, true, true); 该方法查询出id值为1和2之间的document,且包含1和2.因为desc设置为“Field.Store.NO”所以这里的值为null
-
指定字符串开头搜索
/** * 指定字符串开头搜索 * @throws Exception */ @Test public void testPrefixQuery()throws Exception{ PrefixQuery query=new PrefixQuery(new Term("city","a")); TopDocs hits=searcher.search(query, 10); for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc=searcher.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } }
结果:
解:查询出city中以a为开头的document
-
多条件查询
/** * 多条件查询 * @throws Exception */ @Test public void testBooleanQuery()throws Exception{ NumericRangeQuery<Integer> 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=searcher.search(booleanQuery.build(), 10); for(ScoreDoc scoreDoc:hits.scoreDocs){ Document doc=searcher.doc(scoreDoc.doc); System.out.println(doc.get("id")); System.out.println(doc.get("city")); System.out.println(doc.get("desc")); } }
解:查询出id值为1和2之间且city值以a开头的document