使用Lucene对预处理后的文档进行创建索引(可运行)
来源:互联网 发布:全国计算机专业与软件 编辑:程序博客网 时间:2024/04/29 04:22
时间:
2015/3/18
杨鑫newlife
对于文档的预处理后,就要开始使用Lucene来处理相关的内容了。
这里使用的Lucene的步骤如下:
首先要为处理对象机那里索引
二是构建查询对象
三是在索引中查找
这里的代码是处理创建索引的部分
代码:
package ch2.lucenedemo.process;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import jeasy.analysis.MMAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.index.IndexWriter;
public class IndexProcessor {
//成员变量,存储创建的索引文件存放的位置
private String INDEX_STORE_PATH = "E:\\Lucene项目\\索引目录";
//创建索引
public void createIndex(String inputDir){
try
{
System.out.println("程序开始运行,正在创建索引->->->->->");
IndexWriter writer = new IndexWriter(INDEX_STORE_PATH, new MMAnalyzer(), true);
File filesDir = new File(inputDir);
//取得所有需要建立索引的文件数组
File[] files = filesDir.listFiles();
//遍历数组
for(int i = 0; i < files.length; i++){
//获取文件名
String fileName = files[i].getName();
//判断文件是否为txt类型的文件
if(fileName.substring(fileName.lastIndexOf(".")).equals(".txt")){
//创建一个新的Document
Document doc = new Document();
System.out.println("正在为文件名创建索引->->->->");
//为文件名创建一个Field
Field field = new Field("filename", files[i].getName(), Field.Store.YES, Field.Index.TOKENIZED);
doc.add(field);
System.out.println("正在为文件内容创建索引->->->->");
//为文件内容创建一个Field
field = new Field("content", loadFileToString(files[i]), Field.Store.NO, Field.Index.TOKENIZED);
doc.add(field);
//把Document加入到IndexWriter
writer.addDocument(doc);
}
}
writer.close();
System.out.println("程序创建结束->->->->");
}catch(Exception e){
e.printStackTrace();
}
}
/*
* 从文件中把内容读取出来,所有的内容就放在一个String中返回
* */
public String loadFileToString(File file){
try{
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sb = new StringBuffer();
String line= br.readLine();
while(line != null){
sb.append(line);
line = br.readLine();
}
br.close();
return sb.toString();
}catch(IOException e){
e.printStackTrace();
return null;
}
}
public static void main(String[] args){
IndexProcessor ip = new IndexProcessor();
ip.createIndex("E:\\Lucene项目\\目标文件");
}
}
- 使用Lucene对预处理后的文档进行创建索引(可运行)
- 利用Lucene对大文件进行预处理(可运行)
- 使用Lucene对建立的索引进行搜索
- Lucene索引前对doc pdf html文件的预处理
- Lucene索引前对doc pdf html文件的预处理
- 初识 Lucene(对目录文档进行检索,各种类的含义及使用)
- Lucene对mysql创建索引
- Lucene使用(一)简单索引的创建和检索
- Lucene使用Sort方法对文档进行排序
- lucene--索引的创建
- 使用Lucene对doc、docx、pdf、txt文档进行全文检索功能的实现
- lucene对数字和日期类型索引的创建
- lucene-创建文档索引处理框架
- lucene series 1 document 文档 索引创建
- [lucene]关于对xml的lucene索引
- Lucene对索引的查询
- 利用Lucene将大文档切割成多个小文档,(可运行)
- Lucene创建索引的过程
- hosts 本机DNS域名解析
- How do I accept a self-signed SSL certificate using iOS 7's NSURLSession and its family of delegate
- 《Java编程思想》学习笔记6——Java动态代理
- Maven 问题总结
- Objective-C Runtime 运行时
- 使用Lucene对预处理后的文档进行创建索引(可运行)
- Android自定义状态栏通知(Status Notification)的正确实现
- git 工具的安装与 与github的连接
- 搭建Cocos2d-JS开发环境
- Input Layout
- git 给远程库 添加多个url地址
- 解决Input Text文本垂直居中的简单快速有效的方法
- telnet发送邮件
- SSL探索03