lucene创建索引失败--生成的索引文件有问题

ttcpu 2014-04-21 05:56:51
各位大神,小弟刚开始学习lucene,看了几天书了(lucnen in action2),打算实践一下写个小的demo。今天搞了一天,索引硬是建立不了。程序也么有错误。实在不知道哪里出问题了。请教各位。帮忙看看咋回事。
问题描述:
开发环境:windows系统、jdk1.8.0、lucene3.1
我的程序大致思路
1.先是把大文件切割成小文件(这个步骤没有问题,小文件已经生成)
2.自己写了一个建立索引的类,利用IndexWriter建立索引。这一步感觉有问题,因为生成的索引文件的文件名全部是以“_0”开头的,而且搜索也搜不到。根据lucene索引文件名生成规则,不应该全是“_0”开头的文件。
代码如下:
public class IndexProcesser {
//成员变量,存储创建的索引文件的位置
private String INDEX_STORE_PATH = "D:\\java_workspace\\Index";
//创建索引
public void createIndex(String inputDir) throws IOException{
Directory dir = FSDirectory.open(new File(INDEX_STORE_PATH));
try {
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_31);
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_31, luceneAnalyzer);
IndexWriter writer = new IndexWriter(dir,indexWriterConfig);
File filesDirFile = new File(inputDir);
//取得所有需要建立索引的文件数组
File[] files = filesDirFile.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();
//为文件名创建一个Field
Field field1 = new Field("name", files[i].getName(),Field.Store.YES,Index.ANALYZED);
Field field2 = new Field("content", loadFileToString(new File(files[i].getPath())),Field.Store.YES,Index.ANALYZED);
doc.add(field1);
doc.add(field2);
//把Document加入IndexWriter
writer.addDocument(doc);
}
}
writer.commit();
//关闭IndexWrite
writer.close();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 从文件中把内容读出,单个文件内的所有内容放在一个String中返回
*/
public String loadFileToString(File file){
try {
BufferedReader br = new BufferedReader(new FileReader(file));
StringBuffer sbBuffer = new StringBuffer();
String line = br.readLine();
while(line!=null){
sbBuffer.append(line);
line=br.readLine();
}
br.close();
return sbBuffer.toString();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
return null;
}
}
}


生成的索引文件如下图所示:
...全文
318 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ttcpu 2014-04-21
  • 打赏
  • 举报
回复
大牛都去吃饭了吗?

2,760

社区成员

发帖
与我相关
我的任务
社区描述
搜索引擎的服务器通过网络搜索软件或网络登录等方式,将Internet上大量网站的页面信息收集到本地,经过加工处理建立信息数据库和索引数据库。
社区管理员
  • 搜索引擎技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