lucene:全文检索lucene学习笔记(一)

  lucene: http://lucene.apache.org/java/docs/index.html

  资料:http://levi.bloghome.cn/posts/121531.html

  代码如下:

  view plaincopy to clipboardpr?

//生成索引:  
packagecom.lucene.index;  
importjava.io.File;  
importjava.io.FileReader;  
importjava.io.IOException;  
importorg.apache.lucene.analysis.standard.StandardAnalyzer;  
importorg.apache.lucene.document.Document;  
importorg.apache.lucene.document.Field;  
importorg.apache.lucene.index.IndexWriter;  
publicIndexer{  
  publicvoid(Stringargs)throwsIOException{  
      
    FileindexDir=File("C:  estindex");  
    FiledataDir =File("C:  estdata");  
      
    numIndexed=index(indexDir,dataDir);  
      
    .out.prln(numIndexed);  
  }  
    
  publicindex(FileindexDir,FiledataDir)throwsIOException{  
      
    (!indexDir.exists||!dataDir.isDirectory){  
      throwIOException;  
    }  
      
    IndexWriterwriter=IndexWriter(indexDir,StandardAnalyzer,true);  
    writer.UseCompoundFile(false);  
      
    indexDirectory(writer,dataDir);  
      
    numIndexed=writer.docCount;  
    writer.optimize;  
    writer.close;  
    numIndexed;  
  }  
    
  privatevoidindexDirectory(IndexWriterwriter,Filedir)throwsIOException{  
      
    Filefiles=dir.listFiles;  
      
    for(i=0;i<files.length;i){  
      Filef=files[i];  
      (f.isDirectory){  
        indexDirectory(writer,f);  
      }(f.getName.toLowerCase.endsWith(".txt")){  
        indexFile(writer,f);  
      }  
    }  
  }  
    
  publicvoidindexFile(IndexWriterwriter,Filef)throwsIOException{  
      
    (f.isHidden||!f.exists||!f.canRead){  
      ;  
    }  
      
    .out.prln("Indexing"+f.getCanonicalPath);  
      
    Documentdoc=Document;  
    doc.add(Field("filename",f.getCanonicalPath,Field.Store.YES,Field.Index.UN_TOKENIZED));  
    doc.add(Field("contents",FileReader(f)));  
    writer.addDocument(doc);  
  }  
}  
//查询代码:  
packagecom.lucene.search;  
importjava.io.File;  
importjava.io.IOException;  
importorg.apache.lucene.analysis.standard.StandardAnalyzer;  
importorg.apache.lucene.queryParser.QueryParser;  
importorg.apache.lucene.search.Hits;  
importorg.apache.lucene.search.IndexSearcher;  
importorg.apache.lucene.search.Query;  
importorg.apache.lucene.store.Directory;  
importorg.apache.lucene.store.FSDirectory;  
publicSearcher{  
    
  publicvoid(Stringargs)throwsException{  
    FileindexDir=File("C:  estindex");  
    Stringq="ERROR";  
    (!indexDir.exists||!indexDir.isDirectory){  
      throwIOException;  
    }  
    search(indexDir,q);  
  }  
  publicvoidsearch(FileindexDir,Stringq)throwsException{  
    DirectoryfsDir=FSDirectory.getDirectory(indexDir);  
    IndexSearchersearcher=IndexSearcher(fsDir);  
    QueryParserparser=QueryParser("contents",StandardAnalyzer);  
    Queryquery=parser.parse(q);  
    Hitshits=searcher.search(query);  
    .out.prln("共有"+searcher.maxDoc+"条索引命中"+hits.length+"条");  
    for(i=0;i<hits.length;i){  
      DocId=hits.id(i);  
      StringDocPath=hits.doc(i).get("filename");  
      .out.prln(DocId+":"+DocPath);  
    }  
  }  
} 


Tags:  lucene教程 lucene.net luceneinaction lucene

延伸阅读

最新评论

发表评论