lucene源代码研究之如何开始

转载自:http://blog.csdn.net/hustsdy/article/details/4133033

万事开头难,在开始研究lucene就如猫吃乌龟,不知从何下手。承蒙高手点化,有所心得,记之。

   对于java一样面向对象的语言来说,几乎每一个java源文件都是有一个个类组成的,即使在知道lucene的工作流程之后,也不好下手,尤其对于对象之间的调用,很是迷茫。

   最开始试想寻找lucene的入口点可以从main函数开始,但是,在执行main函数时,并没有出现自己想要的结果,甚是痛苦,不得不求助于人。最终还是得到了满意的解决方案。

   解决方案:

   新建一个test类,把整个索引的过程从头到尾走一遍,然后用单步调试跟踪,直到调用涉及底层方法为止。

  新建类的源代码如下:

[java]  view plain copy
  1. package test;  
  2.   
  3.   
  4. import java.io.BufferedReader;  
  5. import java.io.File;  
  6. import java.io.FileInputStream;  
  7. import java.io.InputStreamReader;  
  8. import java.io.Reader;  
  9. import java.util.Date;  
  10.   
  11. import org.apache.lucene.analysis.standard.StandardAnalyzer;  
  12. import org.apache.lucene.document.Document;  
  13. import org.apache.lucene.document.Field;  
  14. import org.apache.lucene.index.IndexWriter;  
  15.   
  16. public class LuceneIndex {  
  17.     public static void main(String[] args) throws Exception {  
  18.         // 声明一个对象  
  19.         LuceneIndex indexer = new LuceneIndex();  
  20.         // 建立索引  
  21.         Date start = new Date();  
  22.         System.out.println(start);  
  23.         indexer.writeToIndex();  
  24.         Date end = new Date();  
  25.         System.out.println(end);  
  26.           
  27.   
  28.         System.out.println("建立索引用时" + (end.getTime() - start.getTime()) + "毫秒");  
  29.   
  30.         indexer.close();  
  31.     }  
  32.   
  33.     public LuceneIndex() {  
  34.         try {  
  35.             writer = new IndexWriter(Constants.INDEX_STORE_PATH,  
  36.                     new StandardAnalyzer(), true);  
  37.         } catch (Exception e) {  
  38.             e.printStackTrace();  
  39.         }  
  40.     }  
  41.   
  42.     // 索引器  
  43.     private IndexWriter writer = null;  
  44.   
  45.     // 将要建立索引的文件构造成一个Document对象,并添加一个域"content"  
  46.     private Document getDocument(File f) throws Exception {  
  47.         Document doc = new Document();  
  48.   
  49.         FileInputStream is = new FileInputStream(f);  
  50.         Reader reader = new BufferedReader(new InputStreamReader(is));  
  51.     //  doc.add(Field.Text("contents", reader));  
  52.         doc.add(new Field("contents",reader));  
  53.     //  doc.add(new Field("path", f.getAbsolutePath()));  
  54.         return doc;  
  55.     }  
  56.   
  57.     public void writeToIndex() throws Exception {  
  58.         File folder = new File(Constants.INDEX_FILE_PATH);  
  59.         if (folder.isDirectory()) {  
  60.             String[] files = folder.list();  
  61.             for (int i = 0; i < files.length; i++) {  
  62.                 File file = new File(folder, files[i]);  
  63.                 Document doc = getDocument(file);  
  64.                 System.out.println("正在建立索引 : " + file + "");  
  65.                 writer.addDocument(doc);  
  66.             }  
  67.         }  
  68.     }  
  69.   
  70.     public void close() throws Exception {  
  71.         writer.close();  
  72.     }  
  73.   
  74. }  
  

双击代码左栏,设置断点,

然后再按F11进入debug状态,在这个过程中有几个快捷键要知道

F5:step into进入方法体

F6:step over跳过方法体

F7:step return返回上一步

 

了解这几个快捷键有助于加快调试过程。

 

这样多次使用F5可以看到对象之间的调用。为了更好的研究代码,觉得有款软件很好

Source Insight 推荐给大家

在我上传资源上面有

http://download.csdn.net/source/1256356

是英文版的,其实英文版面挺好用的,O(∩_∩)O

 

 

自己也是刚刚研究源代码,有什么不足之处还望各位斧正。

看代码是个很痛苦的过程,最好在你心静的时候研究,在晚上夜深人静的时候看更好

 

有时候就在想,如果晚上能把自己与心仪的女孩子聊天的那股热情用到看代码上面,小小的一个lucene岂不是区区小菜


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值