Lucene_demo09_txt文件索引
/**
* txt文件索引
*/
public class IndexFile {
private Directory directory;
private String indexPath = "D://lucene/index"; // 建立索引文件的目录
private String dirPath = "D://lucene/data"; // txt资源目录
private IndexWriter indexWriter;
/*
* 获得所有txt文件
*/
public List<File> getFileList(String dirPath) {
File[] files = new File(dirPath).listFiles();
List<File> fileList = new ArrayList<File>();
for (File file : files) {
if (isTxtFile(file.getName())) {
fileList.add(file);
}
}
return fileList;
}
/*
* 判断是否是txt文件
*/
public boolean isTxtFile(String fileName) {
if (fileName.lastIndexOf(".txt") > 0) {
return true;
}
return false;
}
/*
* 将文件转换成Document对象
*/
public Document fileToDocument(File file) throws Exception {
Document document = new Document();
document.add(new Field("filename", file.getName(), Store.YES, Index.ANALYZED));
document.add(new Field("content", getFileContent(file), Store.YES, Index.ANALYZED));
document.add(new Field("size", String.valueOf(file.getTotalSpace()), Store.YES, Index.ANALYZED));
return document;
}
/*
* 获得indexwriter对象
*/
public IndexWriter getIndexWriter(Directory dir) throws Exception {
IndexWriter indexWriter = new IndexWriter(dir, LuceneUtils.analyzer, MaxFieldLength.LIMITED);
return indexWriter;
}
/*
* 关闭indexwriter对象
*/
public void closeWriter() throws Exception {
if (indexWriter != null) {
indexWriter.close();
}
}
/*
* 读取文件内容
*/
public String getFileContent(File file) throws Exception {
Reader reader = new InputStreamReader(new FileInputStream(file), "UTF-8");
BufferedReader br = new BufferedReader(reader);
String result = "";
while (br.readLine() != null) {
result = result + "\n" + br.readLine();
}
br.close();
reader.close();
return result;
}
/**
* 启动初始化
*/
@Before
public void init() {
try {
directory = FSDirectory.open(new File(indexPath));
indexWriter = getIndexWriter(directory);
}
catch (Exception e) {
System.out.println("索引打开异常!");
}
}
/**
* 创建索引 Main方法
*/
@Test
public void createIndex() throws Exception {
List<File> fileList = getFileList(dirPath);
Document document = null;
for (File file : fileList) {
document = fileToDocument(file);
indexWriter.addDocument(document);
System.out.println("filename:" + document.get("filename"));
System.err.println("content:" + document.get("content"));
indexWriter.commit();
}
closeWriter();
}
}
分享到:
相关推荐
Lucene5写的全文搜索的demo,包括创建索引和搜索
全文索引工具 开源工具 java编写 lucene的简单demo
lucene 4.7.2支持java 6 ,之后的版本需要java 7以上,创建、删除、修改索引,搜索支持通用对象(可以根据对象类型搜索),可以范围搜索、排序、高亮,希望有所帮助
这是我通过对Lucene3.3.0源码进行了简单解读,依据应用详细的做了很多Demo,大家可以一道学习。
Lucene结合Sql建立索引Demo源码 Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的...
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
//src要创建索引的文件,destDir索引存放的目录 public static void createIndex(File src, File destDir){ Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); //创建一个语法分析器 ...
Lucene.NET2.9.2 的应用Demo,从提取自淘车网的搜索项目,主要分两部分,创建索引和查询索引. 效果展示:http://www.taoche.com
里面编写了法创建,搜索的基本方法 LuceneDemo 单个文件索引,创建查找 CopyFile 为多文件复制多些文件做准备 CreateIndexe 多文件创建索引;Searcher多文件搜索对应CreateIndexe
创建索引 一、创建词法分析器 二、创建索引存储目录 三、创建索引写入器 四、将内容存储到索引 关键字查询 一、创建索引存储目录读取器 二、创建索引搜索器 三、解析查询 四、获取结果
lucene+zoie近实时索引demo lucene+zoie近实时索引demo
总结了一些实用的demo 包括: 1.建立索引 2.通过IKAnalyzer搜索中文关键词 3.复杂的多字段搜索 4.多线程并发搜索,通过contiperf测试,详见:contiperf_百度百科 5.分页搜索 注意:lucene4.10.0需要jdk1.7以上...
Lucene3.0 Demo 索引的创建,更新(未实现),删除
Lucene使用的一个Demo,包括索引的创建、增加、更新、删除等
这是Lucene.NET v3.0.3 DEMO范例程序(含PanGu分词),用C#语言编写的,同时对PanGu分词进行了整合,可以直接下载运行。 项目中还整理了一个后台任务线程监听范例,可以用作增量索引创建,但这个需要你自行加入相关...
利用Lucene.net做的建立索引,检索分页
一个简单的lucene demo,使用Lucene5.5+springmvc+mysql开发,包括索引的增删改查,查询中可以高亮显示+分页+自定义排序,有兴趣的朋友可以看看
lucene4.0创建索引,删除索引,搜索的一些小demo,分享出来供大家学习。
jsuop+lucene 实现简单的新闻爬去、全文索引查询