为什么我创建的索引总为空?郁闷

来源:互联网 发布:apache ant jdk1.7 编辑:程序博客网 时间:2024/04/29 16:31

/*
 *michzel new java files
 *
 *Created on 2010-9-15
 *
 *Copyright 2010 Anchora info company. all rights reserved
 */

package LuceneTest;


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

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.*;
import org.apache.lucene.demo.IndexFiles;
import org.apache.lucene.document.*;
import org.apache.lucene.index.*;
import org.apache.lucene.queryParser.*;
import org.apache.lucene.store.*;
import org.apache.lucene.util.Version;
import org.apache.lucene.store.LockFactory;

@SuppressWarnings("unused")
public class IndexPerf {
 public static final String INDEX_STORE_PATH= "d://test";
 public static final int DEFAULT_MAX_DOC_IN_RAM=64;
 public static final int DEFAULT_MAX_FIELD_LENGTH = Integer.MAX_VALUE;
 
 private static IndexPerf indexer= null;
 private IndexWriter ramWriter= null;
 private IndexWriter fsWriter= null;
 
 private int docs_in_ram;
 private int ramMaxFile= DEFAULT_MAX_DOC_IN_RAM;
 private boolean refreshed= true;
 
 FSDirectory fsdir= null;
 RAMDirectory ramdir= null;
 
 //静态方法构造一个单例
 public static IndexPerf getInstance() throws IOException{
  if(indexer==null){
   indexer= new IndexPerf();
  }
  return indexer;
  
 }
 //构造索引器
 public void newWriter(){
  if(fsWriter != null || ramWriter != null){
   System.out.println("close the indexer first then to creat a new one");
   return;
  }
  try{
    File file = new File(INDEX_STORE_PATH);
   
   fsdir= FSDirectory.open(file);
   
   ramdir= new RAMDirectory();
   
   StandardAnalyzer anlyzer= new StandardAnalyzer(Version.LUCENE_30);
   
   ramWriter= new IndexWriter(ramdir, anlyzer,
     true,IndexWriter.MaxFieldLength.UNLIMITED);
   ramWriter.setInfoStream(System.out);
   
   fsWriter= new IndexWriter(fsdir,anlyzer,
     true,IndexWriter.MaxFieldLength.UNLIMITED);
   
   initWriter();
  }catch (IOException e){
   e.printStackTrace();
   
  }
 }
 //初始化索引器
 private void initWriter() throws IOException{
  fsWriter.setMaxFieldLength(DEFAULT_MAX_FIELD_LENGTH);
  
 }
 //优化索引
 public void optimizeIndex() throws IOException{
  if(fsWriter == null || ramWriter == null){
   System.out.println("use newWriter() method to initialize a new Indexer first.");
   return ;
   }
 //refreshRam();
 //fsWriter.optimize();
 }
 //索引某个目录下的文件
 public void toIndex(String path) throws IOException{
  toIndex(new File(path));
 }
 //索引某个对象
 public void toIndex(File file) throws IOException{
  
  refreshed = false;
  Date start = new Date();
  int number= indexFiles(file);
  Date end= new Date();
  System.out.println("总共耗时"+(end.getTime()-start.getTime())+"毫秒");
  System.out.println("一共为"+number+"个文件建立索引");
 }
 //关闭索引器
 public void close() throws IOException{
  
  //refreshRam();
  ramWriter.close(true);
  fsWriter.addIndexesNoOptimize(new Directory[]{ramdir});
  fsWriter.close(true);
  
 }
 //刷新内存
 private void refreshRam() throws IOException{
  if(!refreshed){
   System.out.println("Refreshing");
   
   fsWriter.addIndexesNoOptimize(new Directory[]{ramdir});
   ramdir.close();
   ramdir = new RAMDirectory();
   ramWriter= new IndexWriter(fsdir,new
     StandardAnalyzer(Version.LUCENE_30), true,IndexWriter.MaxFieldLength.UNLIMITED);
   docs_in_ram=0;
   refreshed= true;
  }
 
  
 } 
 //向索引中加入文档
 private void addDocument(File file) throws IOException{
  
  ramWriter.addDocument(MyDocument.getDocument(file));
  
  docs_in_ram++;
  System.out.println("向索引中加入文档");
  //refreshed= false;
  
 }
 //递归遍历文件目录来建立索引
 private int indexFiles(File file) throws IOException{
  if(file.isDirectory()){
   File[] files= file.listFiles();
   int num= 0;
   for(int i= 0; i<files.length;i++){
    num+= indexFiles(files[i]);
   System.out.println(+i); 
   }
   return num;
  }else{
   if(file.getPath().endsWith(".txt")){
    System.out.println("正在建索:"+file.getCanonicalPath());
    addDocument(file);
    return 1;
   }
   else{
    System.out.println("文件类型不支持"+file);
    return 0;
   }
  }
  
  
 }
 public static void main(String[] args)throws IOException{
  IndexPerf indexer= IndexPerf.getInstance();
  indexer.newWriter();
  indexer.toIndex("d://data");
  indexer.close();
  
 }
}

 

原创粉丝点击