package org.wdj.demo.lucene;import java.io.IOException;
import java.util.Date;import org.apache.lucene.analysis.Analyzer;
//import org.apache.lucene.analysis.standard.ParseException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.mira.lucene.analysis.MIK_CAnalyzer;public class R { /**
 * @param args
 * @throws org.apache.lucene.queryParser.ParseException 
 * @throws IOException 
 * @throws Exception 
 */
public static void main(String[] args) throws org.apache.lucene.queryParser.ParseException, Exception, IOException {
Hits hits = null;
String queryString = "测试";
Query query = null;
IndexSearcher searcher = new IndexSearcher("d:\\luceneIndex"); Date start = new Date();
//Analyzer analyzer = new StandardAnalyzer();
 Analyzer analyzer = new MIK_CAnalyzer();
QueryParser qp = new QueryParser("TITLE", analyzer);
query = qp.parse(queryString);
Date end = new Date();
System.out.println(System.currentTimeMillis());
if (searcher != null) {
hits = searcher.search(query);
System.out.println("找到:" + hits.length() + " 个结果,一共用了 "+(end.getTime()-start.getTime())+" 毫秒!");
System.out.println("");
if (hits.length() > 0) {
for(int i=0;i<hits.length();i++){
//Thread.sleep(50);
Document doc = hits.doc(i);
System.out.print ("DID: "+doc.get("DID")+"  ");
System.out.print ("TITLE: "+doc.get("TITLE")+"  ");
System.out.println("TBLNAME: "+doc.get("TBLNAME"));
}

System.out.println();
System.out.println("找到:" + hits.length() + " 个结果,一共用了 "+(end.getTime()-start.getTime())+" 毫秒!");

}
}
}}
运行的时候出现Exception in thread "main" org.apache.lucene.index.CorruptIndexException: Unknown format version: -4
at org.apache.lucene.index.SegmentInfos.read(SegmentInfos.java:204)
at org.apache.lucene.index.IndexReader$1.doBody(IndexReader.java:190)
at org.apache.lucene.index.SegmentInfos$FindSegmentsFile.run(SegmentInfos.java:610)
at org.apache.lucene.index.IndexReader.open(IndexReader.java:185)
at org.apache.lucene.index.IndexReader.open(IndexReader.java:148)
at org.apache.lucene.search.IndexSearcher.<init>(IndexSearcher.java:48)
at org.wdj.demo.lucene.R.main(R.java:29)我这个是用eclipse 3.3做的,可是我用eclipse 3.1确没有任何错误,请问是怎么回事呢

解决方案 »

  1.   

    你用的lucene版本是多少,建议察看一下相应的版本信息,或者换最新的版本看看。有可能有些接口作了变动。
      

  2.   

    我也遇到了这个问题,但还没有注意是不是Eclipse的版本的问题,正在解决中。
      

  3.   

    向lqjava说的一样,我找到了这个:
    http://archive.netbsd.se/?ml=lucene-java-user&a=2007-11&t=5788781From: Michael McCandless
    Date: 2007-11-29 10:30:28--------------------------------------------------------------------------------That exception means your index was written with a newer version of
    Lucene than the version you are using to open the IndexReader.It looks like you used the unreleased (2.3 dev) version of Lucli from
    the Lucene trunk and then went back to an older Lucene JAR (maybe 2.2?)
    for accessing it?  In general writing an index with a newer version
    of Lucene and then trying to access it using an older version of Lucene
    doesn't work (whereas the opposite does).I'm afraid you either have to switch to 2.3-dev for reading your index
    (but beware it could have sneaky bugs ...), or, rebuild your index with
    the 2.2 version of Lucene and use the 2.2 Lucli in the future.Mike
      

  4.   

    我也遇到这个问题,原来的项目里用的是2.3版本,那时2.4版本还正在修正中。现在的项目里发现是2.4版本,用lukeall-0.8.1.jar就报这个错,貌似这个工具最多支持2.3版本,不支持2.4版本。