package cn.lucene.lesson1;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.analysis.Analyzer;
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文件索引
*/
public class TxtFileIndex2 {
private Directory directory;
private String indexPath = "D://lucene/index1"; // 建立索引文件的目录
private String dirPath = "D://lucene/data"; // 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_43, 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;
}
}
最近下载更多
最近浏览更多
szf123 LV12
2023年12月10日
微信网友_6040315240812544 LV8
2022年10月20日
19895630790 LV1
2022年7月1日
chenhongjie LV5
2021年10月21日
Altria LV5
2021年8月13日
kilord LV1
2021年5月31日
ronnie0507 LV8
2021年5月17日
lodddy LV6
2021年5月4日
3270302262
2021年3月27日
暂无贡献等级
caozhaoqi83 LV5
2021年1月21日

