copy一个lucene in action的例子
package com.s.lucene;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
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.apache.lucene.util.Version;
public class IndexReader {
public static void main(String[] args) throws IOException, ParseException {
String indexDir = "F:\\workspace\\JavaDemo\\src\\com\\s\\lucene\\index";//存储lucene索引的文件夹
String queryKey = "最优秀";//查询词
//查询
search(indexDir,queryKey);
}
public static void search(String indexDir,String queryKey) throws IOException, ParseException{
Directory dir = FSDirectory.open(new File(indexDir));//创建索引文件夹
IndexSearcher is = new IndexSearcher(dir);//查询
QueryParser parser = new QueryParser(Version.LUCENE_30,
"contents",
new StandardAnalyzer(Version.LUCENE_30));
Query query = parser.parse(queryKey);//封装查询为query
long start = System.currentTimeMillis();
TopDocs hits = is.search(query, 10);//查询
long end = System.currentTimeMillis();
System.out.println(end-start);
for (ScoreDoc sd : hits.scoreDocs) {//遍历查询结果
Document doc = is.doc(sd.doc);
System.out.println(doc.get("fullpath"));
System.out.println(doc.get("body"));
}
is.close();
}
}
标签:检索,search,入门,System,lucene,import,apache,org
From: https://blog.51cto.com/u_2465818/6209760