package com.searchtxt.lucene;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.Token;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.tokenattributes.TypeAttribute;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.TextField;
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.util.Version;
import org.junit.Before;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer;
/**
* txt文件索引
* @author lijunqing
*/
public class IndexFile {
private Directory directory;
private String indexPath = "D://lucene/index"; // 建立索引文件的目录
private String dirPath = "D://lucene/test"; // txt资源目录
private Analyzer analyzer = new IKAnalyzer();
private IndexWriter indexWriter;
@Before
public void init() {
try {
directory=FSDirectory.open(new File(indexPath));
indexWriter=getIndexWriter(directory);
} catch(Exception e) {
System.out.println("索引打开异常!");
}
}
/**
* 获得所有txt文件
* @param dirPath
* @return
*/
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;
}
/**
* 创建索引
* @throws Exception
*/
@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"));
indexWriter.commit();
}
closeWriter();
}
/**
* 判断是否是txt文件
* @param fileName
* @return
*/
public boolean isTxtFile(String fileName) {
if(fileName.lastIndexOf(".txt") > 0) {
return true;
}
return false;
}
/**
* 将文件转换成Document对象
* @param file
* @return
* @throws Exception
*/
public Document fileToDocument(File file) throws Exception {
Document document=new Document();
document.add(new TextField("filename", file.getName(), Store.YES));
document.add(new TextField("content", getFileContent(file), Store.YES));
document.add(new LongField("size", file.getTotalSpace(), Store.YES));
return document;
}
/**
* 获得indexwriter对象
* @param dir
* @return
* @throws Exception
*/
public IndexWriter getIndexWriter(Directory dir) throws Exception {
IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_40, analyzer);
return new IndexWriter(dir, iwc);
}
/**
* 关闭indexwriter对象
* @throws Exception
*/
public void closeWriter() throws Exception {
if(indexWriter != null) {
indexWriter.close();
}
}
/**
* 读取文件内容
* @param file
* @return
* @throws Exception
*/
public String getFileContent(File file) throws Exception{
Reader reader = new InputStreamReader(new FileInputStream(file),"GBK");
BufferedReader br = new BufferedReader(reader);
String result ="";
while(br.readLine() != null){
result = result+"\n"+br.readLine();
}
br.close();
reader.close();
return result;
}
}
说明:lucene4.0中TextField()中reader参数那个源代码中是
public TextField(String name, Reader reader) {
super(name, reader, TYPE_NOT_STORED);
}
而TYPE_NOT_STORED的定义为:是默认不保存的。
所以就先读出文件的内容用:
public TextField(String name, String value, Store store) {
super(name, value, store == Store.YES ? TYPE_STORED : TYPE_NOT_STORED);
}
这个方法。
分享到:
相关推荐
比较好用的版本,要求jdk1.6 lucene4.0+ikanalyzer2012
Ik中分分词器介绍,可用于lucene4.0
自己做的一些 lucene4.0的 demo 希望能帮助各位 写的不好 亲们 别喷我
简单的lucene4.0样例,包含新增和查询功能,包含lucene4.0jar包,适合jdk1.6。
lucene4.0创建索引,删除索引,搜索的一些小demo,分享出来供大家学习。
支持lucene4.0的paoding分词
lucene4.0常用jar包,里面有lucene-analyzers-common-4.0.0.jar,lucene-core-4.0.0.jar,lucene-highlighter-4.0.0.jar,lucene-queryparser-4.0.0.jar
IK分词 lucene4.0 老版本的分词器 可以性强 网上例子多
应用Lucene4.0版本时,发现用现有的IKAnalyzer2012.jar会有冲突,然后找到官网下载最新的IKAnalyzer2012源码,打包jar后解决
IK Analyzer 2012FF_hf1中文分词,lucene4.0搜索引擎的jar包。
IK Analyzer是一个结合词典分词和文法分词的中文分词开源工具包。它使用了全新的正向迭代最细粒度切分算法。可以结合lucene4.0使用
lucene 是全开源文索引搜索引擎,使用它可以很快的架设我们自己的搜索引擎,但遗憾的是它默认只对txt和html格式文档生成index,但我们常常要使用其他的文档如word,pdf等,我搜集了这几种文档生成index的笔记
在Eclipse环境中运用java,Lucene建索引及查询关键字
Lucene实现索引和查询的实例讲解Lucene实现索引和查询的实例讲解
lucene 做索引查询流程,来自《lucene in action》
Lucene创建索引步骤: 1、创建Directory(索引位置) 2、创建IndexWrite(写入索引) 3、创建Document对象 4、为Document添加Field(相当于添加属性:类似于表与字段的关系) 5、通过IndexWriter添加文档到索引中
Lucene索引器实例Lucene索引器实例Lucene索引器实例Lucene索引器实例
基于lucene的索引与搜索 基于lucene的索引与搜索
深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制
基于lucene技术的增量索引,实现索引的首次创建,动态增删改