Lucene整理--索引的建立

看lucene主页(http://lucene.apache.org/)上眼下lucene已经到4.9.0版本号了, 參考学习的书是依照2.1版本号解说的,写的代码样例是用的3.0.2版本号的,版本号

的不同导致有些方法的使用差异,可是大体还是同样的。

源码用到的jar包(3.0.2版本号)下载地址

參考资料:

1、公司内部培训资料

2、《Lucene搜索引擎开发权威经典》于天恩著.

Lucene使用挺简单的,耐心看完都能学会,还有源码。

一、创建索引的基本方式

全部开源搜索引擎的基本架构和原理都是类似的,Lucene也不例外,用它来建立搜索引擎也是要解决的四个基本问题:抓取数据、解析数据、创建索引和运行搜索。

1、理解创建索引的过程

參考书中对索引的创建有一个非常形象的比喻:

创建索引的过程能够类比为写文集。以下以文集的写作为例进行解说,文集里面有很多文章,每一篇文章包含标题、内容、作品名称、写作时间等信息。

我们採用下面的方式来写这本文集:先写文章,再将文章整合起来。

首先为每一篇文章加入标题、内容、写作时间等信息,从而写好一篇文章。

然后把每一篇文章加入到书里面去,这样文集就写好了。

文集的结构例如以下图所看到的:按从左到右的方向,是读文集,即打开一本书,然后翻阅里面的文章。按从右到左的方向是写文集。


创建索引的步骤例如以下:

(1)、建立索引器IndexWriter。这相当于一本书的框架

(2)、建立文档对象Document,这相当于一篇文章

(3)、建立信息字段对象Field,这相当于一篇文章中的不同信息(标题、正文等)。

(4)、将Field加入到Document里面。

(5)、将Document加入到IndexWriter里面。

(6)、关闭索引器IndexWriter。

例如以下图所看到的是一个索引的结构,按从左到右是读索引(即搜索)。按从右到左是创建索引:


依照上图所看到的的结构,创建索引有三个主要的步骤:

(1)、创建Field,将文章的不同信息包装起来

(2)、将多个Field组织到一个Document里面。这样完毕了对一篇文章的包装。

(3)、将多个Document组织到一个IndexWriter里面,也就是将多个文章组装起来,终于形成索引

以下的三个小节就依照创建索引的基本步骤来解说创建索引的详细方法。

2、创建Field

创建Field的方法有很多。以下是最经常使用的方法。

Field field=new Field(Field名称,Field内容,存储方式,索引方式);

这四个參数的含义例如以下:

(1)、Field名称就是为Field起的名字。类似于数据表的字段名称。

(2)、Field内容就是该Field的内容,类似数据库表的字段内容。

(3)、存储方式包含三种:不存储(Field.Store.NO)、全然存储(Field.Store.YES)和压缩存储(Field.Store.COMPRESS)。

通常。假设不操心索引太大的话。能够都使用全然存储的方式。

可是。出于对性能的考虑。索引文件的内容是越小越好。因此。假设Field的内容非常少就採用全然存储(如标

题),假设Field的内容非常多就採用不存储或压缩存储的方式,如正文。

 (4)、索引的方式包含四种:

不索引(Field.Index.NO)、索引但不分析(Field.Index.NO_NORMS)、索引但不分词(Field.Index.UN_TOKENIZED)、分词并索引(Field.Index.TOKENIZED)。

3、创建Document

创建Document方法例如以下:

Document doc=new Document();

这种方法用来创建一个不含有不论什么Field的空Document。

假设想把Field加入到Document里面,仅仅须要add方法。比如doc.add(field);

反复的使用就能够将多个Field增加到一个Document里面。

4、创建IndexWriter

创建IndexWriter方式也不少,拿出一个经常使用的:

<span style="font-family:SimSun;font-size:12px;"><span style="font-family:SimSun;font-size:12px;">			File indexDir = new File("E:\Index");
			Directory dir = new SimpleFSDirectory(indexDir);
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
			IndexWriter indexWriter = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);</span></span>

关于几个參数的介绍:

(1)、Directory (文件夹类型) 该类是抽象类

它的直接子类在不同版本号中还是有差别的,我们直说3.0.2版本号,它的子类包含FSDirectoryRAMDirectoryFileSwitchDirectory、CompoundFileReader,两个子类代表着不同的两种文件夹类型

FSDirectory:文件系统中的一个路径,会直接将索引写入到磁盘上

RAMDirectory:内存中的一个区域,虚拟机退出后内容会随之消失,所以须要将RAMDirectory中的内容转到FSDirectory

