`

【Lucene3.0 初窥】索引创建(1):IndexWriter索引器

阅读更多

《Lucene索引创建》系列文章将从源代码出发,详细揭示Lucene两大功能之一的索引创建过程。并阐述其中所用到的信息检索的相关技术。由于Lucene是基于多线程的,为了能够简洁明了的说明Lucene索引创建的过程,我们尽量会绕开Lucene多线程机制的处理细节。

 

1.1 准备工作

 我们对content目录中1.txt、2.txt、3.txt这三个英文文档的文件名,路径,内容建立索引。代码如下:

//索引文件存放的位置
File indexDir=new File(".\index");
//需要建立索引的文档集合的位置
 File docDir = new File(".\content"); 
//创建索引器(核心)
IndexWriter standardWriter = new IndexWriter(FSDirectory.open(indexDir),new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.LIMITED);         
//不建立复合式索引文件,默认的情况下是复合式的索引文件
standardWriter.setUseCompoundFile(false);
//为原文档集合中的每个文档的相关信息建立索引
for (File fileSrc : docDir.listFiles()) {   
        //Lucene的文档结构
        Document doc = new Document();  	           	   
        //文件名称,可查询,不分词
        String fileName=file.getName().substring(0,file.getName().indexOf("."));
        doc.add(new Field("name",fileName, Field.Store.YES, Field.Index.NOT_ANALYZED));  
         //文件路径,可查询,不分词
        String filePath=file.getPath();
        doc.add(new Field("path", filePath, Field.Store.YES, Field.Index.NOT_ANALYZED));
        //文件内容,需要检索
        doc.add(new Field("content", new FileReader(file)));  	       	
        //使用索引器对Document文档建索引
       standardWriter.addDocument(doc);  
}  
//关闭索引器,并写入磁盘索引文件
standardWriter.optimize();  
standardWriter.close();  

其中,IndexWriter索引器用来创建索引。Document和Field类(详见《Document/Field 数据源组织结构》 )表示这三个文档的结构如下:

            Document      Field1(name)        Field2(path)                           Field3(content)

                doc1                 1               e:\\content\\1.txt        The lucene is a good IR. I hope I can lean well.

                doc2                 2               e:\\content\\2.txt        You know it's difficult to learn Lucene well.

                doc3                 3               e:\\content\\3.txt        Maybe is very hard. I must do it well.

 

 

1.2 IndexWriter 索引器

 

一个IndexWriter对象创建并且维护(maintains) 一条索引并生成segment,使用DocumentsWriter类来建立多个文档的索引数据,SegmentMerger类负责合并多个 segment。

 

1.2.1 构造器

org.apache.lucene.index包是lucene用来创建索引的。其中的IndexWriter类是Lucene的索引器 下面是IndexWriter的构造器。

/**
 * d 索引文件存储的目录类
 * a 语言分析器,主要用于分词
 * create 真—创建新的索引文件或者重新覆盖已存在的索引文件; 假—在已存在的索引文件后追加索引记录
 * mfl 允许Field中的词的最大数量
 */
public IndexWriter(Directory d, Analyzer a, boolean create, MaxFieldLength mfl){
    init(d, a, null, mfl.getLimit(), null, null);
}

其中Directory将在后面再谈;Analyzer详细可见《Lucene分析器—Analyzer》 ;create确定是否一条新的索引将被创建,或者是否一条已经存在的索引将被打开;MaxFieldLength默认的最大长度为10000,在《Document-Field数据源 组织结构 中我们知道Field可以表示数据源的任何属性信息,如果Field中的value数据量非常大,那么很有可能在建索引的时候造成内存溢出。所以有必要限制每个Field中value的最大词数。

 

1.2.3 建立索引的方法:addDocument(Document)

该方法是用来创建索引的。我们进一步看看这个方法的源码:

public void addDocument(Document doc)  {
       addDocument(doc, analyzer);
}

public void addDocument(Document doc, Analyzer analyzer) {
    ensureOpen();
    boolean doFlush = false;
    boolean success = false;
    try {
      try {
            //使用DocumentWriter建立索引
            doFlush = docWriter.addDocument(doc, analyzer);
            success = true;
      } finally {
           if (!success) {

               if (infoStream != null)
               message("hit exception adding document");

               synchronized (this) {

                   if (docWriter != null) {
                        final Collection<String> files = docWriter.abortedFiles();
                        if (files != null)
                        deleter.deleteNewFiles(files);
                   }
               }
           }
      }
      if (doFlush)
             flush(true, false, false);
      }catch (OutOfMemoryError oom) {
             handleOOM(oom, "addDocument");
    }
}

其中docWriter.addDocument(doc,analyzer);调用了DocumentWriter类来创建一个Document对象的索引。而DocumentWriter类将在《索引创建(2):DocumentWriter处理流程 》详细介绍。

 

7
0
分享到:
评论

相关推荐

    Lucene创建索引步骤

    Lucene创建索引步骤: 1、创建Directory(索引位置) 2、创建IndexWrite(写入索引) 3、创建Document对象 4、为Document添加Field(相当于添加属性:类似于表与字段的关系) 5、通过IndexWriter添加文档到索引中

    lucene2.9.1所有最新开发包及源码及文档

    1) IndexWriter:索引写出器 a) 构造方法: IndexWriter(Directory d, Analyzer a, IndexWriter.MaxFieldLength mfl) 如果索引不存在,就会被创建。如果索引存在,就追加. IndexWriter(Directory d, Analyzer ...

    Lucene 详细教案

    这样你就告诉 lucene 我要在 c 盘的 index 目录下建立索引文件,我要使用车东老师的二分词算法做分析器、我要在这个目录下删除以前的索引或任何文件创立我的索引文件。 索引的建立有三种方式,让我一一道来: 1 ...

    基于lucene的搜索引擎总结

    IndexSearcher:用于搜索IndexWriter创建的索引 Term:用于搜索的一个基本单元包括了一对字符串元素,与Field相对应 Query :抽象的查询类 TermQuery:最基本的查询类型,用来匹配特定Field中包含特定值的文档 Hits...

    基于JAVA的搜索引擎 lucene-2.2.0

    通过IndexWriter索引器的构造函数,以及它初始化时调用的一个init方法,可以了解一个IndexWriter索引器的构造最重要的是在init方法中的初始化工作。它主要实现了根据指定的建立索引的方式(重写、追加写入),通过...

    learn-lucene:lucene学习

    lucene_learnlucene学习day_01:索引创建的步骤:创建directory创建IndexWriter创建Document为Document添加Field通过IdexUriter添加文档到索引中搜索的步骤:创建directory创建IndexReader根据IndexReader创建...

    lucene,lucene教程,lucene讲解

    Directory类代表一个Lucene索引的位置。它是一个抽象类. 其中的两个实现: 第一个是 FSDirectory,它表示一个存储在文件系统中的索引的位置。 第二个是 RAMDirectory,它表示一个存储在内存当中的索引的位置。 ...

    lucene2.9.1完整DEMO及开发文档

    //创建一个IndexWriter(存放索引文件的目录,分析器,Field的最大长度) iwriter = new IndexWriter(directory, analyzer,true, IndexWriter.MaxFieldLength.UNLIMITED); //iwriter.setUseCompoundFile(true);//...

    Apache Lucene全文检索和IKAnalyzer分词工具类

    IndexWriter indexWriter = new IndexWriter(indexDir, luceneAnalyzer,true); for (int i = 0; i (); i++) { LuceneVO vo = (LuceneVO)list.get(i); Document doc = new Document(); Field FieldId = ...

    lucene.net搜索技术,附带学习资料

    性能优化也很重要,因为如果要索引的文件比较大的话,建立索引的性能就会很大的下降,你可以调整IndexWriter的几个参数来优化索引性能,还有可以用IndexWriter.Optimize()方法(这个方法主要是优化查询速度,反而使...

    lucene的第一个程序

    创建一个indexwriter // 1)指定索引库的存放位置Directory对象 // 2)指定一个分析器,对文档内容进行分析 Directory directory = FSDirectory.open(new File&#40;"D:\\temp\\index"&#41;); Analyzer analyzer = ...

    Lucene3.1使用教程.doc

    Lucene3.1使用教程 随着Lucene开发的推进,Lucene3.1推出了,但是目前Lucene3.1的使用文档较少,特收集了《Lucene3.1使用教程》 值得关注的内容有: 1. 性能提升 2. ReusableAnalyzerBase使得跟容易让 ...

    apache lucene 4.10.0入门单元测试代码demo

    总结了一些实用的demo 包括: 1.建立索引 2.通过IKAnalyzer搜索中文关键词 3.复杂的多字段搜索 4.多线程并发搜索,通过contiperf测试,详见:...lucene支持多线程并发搜索和建索引,只要IndexWriter是单例模式即可

    【分享:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    1&gt; lucene学习笔记 2&gt; 全文检索的实现机制 【1】lucene学习笔记的目录如下 1. 概述 3 2. lucene 的包结构 3 3. 索引文件格式 3 4. lucene中主要的类 4 4.1. Document文档类 4 4.1.1. 常用方法 4 4.1.2. 示例 4 4.2...

    Lucene 源码解析

    在创建完最重要的IndexWriter之后,就开始遍历需要索引的文件,构造对应的Document和Filed类,最终通过IndexWriter的addDocument函数开始索引。 Document的构造函数为空,StringField、TextField和Field的构造函数...

    Lucene5 工具类

    工具类对IndexWriter,IndexReader,IndexSearcher,Analyzer,QueryParser等Lucene这些常用操作对象的获取进行了封装,其中IndexWriter采用了单例模式,确保始终只有一个对象实例,因为Lucene限制了索引写操作是阻塞的...

    Homework-4 作业补充1

    2.1 在创建索引过程中,首先提供的是一个目录,用于存储索引 2.2 然后建立索引写文件入口 IndexWriter 2.3 创建索引过程中会使用 Field,

    SearchEngineDemo:学习搜索引擎的一些demo

    luceneIndexDetail Lucene索引详解(IndexWriter详解、Document详解、索引更新)博客学习地址: luceneSearchDetail Lucene搜索详解(Lucene搜索流程详解、搜索核心API详解、基本查询详解、QueryParser详解)博客...

    Lucene 全文检索

    public static String indexDirpath = "e:\\work\\zhzyk\\index";...第一次执行请把 indexWriter = new IndexWriter(indexDir, luceneAnalyzer, false);//true建立索引库,false追加索引库 false改为true

    中文分词工具word-1.0,Java实现的中文分词组件多种基于词典的分词算法

    3、利用word分析器建立Lucene索引 Directory directory = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_47, analyzer); IndexWriter indexWriter = new IndexWriter...

Global site tag (gtag.js) - Google Analytics