lucene2.4测试二RAMDirectoryTest

来源:互联网 发布:json python 编辑:程序博客网 时间:2024/06/02 01:16
  1. package lucene.test;
  2. import java.io.IOException;
  3. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  4. import org.apache.lucene.document.Document;
  5. import org.apache.lucene.document.Field;
  6. import org.apache.lucene.index.CorruptIndexException;
  7. import org.apache.lucene.index.IndexWriter;
  8. import org.apache.lucene.index.IndexWriter.MaxFieldLength;
  9. import org.apache.lucene.queryParser.ParseException;
  10. import org.apache.lucene.queryParser.QueryParser;
  11. import org.apache.lucene.search.IndexSearcher;
  12. import org.apache.lucene.search.Query;
  13. import org.apache.lucene.search.ScoreDoc;
  14. import org.apache.lucene.search.Searcher;
  15. import org.apache.lucene.search.TopDocCollector;
  16. import org.apache.lucene.store.LockObtainFailedException;
  17. import org.apache.lucene.store.RAMDirectory;
  18. public class RAMDirectoryTest {
  19.     public static void main(String[] args) throws ParseException{
  20.         RAMDirectory ramindex=new RAMDirectory();
  21.         
  22.         try {
  23.             IndexWriter writer=new IndexWriter(ramindex,new StandardAnalyzer(),true,MaxFieldLength.LIMITED);
  24.             writer.addDocument(createDocument("道德经","天地不仁,以万物为刍狗,圣人不仁,以百姓为刍狗。"));
  25.             writer.addDocument(createDocument("大学","大学之道,在明明德,在亲民,在止于至善。"));
  26.             writer.addDocument(createDocument("黄帝内经","恬惔虚无,精神内守;食饮有节,谨和五味。"));
  27.             writer.addDocument(createDocument("中庸","忠恕违道不远,施诸己而不愿,亦勿施于人。"));
  28.             
  29.             writer.optimize();
  30.             writer.close();
  31.             
  32.             Searcher searcher=new IndexSearcher(ramindex);
  33.             search(searcher,"德");
  34.         } catch (CorruptIndexException e) {
  35.             // TODO Auto-generated catch block
  36.             e.printStackTrace();
  37.         } catch (LockObtainFailedException e) {
  38.             // TODO Auto-generated catch block
  39.             e.printStackTrace();
  40.         } catch (IOException e) {
  41.             // TODO Auto-generated catch block
  42.             e.printStackTrace();
  43.         }
  44.         
  45.     }
  46.     private static void search(Searcher searcher, String queryString) throws ParseException, IOException{
  47.         // TODO Auto-generated method stub
  48.         QueryParser parser=new QueryParser("content",new StandardAnalyzer());
  49.         Query query=parser.parse(queryString);
  50.         
  51.         int hitsPerPage=100;
  52.         TopDocCollector collector=new TopDocCollector(hitsPerPage);
  53.         searcher.search(query,collector);
  54.         ScoreDoc[] hits=collector.topDocs().scoreDocs;
  55.         int hitCount=hits.length;
  56.         if(hitCount==0){
  57.             System.out.println("没有找到 /""+queryString+"/"");
  58.         }else{
  59.             System.out.println("查找'"+queryString+"'返回"+hitCount+"个结果");
  60.         }
  61.         for(int i=0;i<hits.length;i++){
  62.             int docId=hits[i].doc;
  63.             Document doc=searcher.doc(docId);
  64.             System.out.println("文档编号:"+docId+":"+doc.get("title")+doc.get("content"));
  65.         }
  66.     }
  67.     private static Document createDocument(String title, String content) {
  68.         // TODO Auto-generated method stub
  69.         Document doc=new Document();
  70.         doc.add(new Field("title",title,Field.Store.YES,Field.Index.ANALYZED));
  71.         doc.add(new Field("content",content,Field.Store.YES,Field.Index.ANALYZED));
  72.         return doc;
  73.         
  74.     }
  75. }
原创粉丝点击