Luence建索中的一个异常

异常:org.apache.lucene.index.CorruptIndexException: failed to locate current segments_N file
 at org.apache.lucene.index.IndexFileDeleter.<init>(IndexFileDeleter.java:223)
 at org.apache.lucene.index.IndexWriter.<init>(IndexWriter.java:670)
 at org.apache.lucene.demo.IKAnalyzerDemo.createIndex(IKAnalyzerDemo.java:45)
 at org.apache.lucene.demo.IKAnalyzerDemo.main(IKAnalyzerDemo.java:185)

  未能找到当前segments_N文件

 

 

建索类:

String fieldName = "title";
    String fieldValue="content";
    Analyzer analyzer = new IKAnalyzer();
    /**
     * 创建索引
     * @param pathFile  索引存放位置
     */
 @SuppressWarnings("deprecation")
 public void createIndex(String pathFile){
    String text1 = "上海(Shanghai),简称“沪”或“申”,中国第一大城市,中华人民共和国直";
         String text2 = "天津,简称津,中华人民共和国直辖市、中国国家中心城市、中国北方经济中心、.";
         String text3 = "北京是中华人民共和国的首都、直辖市和国家中心城市之一,";
         try {
          //文件生成在classpath下
             //RAMDirectory directory = new RAMDirectory();
             Directory directory = FSDirectory.open(new File(pathFile));
             //索引存放文件夹  通过测试该文件夹可以持续放入索引 每建索一次 就会生成对应的文件
           //Directory directory=FSDirectory.open(new File(pathFile)); 
              IndexWriterConfig writerConfig = new IndexWriterConfig(Version.LUCENE_34, analyzer);
              IndexWriter indexWriter = new IndexWriter(directory, writerConfig);
              /**
               * Field.Store.YES 表示是否存储    Field.Index.ANALYZED 表示对该对象是否建索
               * 一般存储需要展示的内容,对有用的如:标题、内容进行建索,链接或者序号不用建索。
               */
              Document document1 = new Document();
              document1.add(new Field(fieldName, "中国上海", Field.Store.YES, Field.Index.ANALYZED));
              document1.add(new Field(fieldValue, text1, Field.Store.YES, Field.Index.ANALYZED));
              indexWriter.addDocument(document1);
             
              Document document2 = new Document();
              document2.add(new Field(fieldName, "中国天津", Field.Store.YES, Field.Index.ANALYZED));
              document2.add(new Field(fieldValue, text2, Field.Store.YES, Field.Index.ANALYZED));
              indexWriter.addDocument(document2);
             
              Document document3 = new Document();
              document3.add(new Field(fieldName, "北京", Field.Store.YES, Field.Index.ANALYZED));
              document3.add(new Field(fieldValue, text3, Field.Store.YES, Field.Index.ANALYZED));
              indexWriter.addDocument(document3);
              //此时删除的文档并未完全删除,而是存储在回收站中,可以恢复的 
              //indexWriter.forceMergeDeletes();
              //indexWriter.ramSizeInBytes();
              indexWriter.close();
   } catch (Exception e) {
    e.printStackTrace();
   }
 }

 

解决方法:修改pathFile文件夹路径  。

     谷歌了一下,好像是上次建索失败导致。具体原因不知道,希望知道原因的大手给解释一下!虾米不胜感激!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值