Mysoo2 站内搜索引擎安装指南
百度阿拉丁平台剑指垂直搜索
谷歌测试在搜索建议中放广告
谷歌与百度的八年“暗战”
Google公布2008年度搜索排行榜
传戴尔PC将放弃谷歌选择微软Live搜索引擎
雅虎高级搜索专家陆奇将任微软数字部门高管
微软不娶雅虎 只想买搜索业务
微软退出搜索市场能对Google造更大伤害
雅虎打造BOSS垂直搜索引擎
谷歌百度口水战升级 比拼"谁更懂中文"
从百度“竞价门”看企业的社会责任感
山寨版搜索引擎“百谷虎”雷倒网友
雅虎搜索技术高管跳槽至微软
面对“搜索门” 百度公司力挽声誉
内容更新频率对SEO的影响


 ·Lucene In Action c...
 ·Mysoo站内搜索引擎安装指南
 ·Mysoo站内搜索引擎1.0预览版正...
 ·Lucene搜索引擎分词原理
 ·Lucene全文检索应用示例及代码简析





Lucene In Action ch 1 笔记 -- 基本概念


 共2页  1  2 

在第一章中作者 主要讲了Lucene 是什么 能用来干什么, 以及一个 indexing 和 searching 的例子, 通过例子讲解了一点基本(核心)概念.给读者一个基本的Lucene 概况. 然后又介绍了现在流行的 搜索框架.

我们主要来看看 这个 indexing and searching 例子 然后了解一些基本概念.

package lia.meetlucene;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;

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

/**
 * This code was originally written for
 * Erik's Lucene intro java.net article
 */
public class Indexer {

  public static void main(String[] args) throws Exception {
    if (args.length != 2) {
      throw new Exception("Usage: java " + Indexer.class.getName()
        " <index dir> <data dir>");
    }
    File indexDir = new File(args[0]); // 在该目录中创建Lucene Incex
    File dataDir = new File(args[1]); // 该目录中存放备索引的文件

    long start = new Date().getTime();
    int numIndexed = index(indexDir, dataDir);
    long end = new Date().getTime();

    System.out.println("Indexing " + numIndexed + " files took "
      + (end - start) + " milliseconds");
  }

  public static int index(File indexDir, File dataDir)
    throws IOException {

    if (!dataDir.exists() || !dataDir.isDirectory()) {
      throw new IOException(dataDir
        " does not exist or is not a directory");
    }

    IndexWriter writer = new IndexWriter(indexDir,
      new StandardAnalyzer(), true);               //(1)创建 Lucene Index
    writer.setUseCompoundFile(false);

    indexDirectory(writer, dataDir);

    int numIndexed = writer.docCount();
    writer.optimize();
    writer.close();                               // close index
    return numIndexed;
  }

  private static void indexDirectory(IndexWriter writer, File dir)
    throws IOException {

    File[] files = dir.listFiles();

    for (int i = 0; i < files.length; i++) {
      File f = files[i];
      if (f.isDirectory()) {
        indexDirectory(writer, f);  //(2) recurse
      else if (f.getName().endsWith(".txt")) {
        indexFile(writer, f);
      }
    }
  }

  private static void indexFile(IndexWriter writer, File f)
    throws IOException {

    if (f.isHidden() || !f.exists() || !f.canRead()) {
      return;
    }

    System.out.println("Indexing " + f.getCanonicalPath());

    Document doc = new Document();
    doc.add(Field.Text("contents"new FileReader(f)));  // (3) index file content
    doc.add(Field.Keyword("filename", f.getCanonicalPath())); // (4) index file name
    writer.addDocument(doc);                   //(5) add document in Lucene index
  }
}

上面的Indexer 使用了几行 Lucene的API, 来indexing 一个目录下面的文件. 运行时候 需要两个参数 , 一个保存index的目录和要索引的文件目录.

在上面的类中,需要下面的一些Lucene classes 来执行 indexing 处理:

IndexWriter

Directory

Analyzer

Document

Field

IndexWriter 是indexing 处理时用到的中心组件,该类create 新index 并且添加documents 到已经存在的index, BTW,在Lucene中还有别的方法来更新index.

Directory: 用来存放index文件的文件目录,该类是个抽象类,用几个子类可以使用,上面使用了File来代表文件路径,在Lucene中用两个主要的Directory子类,一个FSDirectory,一个 RAMDirectory,前者是把index保存到硬盘中的;后者是保存在内存中的,在内存中处理数度当然就相应的快一些 了但只适合于小文件.

Analyzer: 在文件备索引以前要先通过Analyzer分析,去掉一些对search无用的词语(如英语中 的小词 in at a 等等,在Lucene中被称为stop words 的词),还可以处理大小写的问题(是大小写相关啊 还是不相关),使用Lucene时候 选择Analyzer是关键.

Document: 代表一些Fields的集合.可以想象为一些数据的集合.

Field: 在index中的每一个Document中都包含一些 命名的Fields 用Field来构造, 每一个field都是的搜索是符合要求和不符合要求的index中的一些数据,Lucene提供了四种不同的Field,

1,Keyword  不分析 只索引和保存,象一些特殊信息 不可以分割的 如 电话号码 网站 Email 等.

2,UnIndexed 既不索引也不分析,只是把值保存在index中.该类型适合用来显示搜索结果的field,但是你从来不搜索该显示的数据,如URL

3,UnStored UnIndexed的对立面, 分析和索引但是不保存在index中,适合大型数据 只搜索但是不显示原始数据.

4,Test 分析且索引,如果索引数据是String则也保存在index中, 如果是Reader则不保存.

 共2页  1  2 

来源: Java爱好者 | 作者: | 发表时间: 2006-11-4 0:06:00

 

 


Copyright © 2000-2008 Mysoo.com.cn, All Rights Reserved Mysoo.com.cn 版权所有                                  把Mysoo添加到 Google   沪ICP备06051139