23,404
社区成员
发帖
与我相关
我的任务
分享
package com.lzw.lucene;
import java.io.*;
import java.util.*;
import org.apache.lucene.analysis.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
/**
* 2010-5-12lucene学习第一天
* 搜索指定字符所在的文件
* lucene-core-2.4.1.jar
*/
public class LuceneTest {
public static void main(String[] args) {
LuceneUtil test = new LuceneUtil();
// test.createIndex("D:\\lzwLucene\\src", "D:\\lzwLucene\\index");
test.search("添加路由的命令 PMSV300R002C05B055", "D:\\lzwLucene\\index");
//加上AND是两空格之间都必须有. 不加就是OR
}
public static void getFileList(File srcFile, List<File> list) {
if (srcFile.isDirectory()) {
File[] files = srcFile.listFiles();
for (int i = 0; i < files.length; i++) {
getFileList(files[i], list);
}
} else {
if (srcFile.getName().toLowerCase().endsWith(".txt")) {
list.add(srcFile);
}
}
}
public static String fileReaderAll(File file, String charset) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), charset));
String temp = null;
StringBuffer sb = new StringBuffer();
while ((temp = reader.readLine()) != null) {
sb.append(temp + "\n");
}
reader.close();
return sb.toString();
}
}
class LuceneUtil{
public Map<String, String> search(String key,String indexFilePath){
try {
IndexSearcher searcher = new IndexSearcher(indexFilePath);
QueryParser queryParser = new QueryParser("content",new StandardAnalyzer());
Query query = queryParser.parse(key);
if(searcher != null){
long startTime = new Date().getTime();
Hits hits = searcher.search(query);
if (hits == null || hits.length() == 0) {
System.out.println("对不起。没你想要的结果!");
}else {
for (int i = 0; i < hits.length(); i++) {
long endTime = new Date().getTime();
System.out.println("路径:" + hits.doc(i).get("path"));
System.out.println("内容:" + hits.doc(i).get("content"));
System.out.println("检索完成,用时" + (endTime - startTime) + "毫秒");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param indexFilePath 创建索引的文件路径
* @param srcFilePath 被索引的文件路径
*/
public void createIndex(String indexFilePath, String srcFilePath) {
File indexFileDir = new File(indexFilePath);
File srcFileDir = new File(srcFilePath);
IndexWriter indexWriter = null;
List<File> files = new ArrayList<File>();
Analyzer luceneAnalyzer = new StandardAnalyzer();
try {
indexWriter = new IndexWriter(indexFileDir, luceneAnalyzer, true);
LuceneTest.getFileList(srcFileDir, files);
System.out.println("一共被索引的文件有:" + files.size());
long startTime = new Date().getTime();
for (File file : files) {
System.out.println("文件:" + file.getAbsolutePath() + "开始被索引...");
String tempStr = LuceneTest.fileReaderAll(file, "GBK");
System.out.println("文件内容Lenth:" + tempStr.length());
Document doc = new Document();
Field fieldPath = new Field("filePath", file.getPath(),
Field.Store.YES, Field.Index.NO);
Field fieldContent = new Field("content", tempStr,
Field.Store.YES, Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
doc.add(fieldPath);
doc.add(fieldContent);
indexWriter.addDocument(doc);
}
indexWriter.optimize();// optimize()方法是对索引进行优化
// 测试一下索引的时间
long endTime = new Date().getTime();
System.out.println("花费了" + (endTime - startTime) + " 毫秒来把文档增加到索引里面去!" + indexFileDir.getPath());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}