package com.fxr.demo3;import java.io.IOException;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.index.CorruptIndexException;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.apache.lucene.store.RAMDirectory;public class TestRamSearcher { Directory directory=new RAMDirectory(); public void creatRamIndex() throws IOException{ String [] ids={"1","2","3","4"}; String [] names={"ZHangsan","lisi","wangwu","zhaoliu"}; String [] addresses={"tianjing","nanjing","beijing","nanning"}; String [] birthdays={"19820720","19840203","19770409","19830130"}; Analyzer analyzer=new StandardAnalyzer(); //true 表示创建或覆盖当前索引;false表示对当前索引进行追加 //Default value is 128 IndexWriter writer=new IndexWriter(directory,analyzer,true,IndexWriter.MaxFieldLength.LIMITED); for(int i=0;i
Lucene中的另一种写法:
package com.fxr.demo3;import java.io.IOException;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.index.Term;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TopDocCollector;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;public class TestTopDocCollectller { public static void main(String[] args) throws IOException { Analyzer analyzer = new StandardAnalyzer(); String indexDir = "f:/lucenefxrindex"; Directory dir=FSDirectory.getDirectory(indexDir);//索引存放的位置 IndexSearcher indexSearcher = new IndexSearcher(dir); ScoreDoc[]hits = null; TopDocCollector topDocCollector = new TopDocCollector(10); Term term = new Term("address","zhengzhou"); TermQuery query = new TermQuery(term); indexSearcher.search(query,topDocCollector); hits = topDocCollector.topDocs().scoreDocs; for(int i=0;i