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的具体索引步骤就介绍到这里~~
.
3.5 实例讲解Lucene索引的结构设计的更多相关文章
- 实例讲解Linux系统中硬链接与软链接的创建
导读 Linux链接分两种,一种被称为硬链接(Hard Link),另一种被称为符号链接(Symbolic Link).默认情况下,ln命令产生硬链接.硬链接与软链接的区别从根本上要从Inode节点说 ...
- Lucene 索引功能
Lucene 数据建模 基本概念 文档(doc): 文档是 Lucene 索引和搜索的原子单元,文档是一个包含多个域的容器. 域(field): 域包含“真正的”被搜索的内容,每一个域都有一个标识名称 ...
- Lucene学习总结之四:Lucene索引过程分析
对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...
- Html代码seo优化最佳布局实例讲解
搜索引擎对html代码是非常优化的,所以html的优化是做好推广的第一步.一个符合seo规则的代码大体如下界面所示. 1.<!–木庄网络博客–> 这个东西是些页面注释的,可以在这里加我的& ...
- 【MySQL】分页查询实例讲解
MySQL分页查询实例讲解 1. 前言 本文描述了团队在工作中遇到的一个MySQL分页查询问题,顺带讲解相关知识点,为后来者鉴.本文的重点不是"怎样"优化表结构和SQL语句,而是探 ...
- 深入Lucene索引机制
Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...
- Java入门系列:实例讲解ArrayList用法
本文通过实例讲解Java中如何使用ArrayList类. Java.util.ArrayList类是一个动态数组类型,也就是说,ArrayList对象既有数组的特征,也有链表的特征.可以随时从链表中添 ...
- Lucene索引文件组成
Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...
- Lucene学习总结之四:Lucene索引过程分析 2014-06-25 14:18 884人阅读 评论(0) 收藏
对于Lucene的索引过程,除了将词(Term)写入倒排表并最终写入Lucene的索引文件外,还包括分词(Analyzer)和合并段(merge segments)的过程,本次不包括这两部分,将在以后 ...
随机推荐
- 内置函数系列之 filter
filter 过滤 基本语法: s = filter(function,iterable) 将可迭代对象的每一个元素,传进函数中,根据函数中的判断条件,返回True或False 返回True的是保留的 ...
- JDK学习---深入理解java中的LinkedList
本文参考资料: 1.<大话数据结构> 2.http://blog.csdn.net/jzhf2012/article/details/8540543 3.http://blog.csdn. ...
- stark组件(11):组合搜索
效果图: 新增函数和类 Option 获取字段的对象或元组 SearchGroupRow 封装数据,展示到前端 get_search_group 获取组合搜索的字段 get_search_group_ ...
- POJ:3276-Face The Right Way(线性反转)
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 6259 Accepted: 2898 De ...
- Eclipse字体修改
第一步: 第二步: 第三步: 第四步: 第五步: 第六步:
- Android面试收集录7 AsyncTask详解
1.Android中的线程 在操作系统中,线程是操作系统调度的最小单元,同时线程又是一种受限的系统资源,即线程不可能无限制地产生, 并且 **线程的创建和销毁都会有相应的开销.**当系统中存在大量的线 ...
- Best Practices in JavaScript
Some items you should konw : Graceful degradation : ensuring that your web pages still work without ...
- android Intent onNewIntent 什么时候调用
1.activity A 的lanch model 为singleTop 此刻,A在activity 栈顶,那么就会调用A 的onNewIntent 如果A不在栈顶,则不会调用. 2.activity ...
- Eclipse 工作空间(Workspace)---Eclipse教程第07课
Eclipse 工作空间(Workspace) eclipse 工作空间包含以下资源: 项目 文件 文件夹 项目启动时一般可以设置工作空间,你可以将其设置为默认工作空间,下次启动后无需再配置: 工作空 ...
- 腾讯云分析MTA HTML5接入方法
从微信服务号或订阅号里跳转到自己项目后,想在项目中统计出实时数据.历史趋势.实时访客.新老访客比.访客画像.地域信息.运营商.终端信息.页面排行.性能监控.访问深度.外部链接.入口页面.离开页面.渠道 ...