我的生成索引的类如下:其中index里有1.txt,2.txt,3.txt
package ceshi;import java.io.IOException;  
import java.io.StringReader;  
  
import org.apache.lucene.analysis.Analyzer;  
import org.apache.lucene.analysis.Token;  
import org.apache.lucene.analysis.TokenStream;  
import org.apache.lucene.analysis.standard.StandardAnalyzer;  import  java.io.BufferedReader;   
import  java.io.File;   
import  java.io.FileInputStream;   
  
import  java.io.InputStreamReader;   
import  java.util.Date;   
  import  org.apache.lucene.document.Document;   
import  org.apache.lucene.document.Field;   
import  org.apache.lucene.index.IndexWriter;   
  
 /** */ /**   
 * author lighter date 2006-8-7  
  */   
 public   class  TextFileIndexer   {   
     public   static   void  main(String[] args)  throws  Exception   {   
         /**/ /*  指明要索引文件夹的位置,这里是C盘的S文件夹下  */   
       // File fileDir  =   new  File( " c:/s" );   
        String filePath1="c:/s/";
        filePath1=filePath1.toString();//中文转换
        java.io.File fileDir=new java.io.File(filePath1);
         /**/ /*  这里放索引文件的位置  */  
        String filePath2="c:/index1/";
        filePath2=filePath2.toString();//中文转换
        java.io.File indexDir=new java.io.File(filePath2);       // File indexDir  =   new  File( " c:/index" );  
        System.out.println("are you ok?");
        Analyzer luceneAnalyzer  = new  StandardAnalyzer(); 
       
        
        IndexWriter indexWriter  =   new  IndexWriter(indexDir, luceneAnalyzer,true );   
        System.out.println("perhaps ok?");
        File[] textFiles  =  fileDir.listFiles();   
         long  startTime  =   new  Date().getTime();   
         System.out.println(textFiles.length);
         // 增加document到索引去    
          for  ( int  i  =   0 ; i  <  textFiles.length; i ++ )   {  
           System.out.println("seems ok?");
           if(textFiles[i].isFile()  ){
           System.out.println("it is a file!");
           }
           if(textFiles[i].getName().endsWith( " .txt " )){
           System.out.println("it is a txt file!!!");
           }
             if  (textFiles[i].isFile())   {   
              System.out.println("is here coming?");
                System.out.println( " File  "   +  textFiles[i].getCanonicalPath()   
                         +   " 正在被索引.... " );   
                String temp  =  FileReaderAll(textFiles[i].getCanonicalPath(),   
                         "utf-8" );   
                System.out.println(temp);   
                Document document  =   new  Document();   
                Field FieldPath  =   new  Field( " path " , textFiles[i].getPath(),   
                        Field.Store.YES, Field.Index.NO);   
                Field FieldBody  =   new  Field( " body " , temp, Field.Store.YES,   
                        Field.Index.TOKENIZED,   
                        Field.TermVector.WITH_POSITIONS_OFFSETS);   
                document.add(FieldPath);   
                document.add(FieldBody);   
                indexWriter.addDocument(document);   
            }    
        }    
         // optimize()方法是对索引进行优化    
        indexWriter.optimize();   
        indexWriter.close();   
           
         // 测试一下索引的时间    
         long  endTime  =   new  Date().getTime();   
        System.out   
                .println( " 这花费了 "   
                         +  (endTime  -  startTime)   
                         +   "  毫秒来把文档增加到索引里面去! "   
                         +  fileDir.getPath());   
    }    
  
     public   static  String FileReaderAll(String FileName, String charset)   
             throws  IOException   {   
        BufferedReader reader  =   new  BufferedReader( new  InputStreamReader(   
                 new  FileInputStream(FileName), charset));   
        String line  =   new  String();   
        String temp  =   new  String();   
           
         while  ((line  =  reader.readLine())  !=   null )   {   
            temp  +=  line;   
        }    
        reader.close();   
         return  temp;   
    }    
}  查找索引的类如下:
package sample.dw.paper.lucene.search;import java.io.IOException;  import org.apache.lucene.analysis.Analyzer;  
import org.apache.lucene.analysis.standard.StandardAnalyzer;  
import org.apache.lucene.queryParser.ParseException;  
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.apache.lucene.store.FSDirectory;
  
public class TestQuery {  
public static void main(String[] args) throws IOException, ParseException {   
        Hits hits = null;   
        String queryString = "ok";   
        Query query = null;   
        IndexSearcher searcher = new IndexSearcher("c:\\index1");   
  
        Analyzer analyzer = new StandardAnalyzer();   
        QueryParser qp = new QueryParser("body", analyzer);   
try {
    query = qp.parse(queryString);
    
    
    // 以下 是评分机制  的代码.等看懂这个例子后,可以把注销代码删了
    
//                hits = searcher.search(query);
//                for(int i=0;i<hits.length();i++){
//                    Explanation explanation = searcher.explain(query,hits.id(i));
//                    System.out.println("得分"+hits.score(i));
//                    System.out.println("具体情况"+explanation);
//                    System.out.println("长度"+hits.length());
//                    
//                }
    
    
} catch (org.apache.lucene.queryParser.ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}   
        if (searcher != null) {   
            hits = searcher.search(query);   
            if (hits.length() > 0) {   
                System.out.println("找到:" + hits.length() + " 个结果!");   
            }   
            else{
                System.out.print("没有找到!!!!!!!!");
            }
        }   
       
    } 
}  
但是,控制台一直打印出找不到,请各位帮我看看是怎么回事啊

解决方案 »

  1.   

    public class TestQuery {  
    public static void main(String[] args) throws IOException, ParseException {  
      Hits hits = null;  
      String queryString = "ok";  
      Query query = null;  
      IndexSearcher searcher = new IndexSearcher("c:\\index1");  
      
      Analyzer analyzer = new StandardAnalyzer();  
      QueryParser qp = new QueryParser("body", analyzer);  
    try {
    query = qp.parse(queryString);
    // 以下 是评分机制 的代码.等看懂这个例子后,可以把注销代码删了
    // hits = searcher.search(query);
    // for(int i=0;i<hits.length();i++){
    // Explanation explanation = searcher.explain(query,hits.id(i));
    // System.out.println("得分"+hits.score(i));
    // System.out.println("具体情况"+explanation);
    // System.out.println("长度"+hits.length());
    //  
    // }
    } catch (org.apache.lucene.queryParser.ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }  
      if (searcher != null) {  
      hits = searcher.search(query);  
      if (hits.length() > 0) {  
      System.out.println("找到:" + hits.length() + " 个结果!");  
      }  
      else{
      System.out.print("没有找到!!!!!!!!");
      }
      }  
       
      } 
    }
    query 你根本没赋值,一直为NULL。   hits = searcher.search(query);这里你肯定得不到结果呀。
    hits.length() > 0根本就不会实现。肯定会打印 "没有找到!!!!!!!!"咯。
      

  2.   

    呀。看错了。query取到值啦。看这代码眼看的有点花。老掉了。不好意思
      

  3.   

    调试的query是有值的,各位再帮忙看看是怎么回事啊?谢谢了
      

  4.   

    if (searcher != null) {   
      hits = searcher.search(query);   
      if (hits.length() > 0) {   
      System.out.println("找到:" + hits.length() + " 个结果!");   
      }   
      else{
      System.out.print("没有找到!!!!!!!!");
      }
      }   
        
    能打印"没有找到!!!!!!!!",说明进到这里去了。但是length>0不会实现。 你看看search()方法吧。
    说明  hits = searcher.search(query);  可能是null。你打印下试试。