Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1671675
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: LINUX

2008-10-18 11:07:58

package test;

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 {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  //创建索引文件和要索引文件所在的目录
  File indexDir = new File(Constants.INDEX_STORE_PATH);//调用Constants中的常量
  File dataDir = new File(Constants.INDEX_FILE_PATH);
  //获取建立索引开始的时间
  long start =new Date().getTime();
  int numIndexed = 0;
  try {
   numIndexed = index(indexDir,dataDir);返回要索引文件的数量
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  long end = new Date().getTime();
  System.out.print("Indexing"+ numIndexed + "files took"
    + (end - start) + "milliseconds");//计算整个索引过程所需要的时间
  

 }

 private static int index(File indexDir, File dataDir) throws IOException{
  // TODO Auto-generated method stub
  if(!dataDir.exists() || !dataDir.isDirectory())
  {
   throw new IOException(dataDir + "does not exit 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();//关闭writer对象
  return numIndexed;
 }

//对文件夹和文件查找遍历。

 private static void indexDirectory(IndexWriter writer, File dataDir)
  throws IOException{
  // TODO Auto-generated method stub
  File[] files = dataDir.listFiles();
  for(int i = 0;i
  {
   File f = files[i];
   if(f.isDirectory())
   {
    indexDirectory(writer,f);
   }
   else if(f.getName().endsWith(".txt"))
   {
    indexFile(writer,f);
   }
  }
  
 }

 private static void indexFile(IndexWriter writer, File f) throws IOException{
  // TODO Auto-generated method stub
  if(f.isHidden() || !f.exists() || !f.canRead())
  {
   return;
  }
  System.out.println("Indexing" + f.getCanonicalPath());

//最主要的部分
  Document doc = new Document();
  Field fieldContents = new Field("contents", new FileReader(f));//得到文本的内容域
  doc.add(fieldContents);
  Field fieldName = new Field("name",f.getCanonicalPath(),Field.Store.YES,Field.Index.UN_TOKENIZED);//得到文本的

路径名称,不同的内容会有不同的操作方法(四种)
  doc.add(fieldName);
  writer.addDocument(doc);

 }

}

 

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