3.5 实例讲解Lucene索引的结构设计
3.2节我们已经运行了一个Lucene建立索引的小程序,这一节我们就以这个小程序为例讲解一下Lucene建立索引的过程。
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.*; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
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; /**
* @author csl
* @description:
* 依赖jar:Lucene-core,lucene-analyzers-common,lucene-queryparser
* 作用:简单的索引建立
*/
public class Indexer {
public static Version luceneVersion = Version.LATEST;
/**
* 建立索引
*/
public static void createIndex(){
IndexWriter writer = null;
try{
//1、创建Directory
//Directory directory = new RAMDirectory();//创建内存directory
Directory directory = FSDirectory.open(Paths.get("index"));//在硬盘上生成Directory00
//2、创建IndexWriter
IndexWriterConfig iwConfig = new IndexWriterConfig( new StandardAnalyzer());
writer = new IndexWriter(directory, iwConfig);
//3、创建document对象
Document document = null;
//4、为document添加field对象
File f = new File("raw");//索引源文件位置
for (File file:f.listFiles()){
document = new Document();
document.add(new StringField("path", f.getName(),Field.Store.YES));
System.out.println(file.getName());
document.add(new StringField("name", file.getName(),Field.Store.YES));
InputStream stream = Files.newInputStream(Paths.get(file.toString()));
document.add(new TextField("content", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));//textField内容会进行分词
//document.add(new TextField("content", new FileReader(file))); 如果不用utf-8编码的话直接用这个就可以了
writer.addDocument(document);
}
}catch(Exception e){
e.printStackTrace();
}finally{
//6、使用完成后需要将writer进行关闭
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws IOException
{
createIndex();
}
}
创建索引共六步:
1.创建索引目录。
Directory directory = new RAMDirectory();
Directory directory = FSDirectory.open(Paths.get("index"));
创建索引目录有两种方式:
- RAMDirectory类:创建一个内存目录,优点是速度快,缺点是程序退出后索引目录数据就会丢失。
- FSDirectory类: 创建一个文件目录,该方式创建的索引数据保存在磁盘上,不会因为程序的退出而消失。
下文针对FSDirectory方式来讲解Lucene的基本使用。
2.创建IndexWriter。
IndexWriterConfig iwConfig = new IndexWriterConfig( new StandardAnalyzer());
IndexWriter writer = new IndexWriter(directory, iwConfig);
通过IndexWriter对象来创建和维护索引。
IndexWriterConfig对象用来对IndexWriter进行初始配置:配置分词器;配置索引维护的方式;配置用来缓冲文档的RAM大小等。
具体可参照IndexWriterrConfig文档根据需求进行个性化配置。
3. 创建Document。
Document doc=new Document();
Document是Lucene建立索引的基本单元,相当于数据库的关系表。
4. 添加Field。
document = new Document();
document.add(new StringField("path", f.getName(),Field.Store.YES));
System.out.println(file.getName());
document.add(new StringField("name", file.getName(),Field.Store.YES));
InputStream stream = Files.newInputStream(Paths.get(file.toString()));
document.add(new TextField("content", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));//textField内容会进行分词
//document.add(new TextField("content", new FileReader(file))); 如果不用utf-8编码的话直接用这个就可以了
Field是Lucene建立索引的最小单元,相当于关系表中的属性。一个Document可以包含多个Field。Document添加Field只需调用Add()方法。
Lucene为我们提供了多种类型的Field,比如IntField, LongField, StringField, TextField等。程序实例中,我们用到了StringField和TextField。我们有必要来了解一下这两种Field的区别,因为这关系到倒排表的建立:
- StringField:对域进行索引,但不进行分词,将域值作为单一的语汇单元,适用于索引那些不能被分解的域值,如URL,文件路径,电话号码等。参考StringField文档。
- TextField: 对域既索引又分词,Lucene会对这个域进行分词并建立倒排表。参考TextField文档。
5.添加Document。
对IndexWriter对象调用addDocument方法将文档添加到索引库中。
6.关闭IndexWriter对象。
把所有的文档都添加到索引库中后,关闭Indexwriter对象。
ps:这篇博客以文集为例形象生动地说明了IndexWriter,Document和Field的关系,大家不妨看一看:例子
关于Lucene的具体索引步骤就介绍到这里~~
.
最新文章
- PDF虚拟打印机
- Daily Scrum Meeting ——FirstDay
- C#中XML和json互相转换
- C# 生成条形码图片,效果不错
- python数据类型详解及列表字典集合推导式详解
- 对Get-Content参数-readcount的解释
- JqueryMobile 跳转问题
- MAX(A,B)
- javascript权威指南第六版学习
- UIImage载入图片的几种方式及差别
- node.js入门系列(一)--Node.js简介
- Dev控件学习-GridControl中的BandGridview导出多层行头操作
- Daily Codeforces 计划 训练时录
- ITEXT5.5.8转html为pdf文档解决linux不显示中文问题
- meta标签的http-equiv与content解析
- codeforces#580 D. Kefa and Dishes(状压dp)
- Leetcode 167. 两数之和 II - 输入有序数组 By Python
- await这个关键词以及asyncio.wait asyncio.gather
- Python3学习之路~5.5 sys模块
- 学习笔记之深度学习(Deep Learning)