【Lucene实验1】构建索引

一、实验名称:构建索引

二、实验日期:2013/9/21

三、实验目的:

1)        能理解Lucene中的Document-Field结构的数据建模过程;

2)        能编针对特定数据生成索引文件。

实验用的仪器和材料

MyEclipse 10,JDK

 

实验的步骤和方法

题目一:在指定目录生成表示3本书的索引,要求建立3个document分别存放书名数据。把生成的索引文件截好图(复合索引与一般索引各生成一次)

 

图1:一般索引的截图

图2:复合索引的截图

题目二:修改题目一的代码,使用多值域在一个文档中存放3本书的书名值。

题目三:针对题目一的三个文档,分别做如下操作:根据书名在索引中删除一个值、修改一个文档的域值。

实验过程:

题目一源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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();
    }
}

  

题目二源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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();
    }
}

 

题目三源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  //题目三
    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

 

posted @   钟汉文  阅读(308)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示