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)的过程,本次不包括这两部分,将在以后 ...
随机推荐
- django+xadmin在线教育平台(十二)
6-4 用form实现登录-1 上面我们的用户登录的方法是基于函数来做的.本节我们做一个基于类方法的版本. 要求对类的继承有了解. 基础教程中基本上都是基于函数来做的,其实更推荐基于类来做.基于类可以 ...
- python scrapy 实战简书网站保存数据到mysql
1:创建项目 2:创建爬虫 3:编写start.py文件用于运行爬虫程序 # -*- coding:utf-8 -*- #作者: baikai #创建时间: 2018/12/14 14:09 #文件: ...
- 007---Django的视图层
视图函数 一个视图函数,简称视图,是一个简单的python函数.它接收web请求并且返回web响应. 1.一张网页的HTML内容 2.一个重定向 3.一个404错误 4.一个xml文档 5.一个字符串 ...
- POJ:3262-Protecting the Flowers
Protecting the Flowers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8606 Accepted: 347 ...
- python-9-IO编程
1-文件读写 f = open('d:/file.txt','r') #如果文件不存在会报异常 print(f.read()) #一次性读取所有内容 f.close() 1.2 由于文件操作会用异常, ...
- format内置函数
1. 函数功能将一个数值进行格式化显示. 2. 如果参数format_spec未提供,则和调用str(value)效果相同,转换成字符串格式化. >>> format(3.14159 ...
- [bzoj2733]永无乡&&[bzoj3545]Peaks
并不敢说完全会了线段树合并,只是至少知道原理写法了...还是太菜了,每天被大佬吊锤qwq 我看到的几道线段树合并都是权值线段树的合并.这个算法适用范围应该只是01线段树的. 这两道算入门题了吧... ...
- c语言printf()输出格式大全(转载)
1.转换说明符 %a(%A) 浮点数.十六进制数字和p-(P-)记数法(C99) %c 字符 %d 有符号十进制整 ...
- 跨域携带cookie
$.ajax({ type: "GET", url: "https://upload.cnblogs.com/imageuploader/upload?host=www. ...
- Java重写与重载
重写的规则: 参数列表必须完全与被重写方法的相同: 返回类型必须完全与被重写方法的返回类型相同: 访问权限不能比父类中被重写的方法的访问权限更低.例如:如果父类的一个方法被声明为public,那么在子 ...