`
pavel
  • 浏览: 923997 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Lucene-2.2.0 源代码阅读学习(1)

阅读更多

package org.apache.lucene.demo;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;

//为指定目录下的所有文件建立索引
public class IndexFiles {
private IndexFiles() {}
static final File INDEX_DIR = new File("index");  
//存放建立索引的目录
public static void main(String[] args) {
    String usage = "java org.apache.lucene.demo.IndexFiles <root_directory>";

// 如果在DOS下直接输入命令java org.apache.lucene.demo.IndexFiles,而没有指定目录名。
    if (args.length == 0) {  
//args没有接收到任何输入
      System.err.println("Usage: " + usage);
      System.exit(1);
    }

// 如果在DOS下输入命令java org.apache.lucene.demo.IndexFiles myDir,而myDir=index目录已经存在。
    if (INDEX_DIR.exists()) {
      System.out.println("Cannot save index to '" +INDEX_DIR+ "' directory, please delete it first");
      System.exit(1);
    }
   
// 如果在DOS下输入命令java org.apache.lucene.demo.IndexFiles myDir,而myDir目录不存在,则无法创建索引,退出。
    final File docDir = new File(args[0]);
// 通过输入的第一个参数构造一个File
    if (!docDir.exists() || !docDir.canRead()) {
      System.out.println("Document directory '" +docDir.getAbsolutePath()+ "' does not exist or is not readable, please check the path");
      System.exit(1);
    }
   
// 如果不存在以上问题,按如下流程执行:
    Date start = new Date();
    try {
      // 通过目录INDEX_DIR构造一个IndexWriter对象
      IndexWriter writer = new IndexWriter(INDEX_DIR, new StandardAnalyzer(), true);
      System.out.println("Indexing to directory '" +INDEX_DIR+ "'...");
      indexDocs(writer, docDir);
      System.out.println("Optimizing...");
      writer.optimize();
      writer.close();
      // 计算创建索引文件所需要的时间
      Date end = new Date();
      System.out.println(end.getTime() - start.getTime() + " total milliseconds");

    } catch (IOException e) {
      System.out.println(" caught a " + e.getClass() +
       "\n with message: " + e.getMessage());
    }
}

static void indexDocs(IndexWriter writer, File file)
    throws IOException {
    // file可以读取
    if (file.canRead()) {
      if (file.isDirectory()) {
// 如果file是一个目录(该目录下面可能有文件、目录文件、空文件三种情况)
        String[] files = file.list(); // 获取file目录下的所有文件(包括目录文件)File对象,放到数组files里
        //如果files!=null
        if (files != null) {
          for (int i = 0; i < files.length; i++) { // 对files数组里面的File对象递归索引,通过广度遍历
            indexDocs(writer, new File(file, files[i]));
          }
        }
      } else {
// 到达叶节点时,说明是一个File,而不是目录,则建立索引
        System.out.println("adding " + file);
        try {
          writer.addDocument(FileDocument.Document(file)); // 通过writer,使用file对象构造一个Document对象,添加到writer中,以便能够通过建立的索引查找到该文件

        }
        catch (FileNotFoundException fnfe) {
          ;
        }
      }
    }
}

}

上面是一个简单的Demo,主要使用了org.apache.lucene.index包里面的IndexWriter类。IndexWriter有很多构造方法,这个Demo使用了它的如下的构造方法,使用String类型的目录名作为参数之一构造一个索引器:

public IndexWriter(String path, Analyzer a, boolean create)
       throws CorruptIndexException, LockObtainFailedException, IOException {
    init(FSDirectory.getDirectory(path), a, create, true, null, true);
}

这里,FSDirectory是文件系统目录,该类的方法都是static的,可以直接方便地获取与文件系统目录相关的一些参数,以及对文件系统目录的操作。FSDirectory类继承自抽象类Directory。

如果想要建立索引,需要从IndexWriter的构造方法开始入手:

可以使用一个File对象构造一个索引器:

public IndexWriter(File path, Analyzer a, boolean create)
       throws CorruptIndexException, LockObtainFailedException, IOException {
    init(FSDirectory.getDirectory(path), a, create, true, null, true);
}

可以使用一个Directory对象构造:

public IndexWriter(Directory d, Analyzer a, boolean create)
       throws CorruptIndexException, LockObtainFailedException, IOException {
    init(d, a, create, false, null, true);
}

使用具有两个参数的构造函数老构造索引器,指定一个与文件系统目录有关的参数,和一个分词工具,IndexWriter类提供了3个:

public IndexWriter(String path, Analyzer a)
    throws CorruptIndexException, LockObtainFailedException, IOException {
    init(FSDirectory.getDirectory(path), a, true, null, true);
}

public IndexWriter(File path, Analyzer a)
    throws CorruptIndexException, LockObtainFailedException, IOException {
    init(FSDirectory.getDirectory(path), a, true, null, true);
}

public IndexWriter(Directory d, Analyzer a)
    throws CorruptIndexException, LockObtainFailedException, IOException {
    init(d, a, false, null, true);
}

另外,还有5个构造方法,可以参考源文件IndexWriter类。

Analyzer是一个抽象类,能够对数据源进行分析,过滤,主要功能是进行分词:

package org.apache.lucene.analysis;

java.io.Reader;
public abstract class Analyzer {
public abstract TokenStream tokenStream(String fieldName, Reader reader);

public int getPositionIncrementGap(String fieldName)
{
    return 0;
}
}

通过使用StandardAnalyzer类(继承自Analyzer抽象类),构造一个索引器IndexWriter。StandardAnalyzer类,对进行检索的word进行了过滤,因为在检索的过程中,有很多对检索需求没有用处的单词。比如一些英文介词:at、with等等,StandardAnalyzer类对其进行了过滤。看下StandardAnalyzer类的源代码:

package org.apache.lucene.analysis.standard;

import org.apache.lucene.analysis.*;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.util.Set;


public class StandardAnalyzer extends Analyzer {
private Set stopSet;

// StopAnalyzer类对检索的关键字进行过滤,这些关键字如果以STOP_WORDS数组中指定的word结尾
public static final String[] STOP_WORDS = StopAnalyzer.ENGLISH_STOP_WORDS;

// 构造一个StandardAnalyzer分析器,下面的几个构造函数都是以不同的方式构造一个限制检索关键字结尾字符串的StandardAnalyzer分析器,可以使用默认的,也可以根据自己的需要设置
public StandardAnalyzer() {
    this(STOP_WORDS);
}
public StandardAnalyzer(Set stopWords) {
    stopSet = stopWords;
}
public StandardAnalyzer(String[] stopWords) {
    stopSet = StopFilter.makeStopSet(stopWords);
}
public StandardAnalyzer(File stopwords) throws IOException {
    stopSet = WordlistLoader.getWordSet(stopwords);
}
public StandardAnalyzer(Reader stopwords) throws IOException {
    stopSet = WordlistLoader.getWordSet(stopwords);
}

看看StopAnalyzer类,它的构造方法和StandardAnalyzer类的很相似,其中默认的ENGLISH_STOP_WORDS指定了下面这些:

public static final String[] ENGLISH_STOP_WORDS = {
    "a", "an", "and", "are", "as", "at", "be", "but", "by",
    "for", "if", "in", "into", "is", "it",
    "no", "not", "of", "on", "or", "such",
    "that", "the", "their", "then", "there", "these",
    "they", "this", "to", "was", "will", "with"
};

也可以使用带参数的构造函数,根据需要自己指定。

分享到:
评论
2 楼 ddc496601562 2010-05-18  
呵呵  如果有可能的话,我加一下你的QQ吧    
我现在在学搜索这一块,但有很多不懂的问题,方便的话请教你一下
49660162 我的QQ
1 楼 ddc496601562 2010-05-18  
可不可以问个问题?? 
FileDocument.Document(file)中的FileDocument哪儿来的??
我用的是2.2的lucene,里面没有这个类 

相关推荐

    lucene-analyzers-2.2.0.jar

    lucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-2.2.0.jarlucene-analyzers-...

    lucene-2.2.0zip

    标题中的"lucene-2.2.0zip"指的是Lucene的2.2.0版本,这是一个较早的版本,对于学习和理解Lucene的基础概念非常有帮助。 Lucene 2.2.0的主要特性包括: 1. **全文检索**:Lucene支持对文档内容进行全文检索,允许...

    Lucene-2.3.1 源代码阅读学习

    《Lucene-2.3.1 源代码阅读学习》 Lucene是Apache软件基金会的一个开放源码项目,它是一个高性能、全文本搜索库,为开发者提供了在Java应用程序中实现全文检索功能的基础架构。本篇文章将深入探讨Lucene 2.3.1版本...

    lucene-highlighter-2.2.0.jar

    lucene-highlighter-2.2.0.jarlucene-highlighter-2.2.0.jarlucene-highlighter-2.2.0.jarlucene-highlighter-2.2.0.jarlucene-highlighter-2.2.0.jarlucene-highlighter-2.2.0.jarlucene-highlighter-2.2.0.jar

    lucene-highlighter-2.2.0-src.zip

    《深入解析Lucene高亮显示源码:剖析`lucene-highlighter-2.2.0-src.zip`》 Lucene,作为一个开源全文检索库,以其高效、灵活的特点在信息检索领域广泛应用。在处理搜索结果时,为了提升用户体验,通常会采用高亮...

    lucene-2.2.0-src

    《深入剖析Lucene 2.2.0源代码》 Lucene是一款强大的开源全文搜索引擎库,由Apache软件基金会开发并维护。它为Java开发者提供了一种高性能、可扩展的文本检索核心工具。本文将深入探讨Lucene 2.2.0版本的源代码,...

    基于JAVA的搜索引擎 lucene-2.2.0

    在前面Lucene-2.2.0 源代码阅读学习(1)中,根据Lucene提供的一个Demo,详细分析研究一下索引器org.apache.lucene.index.IndexWriter类,看看它是如果定义的,掌握它建立索引的机制。 通过IndexWriter类的实现源代码...

    lucene-analyzers-smartcn-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:lucene-analyzers-smartcn-7.7.0-javadoc-API文档-中文(简体)版....

    lucene-core-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene-core-7.7.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-5.3.1源代码

    - 通过阅读源代码,可以理解Lucene的内部工作原理,如如何构建索引、执行查询等。 - 分析器部分的源码有助于了解文本预处理过程,包括分词、去除停用词等。 - 探究查询解析器的实现,掌握如何将自然语言转化为...

    lucene-analyzers-common-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-common-6.6.0.pom; 包含翻译后的API文档:lucene-analyzers-common-6.6.0-javadoc-API文档-中文(简体)版.zip;...

    lucene-suggest-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API文档:lucene-suggest-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache....

    lucene-core-2.1.0.jar

    这是一个java开发用的.jar文件,用它和Lucene-core-2.0.0.jar可以实现搜索引擎

    lucene-core-7.2.1-API文档-中文版.zip

    赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene-core-7.2.1-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-backward-codecs-7.3.1-API文档-中英对照版.zip

    赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-codecs-7.3.1.pom; 包含翻译后的API文档:lucene-backward-codecs-7.3.1-javadoc-API文档-中文(简体)-英语-对照...

    lucene-core-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene-core-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:lucene...

    lucene-spatial-extras-7.3.1-API文档-中英对照版.zip

    赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras-7.3.1.pom; 包含翻译后的API文档:lucene-spatial-extras-7.3.1-javadoc-API文档-中文(简体)-英语-对照版....

    lucene-memory-6.6.0-API文档-中文版.zip

    赠送源代码:lucene-memory-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-memory-6.6.0.pom; 包含翻译后的API文档:lucene-memory-6.6.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.lucene:...

    lucene-suggest-7.7.0-API文档-中文版.zip

    赠送源代码:lucene-suggest-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-7.7.0.pom; 包含翻译后的API文档:lucene-suggest-7.7.0-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache....

    lucene-analyzers-smartcn-7.7.0-API文档-中英对照版.zip

    赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-analyzers-smartcn-7.7.0.pom; 包含翻译后的API文档:lucene-analyzers-smartcn-7.7.0-javadoc-API文档-中文(简体)-英语...

Global site tag (gtag.js) - Google Analytics