81,091
社区成员
发帖
与我相关
我的任务
分享
public void createIndex()
{
boolean create = true;
Directory directory = null;
IndexWriter indexWriter = null;
try
{
File fold = new File("e:\\TEMP\\lucene\\db");
// //1.创建目录
// directory = new SimpleFSDirectory(fold);
directory = FSDirectory.open(fold);
//2.创建索引
/*2.2*/
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_35);
/*2.1*/
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, analyzer);
if (create)
{
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
} else
{
indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
}
/*2.0*/
indexWriter = new IndexWriter(directory, indexWriterConfig);
//3.创建Document
Document document = null;
File file = new File("E:\\TEMP\\Lucene");
for (File txtFile : file.listFiles())
{
if (txtFile.isFile())
{
//4.为Document 添加Field
document = new Document();
document.add(new Field("fileName", txtFile.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("filePath", txtFile.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
document.add(new Field("fileContent", new BufferedReader(new FileReader(txtFile))));
}
}
//5.通过IndexWriter 添加到文档索引中
indexWriter.addDocument(document);
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (indexWriter != null)
{
indexWriter.close();
}
if (directory != null)
{
directory.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
public void searcher()
{
Directory d = null;
IndexReader ir = null;
IndexSearcher is = null;
OutputStreamWriter osw = null;
try
{
//1.创建Directory
d = FSDirectory.open(new File("e:\\TEMP\\lucene\\db"));
//2.创建IndexReader
ir = IndexReader.open(d);
//3.根据IndexReader 创建 IndexSearcher
is = new IndexSearcher(ir);
//3.创建搜索对象 Query
StandardAnalyzer sdr = new StandardAnalyzer(Version.LUCENE_35);
QueryParser qp = new QueryParser(Version.LUCENE_35, "fileName", sdr);
Query qr = qp.parse("t");
//4.根据serarch并且返回TopDocs
TopDocs topDocs = is.search(qr, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("文档数:" + scoreDocs.length);
//5.遍历TopDocs
osw = new OutputStreamWriter(System.out);
// for (ScoreDoc sd : scoreDocs)
// {
// //5.1得到该document对象
// Document doc = is.doc(sd.doc);
// List<Fieldable> fieldableList = doc.getFields();
// for (Iterator<Fieldable> iterator = fieldableList.iterator(); iterator.hasNext(); )
// {
// Fieldable next = iterator.next();
// Reader reader = next.readerValue();
// //6.获取所要的值
// char[] cbf = new char[1024];
// while(reader.read(cbf) > 0)
// {
// osw.write(cbf);
// System.out.println(1);
// }
// }
// }
//请忽略掉以上注释部分
//6.获取所要的值
} catch (Exception e)
{
e.printStackTrace();
} finally
{
//7.关闭Reader流
try
{
if (osw != null)
{
osw.close();
}
if (is != null)
{
is.close();
}
if (ir != null)
{
ir.close();
}
if (d != null) {
d.close();
}
} catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}
public void searcher()
{
Directory d = null;
IndexReader ir = null;
IndexSearcher is = null;
OutputStreamWriter osw = null;
try
{
//1.创建Directory
d = FSDirectory.open(new File("e:\\TEMP\\lucene\\db"));
//2.创建IndexReader
ir = IndexReader.open(d);
//3.根据IndexReader 创建 IndexSearcher
is = new IndexSearcher(ir);
//3.创建搜索对象 Query
StandardAnalyzer sdr = new StandardAnalyzer(Version.LUCENE_35);
QueryParser qp = new QueryParser(Version.LUCENE_35, "fileName", sdr);
Query qr = qp.parse("t");
//4.根据serarch并且返回TopDocs
TopDocs topDocs = is.search(qr, 10);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
System.out.println("文档数:" + scoreDocs.length);
//........
} catch (Exception e)
{
e.printStackTrace();
} finally
{
//7.关闭流
try
{
if (osw != null)
{
osw.close();
}
if (is != null)
{
is.close();
}
if (ir != null)
{
ir.close();
}
if (d != null) {
d.close();
}
} catch (IOException e)
{
System.out.println(e.toString());
}
}
}
}