zoukankan      html  css  js  c++  java
  • lucene索引的建立昨天的问题已解决

    //昨天的错误找到了  搜索与创建的目录不应在同一个根目录下。
    //全靠队长啊
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.index.CorruptIndexException;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.store.IndexInput;
    import org.apache.lucene.store.IndexOutput;
    import org.apache.lucene.store.LockObtainFailedException;
    import org.apache.lucene.store.RAMDirectory;
    import org.apache.lucene.util.Version;
    
    
    public class HelloLucene {
    
     /**
      * @param args
      */
     
     
     public static void index()throws IOException
     {
    //  Directory directory=new RAMDirectory() ;
      Directory directory=FSDirectory.open(new File("g:/lucene/first1") );
      IndexWriterConfig iwc=new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));
      IndexWriter writer=null;
    
      writer=new  IndexWriter(directory,iwc);
      Document doc=null;
      File f=new File("g:/lucene/testFiles");
       for (File file:f.listFiles())
       {
        doc=new Document();
        doc.add(new Field("content",new FileReader(file)));
        doc.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED));
        doc.add(new Field("filepath",file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
        writer.addDocument(doc);
        System.out.println(file.getName()+" has Indexed!");
       }
    
    
      writer.close();
      
     }
      
     
     public static void main(String[] args) {
      // TODO Auto-generated method stub
    
      try {
       index();
      } catch (IOException e) {
       // TODO Auto-generated catch block
       e.printStackTrace();
      }
      
     }
    
    }
    
    
    
    
    


     

  • 相关阅读:
    FPGA学习之基本结构
    凸优化和机器学习
    第6篇如何访问pod
    吉日嘎拉DotNet.BusinessV4.2中的一处bug,及我的修复和扩展
    吉日嘎拉C#快速开发平台V4.0到V4.2升级记
    布隆过滤器简介及实现-----源自数学之美
    poj [1753]
    Zookeeper Hello World
    获取用户真实IP,php实现
    mysql中engine=innodb和engine=myisam的区别
  • 原文地址:https://www.cnblogs.com/lixingle/p/3313048.html
Copyright © 2011-2022 走看看