【Lucene实验1】构建索引
一、实验名称:构建索引
二、实验日期:2013/9/21
三、实验目的:
1) 能理解Lucene中的Document-Field结构的数据建模过程;
2) 能编针对特定数据生成索引文件。
四、实验用的仪器和材料:
MyEclipse 10,JDK
五、实验的步骤和方法:
题目一:在指定目录生成表示3本书的索引,要求建立3个document分别存放书名数据。把生成的索引文件截好图(复合索引与一般索引各生成一次)

图1:一般索引的截图

图2:复合索引的截图
题目二:修改题目一的代码,使用多值域在一个文档中存放3本书的书名值。
题目三:针对题目一的三个文档,分别做如下操作:根据书名在索引中删除一个值、修改一个文档的域值。
实验过程:
题目一源代码:
package lab02; import java.io.File;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; public class GreatIndex {
public static void main(String[] args) {
GreatIndex GreateIndexobj=new GreatIndex();
try {
GreateIndexobj.setUp();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private String indexDir="E:/Users/Administrator/Workspaces/MyEclipse 10/mylucene/src/lab02/index";
private Directory directory; //表示索引存放的目录
public void setUp()throws Exception {
//directory =new RAMDirectory(); //索引存放在内存的RAM中
directory =FSDirectory.open((new File(indexDir))); //索引存放在物理硬盘的文件系统内(就是存放指定路径)
IndexWriter writer=new IndexWriter(directory,
new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
//write.setUseCompoundFile(false);//设置false就是使用一般索引(有多种文件的)
//建立3本书的document
Document doc1=new Document();
Document doc2=new Document();
Document doc3=new Document();
//建立名字叫“bookname”的field并添加域值到文档中,设置国域值存储到索引中,不被分词与加权
doc1.add(new Field("bookname", "伐清",
Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
doc2.add(new Field("bookname", "奥术神座",
Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
doc1.add(new Field("bookname", "冰与火之歌",
Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
writer.addDocument(doc1);
writer.addDocument(doc2);
writer.addDocument(doc3);
writer.close();
}
}
题目二源代码:
package lab02; import java.io.File;
import java.io.IOException; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version; public class GreatIndex {
public static void main(String[] args) {
GreatIndex GreateIndexobj=new GreatIndex();
try {
//GreateIndexobj.setUp();
GreateIndexobj.setUp2();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
private String indexDir="E:/Users/Administrator/Workspaces/MyEclipse 10/mylucene/src/lab02/index";
private Directory directory; //表示索引存放的目录
private String[] booknames={"伐清","奥术神座","冰与火之歌"};
public void setUp2() throws Exception{
//directory =new RAMDirectory(); //索引存放在内存的RAM中
directory =FSDirectory.open((new File(indexDir))); //索引存放在物理硬盘的文件系统内(就是存放指定路径)
IndexWriter writer=new IndexWriter(directory,
new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
//writer.setUseCompoundFile(false);//设置false就是使用一般索引(有多种文件的)
//建立包含三个域值的document
Document doc=new Document();
for (String bookname:booknames) {
doc.add(new Field("bookname",bookname,Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
}
writer.addDocument(doc);
writer.close();
}
}
题目三源代码:
//题目三
public void DeleteDocument()throws IOException{
IndexWriter writer=new IndexWriter(directory,
new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
writer.optimize();//使用优化策略删除文档(直接删除,不能回复)
writer.deleteDocuments(new Trem("bookname", "伐清"));
writer.close();
}
public void UpdateDocument() throws IOException{
IndexWriter writer=new IndexWriter(directory,
new StandardAnalyzer(Version.LUCENE_30),true,IndexWriter.MaxFieldLength.UNLIMITED);
//构建一个新的document用与替换
Document doc=new Document();
doc.add(new Field("bookname","Lucene实战第二版",
Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS));
writer.updateDocument(new Term("bookname","官仙"), doc);
writer.close();
}
六、数据记录和计算:
项目的结构图:



七、实验结果或结论:
总结:通过这次的实验,我基本理解Lucene中的Document-Field结构的数据建模过程, 能编针对特定数据生成索引文件.在这次的实验过程中,实验不是很顺利,这次实验让我感受到了Lucene的强大,增加我对Lucene的兴趣!
八、备注或说明:
九、引用参考文献:
http://lucene.apache.org
【Lucene实验1】构建索引的更多相关文章
- 如何提高Lucene构建索引的速度
如何提高Lucene构建索引的速度 hans(汉斯) 2013-01-27 10:12 对于Lucene>=2.3:IndexWriter可以自行根据内存使用来释放缓存.调用writer.set ...
- 【Lucene】Apache Lucene全文检索引擎架构之构建索引2
上一篇博文中已经对全文检索有了一定的了解,这篇文章主要来总结一下全文检索的第一步:构建索引.其实上一篇博文中的示例程序已经对构建索引写了一段程序了,而且那个程序还是挺完善的.不过从知识点的完整性来考虑 ...
- 利用Lucene与Nutch构建简单的全文搜索引擎
文章地址 1.简介 本次实现分为两个部分,第一个部分是利用Lucene构建一个全文的搜索引擎,另外一部分则是利用Nutch实现同样的功能.由于Lucene并不是一个可以直接运行的程序,且不具备爬虫和文 ...
- 构建NCBI本地BLAST数据库 (NR NT等) | blastx/diamond使用方法 | blast构建索引 | makeblastdb
参考链接: FTP README 如何下载 NCBI NR NT数据库? 下载blast:ftp://ftp.ncbi.nlm.nih.gov/blast/executables/blast+ 先了解 ...
- 【转】Lucene工作原理——反向索引
原文链接: http://my.oschina.net/wangfree/blog/77045 倒排索引 倒排索引(反向索引) 倒排索引源于实际应用中需要根据属性的值来查找记录.这种索引表中的每一项 ...
- 如何使用Spark大规模并行构建索引
使用Spark构建索引非常简单,因为spark提供了更高级的抽象rdd分布式弹性数据集,相比以前的使用Hadoop的MapReduce来构建大规模索引,Spark具有更灵活的api操作,性能更高,语法 ...
- Lucene实战构建索引
搭建lucene的步骤这里就不详细介绍了,无外乎就是下载相关jar包,在eclipse中新建java工程,引入相关的jar包即可 本文主要在没有剖析lucene的源码之前实战一下,通过实战来促进研究 ...
- Lucene构建索引时的一些概念和索引构建的过程
在搜索文档内容之前要做的事情就是对从各种不同来源(网页,数据库,电子邮件等)的文档进行索引,索引的过程就是对内容进行提取,规范化(通过对内容进行建模来实现),然后存储. 在索引的过程中有几个基本的概念 ...
- lucene 3.0.2 + 多文件夹微博数据(时间,微博)构建索引
package lia.meetlucene; import java.io.File; import java.io.IOException; import java.util.LinkedList ...
随机推荐
- Semiconnected--强连通缩点
1451: Semiconnected 时间限制: 1 Sec 内存限制: 32 MB 提交: 79 解决: 20 题目描述 For a directed graph G = (V, E), if ...
- 2016开发一个app需要多少钱?app开发需要哪些成本-app开发问题汇总-广州达到信息
作为一个APP开发从业者,被外行的朋友们问及最多的问题是,"做一个网站需要多少钱?"或者"开发一个APP需要多少钱?".作为开发过完整网站项目和手机APP的人, ...
- #include <NOIP2009 Junior> 细胞分裂 ——using namespace wxl;
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...
- UESTC 883 方老师与两个串 --二分搜索+DP
CF原题 由题可知,n,m太大,无法开出dp[n][m]的数组. 观察发现s/e最大为300,也就是说,选用第一种操作的次数不会超过300. 于是定义dp[i][j],第一个串的前i个数,使用了j次第 ...
- MySQL数据库学习笔记(四)----MySQL聚合函数、控制流程函数(含navicat软件的介绍)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- Windows远程连接MAC桌面
一.准备软件 VNC Server (MAC OS X已支持) RealVNC/TightVNC 二.MAC OS X设置 注:Mac OS X 10.5 已经支持了VNC Viewer访问的功能,设 ...
- LoadRunner支持的IE版本
LoadRunner支持的IE版本: 8.0 最高ie68.1 最高ie69.0 最高ie79.5 最高ie811.0 最高ie9( win7 32位+LR11+IE10可用,但win7 64位+LR ...
- 自定义WPF ListBox的选中项样式
首先介绍一种简单地方法:就是通过自定义SystemColors类的参数来自定义WPF ListBox选择颜色的,SystemColors的HighlightBrushKey和HighlightText ...
- RequiredFieldValidator 根据group组来触发验证
今天在开发过程中遇到了这样一个问题 在这个用户添加界面中,我使用了多个验证控件RequiredFieldValidator,分别控制用户名密码.在默认情况下,当单击“检查用户名”时,密码的验证控件也被 ...
- 收集的User-Agent
headers = [ {"User-Agent": "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; A ...