private void getLucene()
{
File filePath = new File("E:\\xbliucss");
File indexPath = new File("E:\\xbliucss");
//创建一个标准解析器
StandardAnalyzer sAnalyzer = new StandardAnalyzer();
//给文档加索引
IndexWriter indexWriter = new IndexWriter(filePath, sAnalyzer, true);
File[] fileList = filePath.listFiles();
Long startTime = new Date().getTime();
//增加Document到索引中
for (int i = 0; i < fileList.length; i++)
{
if (fileList[i].isFile() && fileList[i].getName().endsWith(".txt"))
{
System.out.println("File: " + fileList[i].getCanonicalPath()
+ "正在被索引");
String temp = fileReaderAll(fileList[i].getCanonicalPath(),
"GBK");
System.out.println(temp);
Document document = new Document();
//保存文件路径
Field filedPath = new Field("path", fileList[i].getPath(),
Field.Store.YES, Field.Index.NO);
//保存文件内容
Field filedBody = new Field("body", temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(filedPath);
document.add(filedBody);
indexWriter.addDocument(document);
}
}
indexWriter.optimize();//对索引进行优化
indexWriter.close();
long endTime = new Date().getTime();
System.out.println("这花费了:" + (endTime - startTime) + "毫秒把文档增加到索引里面去"
+ indexPath.getPath());
}
private String fileReaderAll(String fileName, String charset)
{
FileInputStream fis = new FileInputStream(fileName);
InputStreamReader is = new InputStreamReader(fis, charset);
BufferedReader reader = new BufferedReader(is);
String line = "";
String temp = "";
while ((line = reader.readLine()) != null)
{
temp += line;
}
reader.close();
return temp;
}
public void queryFile()
{
Hits hits = null;
String queryStr = "中华";
Query query = null;
IndexSearcher iSearcher = new IndexSearcher("E:\\xbliucss"); //检索工具,索引存放的目录
Analyzer analyzer = new StandardAnalyzer();//标准解析器
QueryParser qParse = new QueryParser("body", analyzer); //查询器,返回一个Query对象
query = qParse.parse(queryStr);
if (iSearcher != null)
{
hits = iSearcher.search(query);
}
Iterator<?> iterator = hits.iterator();
while (iterator.hasNext())
{
//一条记录对应一个Hit对象
Object next = iterator.next();
if (next instanceof Hit)
{
Hit hit = (Hit)next;
Document document = hit.getDocument();
Field field = document.getField("path");//path:对应索引时的标识
String filePath = field.stringValue();//获取文件路径
System.out.println("文件路径:" + filePath);
Field field2 = document.getField("body");//body:对应索引时的标识
String fileBody = field2.stringValue();//获取文件内容
System.out.println("文件内容:" + fileBody);
}
}
System.out.println("共找到" + hits.length() + "个结果。");
}
{
File filePath = new File("E:\\xbliucss");
File indexPath = new File("E:\\xbliucss");
//创建一个标准解析器
StandardAnalyzer sAnalyzer = new StandardAnalyzer();
//给文档加索引
IndexWriter indexWriter = new IndexWriter(filePath, sAnalyzer, true);
File[] fileList = filePath.listFiles();
Long startTime = new Date().getTime();
//增加Document到索引中
for (int i = 0; i < fileList.length; i++)
{
if (fileList[i].isFile() && fileList[i].getName().endsWith(".txt"))
{
System.out.println("File: " + fileList[i].getCanonicalPath()
+ "正在被索引");
String temp = fileReaderAll(fileList[i].getCanonicalPath(),
"GBK");
System.out.println(temp);
Document document = new Document();
//保存文件路径
Field filedPath = new Field("path", fileList[i].getPath(),
Field.Store.YES, Field.Index.NO);
//保存文件内容
Field filedBody = new Field("body", temp, Field.Store.YES,
Field.Index.TOKENIZED,
Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(filedPath);
document.add(filedBody);
indexWriter.addDocument(document);
}
}
indexWriter.optimize();//对索引进行优化
indexWriter.close();
long endTime = new Date().getTime();
System.out.println("这花费了:" + (endTime - startTime) + "毫秒把文档增加到索引里面去"
+ indexPath.getPath());
}
private String fileReaderAll(String fileName, String charset)
{
FileInputStream fis = new FileInputStream(fileName);
InputStreamReader is = new InputStreamReader(fis, charset);
BufferedReader reader = new BufferedReader(is);
String line = "";
String temp = "";
while ((line = reader.readLine()) != null)
{
temp += line;
}
reader.close();
return temp;
}
public void queryFile()
{
Hits hits = null;
String queryStr = "中华";
Query query = null;
IndexSearcher iSearcher = new IndexSearcher("E:\\xbliucss"); //检索工具,索引存放的目录
Analyzer analyzer = new StandardAnalyzer();//标准解析器
QueryParser qParse = new QueryParser("body", analyzer); //查询器,返回一个Query对象
query = qParse.parse(queryStr);
if (iSearcher != null)
{
hits = iSearcher.search(query);
}
Iterator<?> iterator = hits.iterator();
while (iterator.hasNext())
{
//一条记录对应一个Hit对象
Object next = iterator.next();
if (next instanceof Hit)
{
Hit hit = (Hit)next;
Document document = hit.getDocument();
Field field = document.getField("path");//path:对应索引时的标识
String filePath = field.stringValue();//获取文件路径
System.out.println("文件路径:" + filePath);
Field field2 = document.getField("body");//body:对应索引时的标识
String fileBody = field2.stringValue();//获取文件内容
System.out.println("文件内容:" + fileBody);
}
}
System.out.println("共找到" + hits.length() + "个结果。");
}