package org.itat.test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
public class HelloLucene {
/**
* 目的:建立索引
*
* 流程:
* 1. 创建Directory 索引的目标
* 2. 创建IndexWriter 写索引
* 3. 创建Document对象
* 索引的资源 可能是 一篇 文档 或者是 数据库中的 一张表。
* 文档的 属性: 路径 名称 Size 等等
* 4. 为 Document 添加 Field (域)
* 5. 通过IdexWriter 添加文档到索引中
*
* @author love
* @version V1.0
* @throws IOException
* @throws CorruptIndexException
*
* */
public void index() throws CorruptIndexException, IOException
{
IndexWriter writer = null;
try {
//1. 创建Directory 索引的目标//内存文件
// Directory dir = new RAMDirectory(); //建立在内存中的!
// Directory dir = FSDirectory.open(new File("D:/study/Code/Lucene/index"));//创建在硬盘上!
Directory dir = FSDirectory.open(new File("D:\\study\\Code\\Lucene\\index1"));//创建在硬盘上!
//2. 创建IndexWriter 写索引
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36));
writer = new IndexWriter(dir, iwc);
//3. 创建Document对象
Document doc = null;
//4. 为 Document 添加 Field (域)
File f = new File("D:\\study\\Code\\Lucene\\Test");
for(File file: f.listFiles())
{
doc = new Document();
//建立索引文档的内容
doc.add(new Field("content",new FileReader(file)));
doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED ));
doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
//5. 通过IdexWriter 添加文档到索引中2
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(writer!=null) writer.close();
}
}
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.util.Version;
public class HelloLucene {
/**
* 目的:建立索引
*
* 流程:
* 1. 创建Directory 索引的目标
* 2. 创建IndexWriter 写索引
* 3. 创建Document对象
* 索引的资源 可能是 一篇 文档 或者是 数据库中的 一张表。
* 文档的 属性: 路径 名称 Size 等等
* 4. 为 Document 添加 Field (域)
* 5. 通过IdexWriter 添加文档到索引中
*
* @author love
* @version V1.0
* @throws IOException
* @throws CorruptIndexException
*
* */
public void index() throws CorruptIndexException, IOException
{
IndexWriter writer = null;
try {
//1. 创建Directory 索引的目标//内存文件
// Directory dir = new RAMDirectory(); //建立在内存中的!
// Directory dir = FSDirectory.open(new File("D:/study/Code/Lucene/index"));//创建在硬盘上!
Directory dir = FSDirectory.open(new File("D:\\study\\Code\\Lucene\\index1"));//创建在硬盘上!
//2. 创建IndexWriter 写索引
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36));
writer = new IndexWriter(dir, iwc);
//3. 创建Document对象
Document doc = null;
//4. 为 Document 添加 Field (域)
File f = new File("D:\\study\\Code\\Lucene\\Test");
for(File file: f.listFiles())
{
doc = new Document();
//建立索引文档的内容
doc.add(new Field("content",new FileReader(file)));
doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED ));
doc.add(new Field("path",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
//5. 通过IdexWriter 添加文档到索引中2
writer.addDocument(doc);
}
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
if(writer!=null) writer.close();
}
}
}
****************************************************************************************
//测试类:
package org.itat.test;
import org.junit.Test;
import java.io.IOException;
import org.apache.lucene.index.CorruptIndexException;
public class TestLucene {
@Test
public void testIndex() throws CorruptIndexException, IOException
{
HelloLucene h1 = new HelloLucene();
h1.index();
}
}