提交 e2830be0 编写于 作者: N nicky

添加Lucene测试文件

上级 653bb6f6
......@@ -89,14 +89,12 @@ public class LuceneIndexer {
indexWriter.addDocument(doc);
}
System.out.println("共索引了"+indexWriter.numDocs()+"个文件");
indexWriter.commit();
indexWriter.close();
System.out.println("创建索引所用时间:"+(System.currentTimeMillis()-startTime));
System.out.println("创建索引所用时间:"+(System.currentTimeMillis()-startTime)+"毫秒");
return true;
}
......@@ -123,9 +121,9 @@ public class LuceneIndexer {
try {
boolean r = LuceneIndexer.getInstance().createIndex(INDEX_DIR,DATA_DIR);
if(r){
System.out.println("创建成功!");
System.out.println("索引创建成功!");
}else{
System.out.println("创建失败!");
System.out.println("索引创建失败!");
}
} catch (IOException e) {
e.printStackTrace();
......
package com.test.lucene;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.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 java.io.IOException;
import java.nio.file.Paths;
public class SearchBuilder {
public static void doSearch(String indexDir , String queryStr) throws IOException, ParseException {
Directory directory = FSDirectory.open(Paths.get(indexDir));
DirectoryReader reader = DirectoryReader.open(directory);
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new SmartChineseAnalyzer();
QueryParser parser = new QueryParser("contents",analyzer);
Query query = parser.parse(queryStr);
long startTime = System.currentTimeMillis();
TopDocs docs = searcher.search(query,10);
System.out.println("查找"+queryStr+"所用时间:"+(System.currentTimeMillis()-startTime));
System.out.println("查询到"+docs.totalHits+"条记录");
for(ScoreDoc scoreDoc : docs.scoreDocs){
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("fullPath"));
}
reader.close();
}
public static void main(String[] args){
String indexDir = "D:\\lucene";
String q = "test"; //查询这个字符串
try {
doSearch(indexDir, q);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册