Chinaunix首页 | 论坛 | 博客
  • 博客访问: 442241
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 13:22:48

    就像每个程序都有一个Hello World来让人体验它一样,lucene也可以很简单的提供一个实例。如下(来自lucene in action的例子)有两个类组成: 一个是建立索引 
    import java.io.File;

    import java.io.FileReader;

    import java.io.IOException;

    import java.util.Date;

    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;

 

    public class Indexer {

      public static void main(String[] args) throws Exception {

        if (args.length != 2) {

          throw new Exception("Usage: java " + Indexer.class.getName() + " ");

        }

        File indexDir = new File(args[0]);

        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");

      }

 

      // open an index and start file directory traversal

      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);

        writer.setUseCompoundFile(false);

        indexDirectory(writer, dataDir);

        int numIndexed = writer.docCount();

        writer.optimize();

        writer.close();

        return numIndexed;

      }

 

      // recursive method that calls itself when it finds a directory

 

[1]    

【责编:landy】

--------------------next---------------------

阅读(151) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~