Lucene系列
来源:互联网 发布:淘宝助理如何同步宝贝 编辑:程序博客网 时间:2024/04/27 03:18
.3 Lucene索引实例:
5.2.1 索引创建代码解析
5.2.2 索引创建器(IndexWriter)
5.2.3 索引管理器(IndexReader)
5.2.4 索引修改器(IndexModifier)
5.2.5 索引分析器(Analyzer)
5.2.1 索引创建代码解析
索引系统可以采用IndexWriter来创建一个具体的索引,涉及四个类:Analyzer、Directory、Document和Field。
/** * 索引创建代码解析(4.x版本) */package com.gangwu.lucene.demo;import java.io.File;import java.io.IOException;import java.util.Date;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.core.SimpleAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.Field.Index;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;import junit.framework.TestCase;/** * @author root * */public class demo001 extends TestCase { private static String Dest_Index_Path = "/root/data/demo/";//索引存放路径 static protected String[] keywords = {"001", "002", "003", "004", "005"};//索引的数据关键字数组 static protected String[] textDetail = {"test1", "test2", "test3", "test4", "test5"};//索引的记录内容数组 public static void testCreateIndex() { try { Date date = new Date(); long startTime = date.getTime(); Analyzer analyzer = new SimpleAnalyzer();//文本分析器 Directory directory = FSDirectory.open(new File(Dest_Index_Path)); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); IndexWriter indexWriter = new IndexWriter(directory, conf); //循环读取数组记录,生成文档添加到索引中 for (int i = 0; i < 5; i++) { Document document = new Document();//生成文档对象 Field fieldId = new Field("id", keywords[i], Field.Store.YES, Index.NOT_ANALYZED);//生成关键字域 document.add(fieldId); Field fieldContent = new Field("content", textDetail[i], Field.Store.YES, Index.NOT_ANALYZED);//生成文本内容域 document.add(fieldContent); indexWriter.addDocument(document);//将文档对象添加到索引中 } indexWriter.forceMerge(2);//执行优化的方法,参数表示优化称几段索引 indexWriter.close(); Date endDate = new Date(); long endTime = endDate.getTime(); long indexTime = endTime - startTime; System.out.println("Total Time : " + indexTime + "(ms)"); } catch (IOException e) { e.printStackTrace(); } System.out.println("Create Index Success"); }}
上述例子,主要实现了索引的创建和文档的添加。
首先创建了一个Index实例,用来指定目录建立一系列文件保存索引。IndexWriter需要绑定一个分析器Analyzer,
然后循环创建文档类Document的实例,添加一个或多个域Field。
每个document可以对应一个实际的文档,也可以代表一系列属性的逻辑组合。
调用addDocument完成文档添加操作以后,索引的文档只是在内存中存在,只有在结束操作关闭索引时,Lucene才会把
新加的文档保存到硬盘索引文件里。
索引的创建主要通过索引操作类来完成。
5.2.2 索引创建器(IndexWriter)
IndexWriter是整个索引创建过程的核心和调度者。在指定目录建立索引文件,读取需要索引的文档内容。
Directory directory = FSDirectory.open(new File(Dest_Index_Path)); Analyzer analyzer = new SimpleAnalyzer(); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); IndexWriter indexWriter = new IndexWriter(directory, conf);
5.2.3 索引管理器(IndexReader)
IndexReader管理已经建立的Lucene索引,负责磁盘目录上存放的索引的加载和删除维护工作。
通过它的内部方法,可以统计当前索引中的文档数目等信息。
IndexReader使用open()方法来确定引用的具体磁盘索引文件和路径。
5.2.4 索引修改器(IndexModifier)
新版本的Lucene功能放在了IndexWriter下,如增加文档addDocument() 和 删除文档deleteDocument()
5.2.5 索引分析器(Analyzer)
Analuzer类用来给索引器提供预处理文本、无意义词过滤、垃圾信息删除、索引项识别确定等功能。
给索引器提供适合索引建立的一系列索引项。
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene系列
- Lucene.net 系列一
- Lucene.net 系列一
- Lucene.Net系列一
- Lucene.net 系列一
- Lucene.net 系列一
- lucene系列教程
- lucene系列教程
- netstat命令每项解释
- python面向对象入门
- SublimeText开发html网站教程(一)
- 入侵检测
- 蓝桥杯之周期串
- Lucene系列
- 日常(超不正经)
- 继承
- HTTP协议学习篇(二)—http响应
- Java虚拟机的体系结构
- 开发板的烧录
- 图像处理学习笔记之图像的几何变换(5)图像缩放和插值算法
- 分页机制
- 生命之树(树形dp)