Lucene学习笔记一(基础)

本文介绍了使用Lucene创建文本索引的过程,包括定义不同字段的存储方式、索引方式及TermVector设置等细节,并提供了具体的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在创建索引的时候,
Field.Store,表示对域的存储
Field.Store.YES:存储字段值(未分词前的字段值)
Field.Store.NO:不存储,存储与索引没有关系
Field.Store.COMPRESS:压缩存储,用于长文本或二进制,但性能受损
Field.Index,表示对域的搜索
Field.Index.ANALYZED:分词建索引
Field.Index.ANALYZED_NO_NORMS:分词建索引,但是Field的值不像通常那样
被保 存,而是只取一个byte,这样节约存储空间
Field.Index.NOT_ANALYZED:不分词且索引
Field.Index.NOT_ANALYZED_NO_NORMS:不分词建索引,Field的值去一个byte保存


TermVector表示文档的条目(由一个Document和Field定位)和它们在当前文档中所出现的次数
Field.TermVector.YES:为每个文档(Document)存储该字段的TermVector
Field.TermVector.NO:不存储TermVector
Field.TermVector.WITH_POSITIONS:存储位置
Field.TermVector.WITH_OFFSETS:存储偏移量
Field.TermVector.WITH_POSITIONS_OFFSETS:存储位置和偏移量


创建索引的简单示例:


/**
* 创建索引(Indexing),生成索引文件
*/
private static void createTextIndex() {
// public final static String INDEX_SOURCE_DIR = "D:\\tomcat-7.0.22";// 要检索的文件夹
//
// public final static String INDEX_STORE_DIR = "d:\\fileIndex";// 索引文件的存放位置
//获取要索引的文件
File baseDir = new File(INDEX_SOURCE_DIR);
List<File> subFiles = new ArrayList<File>();
if (!baseDir.exists()) {
System.out.println("不存在" + INDEX_SOURCE_DIR + "目录");
System.exit(1);
} else if (baseDir.isDirectory()) {
subFiles = FileUtils.searchFilesByType(baseDir, subFiles, ".html");
}

//创建存放索引文件的目录
File indexDir = new File(INDEX_STORE_DIR);
if (!indexDir.exists()) {
indexDir.mkdir();
}

//索引分析器
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);

//创建索引器(核心)
IndexWriter indexWriter = null;
long startTime = 0;
try {
indexWriter = new IndexWriter(FSDirectory.open(indexDir),luceneAnalyzer, true, IndexWriter.MaxFieldLength.LIMITED);
indexWriter.setMaxMergeDocs(5);
indexWriter.setMergeFactor(5);
//不建立复合式索引文件,默认的情况下是复合式的索引文件
indexWriter.setUseCompoundFile(false);

startTime = new Date().getTime();
// 增加document到索引去
for (int i = 0; i < subFiles.size(); i++) {
if(subFiles.get(i).getName().indexOf(".")<=0){
continue;
}
System.out.println(new Date()+" File " + subFiles.get(i).getCanonicalPath()+ " 正在被索引....");

// 将txt 文件写到 Document中
Document document = new Document();

String fileName=subFiles.get(i).getName();//.substring(0,subFiles.get(i).getName().indexOf("."));
Field fieldName = new Field("fileName",fileName,Field.Store.NO,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(fieldName);

String filePath=subFiles.get(i).getPath();
Field FieldPath = new Field("filePath",filePath,Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldPath);

String contents = FileUtils.fileReaderAll(subFiles.get(i).getCanonicalPath(),"GBK");// 转化成GBK
Field FieldBody = new Field("contents", contents, Field.Store.YES,Field.Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS);
document.add(FieldBody);

indexWriter.addDocument(document);
}

} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
// optimize()方法是对索引进行优化
indexWriter.optimize();
indexWriter.close();
// 测试一下索引的时间
long endTime = new Date().getTime();
System.out.println("这花费了" + (endTime - startTime)+" 毫秒来把文档增加到索引里面去!\n"+indexDir.getPath());
File[] fils=indexDir.listFiles();
for(int i=0;i<fils.length;i++){
System.out.println(fils[i].getCanonicalPath());
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值