我的第一 lucene 实例
来源:互联网 发布:手机软件java版下载 编辑:程序博客网 时间:2024/05/17 03:14
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();
}
}
- 我的第一 lucene 实例
- 我的第一个 lucene程序
- 第一个Lucene实例
- 我自己跑通的网上的lucene实例
- 我的第一个Struts实例
- 我的第一个Ajax实例
- 我的第一个Spring MVC实例
- 我的第一个activiti实例
- lucene的简单实例
- lucene的简单实例
- lucene的简单实例
- lucene的简单实例
- 我的Lucene学习
- 历时一年,我的第一本书《从Lucene到Elasticsearch:全文检索实战》已出版!
- 我的第一个Android实例之创建AVD总结
- day65(5.15)我的第一个框架学习实例struts
- MMSeg4j + Lucene的应用实例
- lucene搜索简单的实例
- 设计范式解析:从面向对象的设计模式看软件设计(zz)
- ADF BC组件的理解
- vc中遇到错误提示:nafxcwd.lib(thrdcore.obj) : error LNK2001: unresolved external symbol __endthreadex
- git+github创建分支&提交并贡献代码
- Android应用程序实现欢迎引导页面的方法实现
- 我的第一 lucene 实例
- 观察者模式--Observer
- 在Mac下使用Lemon
- s-video CVBS接口 区别 .
- 我逛贴吧后的想法
- 连续drop 表的注意事项
- 位域的用法
- NSString / NSMutableString 字符串处理,常用代码 (实例)
- js初学笔记