基于lucene的案例开发:创建索引

释放双眼,带上耳机,听听看~!

      从这篇博客开始,不论是API介绍还是后面的案例开发,都是基于 lucene4.3.1 这个版本,Lucene4.3.1 下载请点击这里, Lucene其他版本下载请点击这里,Lucene4.3.1官方API文档请点击这里

创建索引demo

      在开始介绍之前,先看一个简单的索引创建demo程序:


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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
1/**  
2 *@Description:   索引创建demo
3 */
4package com.lulei.lucene.study;  
5
6import java.io.File;
7
8import org.apache.lucene.analysis.Analyzer;
9import org.apache.lucene.analysis.standard.StandardAnalyzer;
10import org.apache.lucene.document.Document;
11import org.apache.lucene.document.Field.Store;
12import org.apache.lucene.document.TextField;
13import org.apache.lucene.index.IndexWriter;
14import org.apache.lucene.index.IndexWriterConfig;
15import org.apache.lucene.index.IndexWriterConfig.OpenMode;
16import org.apache.lucene.store.Directory;
17import org.apache.lucene.store.FSDirectory;
18import org.apache.lucene.util.Version;
19  
20public class IndexCreate {
21
22  public static void main(String[] args) {
23      //指定索引分词技术,这里使用的是标准分词
24      Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
25      //indexwriter 配置信息
26      IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_43, analyzer);
27      //索引的打开方式,没有索引文件就新建,有就打开
28      indexWriterConfig.setOpenMode(OpenMode.CREATE_OR_APPEND);
29      Directory directory = null;
30      IndexWriter indexWrite = null;
31      try {
32          //指定索引硬盘存储路径
33          directory = FSDirectory.open(new File("D://study/index/testindex"));
34          //如果索引处于锁定状态,则解锁
35          if (IndexWriter.isLocked(directory)){
36              IndexWriter.unlock(directory);
37          }
38          //指定所以操作对象indexWrite
39          indexWrite = new IndexWriter(directory, indexWriterConfig);
40      } catch (Exception e) {
41          e.printStackTrace();
42      }
43     
44      //创建文档一
45      Document doc1 = new Document();
46      //对name域赋值“测试标题”,存储域值信息
47      doc1.add(new TextField("name", "测试标题", Store.YES));
48      //对content域赋值“测试标题”,存储域值信息
49      doc1.add(new TextField("content", "测试内容", Store.YES));
50      try {
51          //将文档写入到索引中
52          indexWrite.addDocument(doc1);
53      } catch (Exception e) {
54          e.printStackTrace();
55      }
56     
57      //创建文档二
58      Document doc2 = new Document();
59      doc2.add(new TextField("name", "基于lucene的案例开发:索引数学模型", Store.YES));
60      doc2.add(new TextField("content", "lucene将一篇文档分成若干个域,每个域又分成若干个词元,通过词元在文档中的重要程度,将文档转化为N维的空间向量,通过计算两个向量之间的夹角余弦值来计算两个文档的相似程度", Store.YES));
61      try {
62          //将文档写入到索引中
63          indexWrite.addDocument(doc2);
64      } catch (Exception e) {
65          e.printStackTrace();
66      }
67     
68      //将indexWrite操作提交,如果不提交,之前的操作将不会保存到硬盘
69      try {
70          //这一步很消耗系统资源,所以commit操作需要有一定的策略
71          indexWrite.commit();
72          //关闭资源
73          indexWrite.close();
74          directory.close();
75      } catch (Exception e) {
76          e.printStackTrace();
77      }
78  }
79}
80

1
2
1      在上述的程序中,已做了详细的注释,对每一条语句的作用就不再介绍,下面就看一下执行这个main函数之后创建的索引文件,如下图:  
2

      通过索引查看工具 luke 可以简单的看下索引中的内容,如下图:

      从上面两张图,我们可以看出索引中一共有两个文档,content域有50个词,name域有18个词,索引中存储了文档的详细信息。

创建索引核心类

      在上述创建索引过程中,用到了几个核心类:IndexWriterDirectoryAnalyzerDocumentField

IndexWriter

      IndexWriter(写索引)是索引过程中的核心组件,这个类负责创建新的索引或打开已有的索引以及向索引中添加、删除、更新被索引的文档信息;IndexWriter需要开辟一定空间来存储索引,该功能可以由Directory完成。

Directory

      Directory类描述了Lucene索引的存放位置。它是一个抽象类,它的子类负责指定索引的存储路径,在前面的例子中,我们用的是FSDirectory.open方法来获取真实文件在文件系统中的存储路径,然后将他们依次传递给IndexWriter类构造方法。

Analyzer

      文档信息在被索引之前需要经过Analyzer(分析器)处理,上述例子中使用的是标准分词,在以后的博客中会单独介绍各种分词器以及使用场景。

Document

      Document对象的结构比较简单,为一个包含多个Field对象的容器,上述事例中的文档就包含两个域 name、 content。

Filed

      索引中的每一个文档都包含一个或多个域不同命名的域,每个域都有一个域名和对应的域值以及一组选项来精确控制Lucene索引操作各个域值。在搜索时,所有域的文本就好像连接在一起,作为一个文本域来处理。

上述几个核心类在Lucene的操作中非常重要而且常用,如需要详细了解,还请参照官方API文档

给TA打赏
共{{data.count}}人
人已打赏
安全运维

OpenSSH-8.7p1离线升级修复安全漏洞

2021-10-23 10:13:25

安全运维

设计模式的设计原则

2021-12-12 17:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索