你好,游客 登录 注册 搜索
背景:
阅读新闻

使用Lucene对预处理后的文档进行创建索引(可运行)

[日期:2015-03-20] 来源:Linux社区  作者:杨鑫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多索引进行索引和搜索 http://www.linuxidc.com/Linux/2012-05/59757.htm

Lucene 实战(第2版) 中文版 配套源代码 http://www.linuxidc.com/Linux/2013-10/91055.htm

Lucene 实战(第2版) PDF高清中文版 http://www.linuxidc.com/Linux/2013-10/91052.htm

使用Lucene-Spatial实现集成地理位置的全文检索 http://www.linuxidc.com/Linux/2012-02/53117.htm

Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a9 http://www.linuxidc.com/Linux/2012-02/53113.htm

Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a8 http://www.linuxidc.com/Linux/2012-02/53111.htm

Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a7 http://www.linuxidc.com/Linux/2012-02/53110.htm

Project 2-1: 配置Lucene, 建立WEB查询系统[Ubuntu 10.10] http://www.linuxidc.com/Linux/2010-11/30103.htm

--------------------------------------分割线 --------------------------------------

Lucene 的详细介绍请点这里
Lucene 的下载地址请点这里

本文永久更新链接地址http://www.linuxidc.com/Linux/2015-03/115263.htm

linux
相关资讯       Lucene索引  Lucene创建索引 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款