FileSwitchDirectory:一个是用于能够同一时候在两个不同的文件夹中读取文件的FileSwitchDirectory,这是个代理类。

CompoundFileReader:是用户读取复合文件的CompoundFileReader。仅仅能读取扩展名为cfs的文件。(写扩展名为cfs的文件用CompoundFileWriter)CompoundFileReader仅在SegmentReader中被引用。

当中FSDirectory又分为3类:

A、windows下的SimpleFSDirectory

B、linux支持NIO的NIOFSDirectory

C、另一个是内存Map文件夹MMapDirectory


(2)、Analyzer(分析器) 抽象类

负责对各种输入的数据源进行分析,包含过滤和分词等多种功能。

分析器是用来做词法分析的,包含英文分析器和中文分析器等。要依据所要建立的索引的文件情况选择合适的分析器。经常使用的有StandardAnalyzer(标准分析器)、CJKAnalyzer(二分法分词

器)、ChineseAnalyzer(中文分析器)等。

能够依据自己的须要去编辑分析器,从而处理不同 的语言文字(当然,我不会)。

IndexWriter创建完之后能够用addDocument()的方法将document对象放置到IndexWriter中:writer.addDocument(doc);

能够放置多个。

终于要调用close()方法关闭索引器,比如writer.close();


到此,创建索引的步骤就介绍完了,以下我们就该看实例了:

二、创建一个简单索引实例:

<span style="font-family:SimSun;font-size:12px;">import java.io.File;

import org.apache.lucene.analysis.Analyzer;
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.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;

public class LuceneMainProcess {

	public static void main(String[] args) {
		createLuceneIndex();
	}

	public static void createLuceneIndex() {
		try {
			File indexDir = new File("E:\Index");
			Directory dir = new SimpleFSDirectory(indexDir);
			Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
			//IndexWriter为某个Document建立索引时所取到的Field内的最大词条数目,该属性的初始值为10000
			IndexWriter indexWriter = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
			// 创建8个文档
			Document doc1 = new Document();
			Document doc2 = new Document();
			Document doc3 = new Document();
			Document doc4 = new Document();
			Document doc5 = new Document();
			Document doc6 = new Document();
			Document doc7 = new Document();
			Document doc8 = new Document();

			Field f1 = new Field("bookname", "钢铁是如何炼成的", Field.Store.YES,
					Field.Index.ANALYZED);
			Field f2 = new Field("bookname", "英雄儿女", Field.Store.YES,
					Field.Index.ANALYZED);
			Field f3 = new Field("bookname", "篱笆女人和狗", Field.Store.YES,
					Field.Index.ANALYZED);
			Field f4 = new Field("bookname", "女人是水做的", Field.Store.YES,
					Field.Index.ANALYZED);
			Field f5 = new Field("bookname", "我的兄弟和女儿", Field.Store.YES,
					Field.Index.ANALYZED);
			Field f6 = new Field("bookname", "白毛女", Field.Store.YES,
					Field.Index.ANALYZED);
			Field f7 = new Field("bookname", "钢的世界", Field.Store.YES,
					Field.Index.ANALYZED);
			Field f8 = new Field("bookname", "钢铁战士", Field.Store.YES,
					Field.Index.ANALYZED);

			doc1.add(f1);
			doc2.add(f2);
			doc3.add(f3);
			doc4.add(f4);
			doc5.add(f5);
			doc6.add(f6);
			doc7.add(f7);
			doc8.add(f8);

			indexWriter.addDocument(doc1);
			indexWriter.addDocument(doc2);
			indexWriter.addDocument(doc3);
			indexWriter.addDocument(doc4);
			indexWriter.addDocument(doc5);
			indexWriter.addDocument(doc6);
			indexWriter.addDocument(doc7);
			indexWriter.addDocument(doc8);

			indexWriter.optimize();//对索引进行优化,保证检索时的速度,可是须要消耗内存和磁盘空间,耗时耗力,须要的时候再优化(而非随意时刻)
			indexWriter.close();//关闭索引器,否则会导致索引的数据滞留在缓存中未写入磁盘,有可能连文件夹的锁也没有去除
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
</span>

运行之后我们发现E盘下出现了一个index文件夹,这就是我们创建的索引文件。

到此,索引文件也就是创建完成了。

明天接着往下整理索引的读取,到时候我们就能够更直观的推断上面创建的索引是否成功。






原文地址:https://www.cnblogs.com/liguangsunls/p/7390801.html