lucene關於IndexReader總結

c3tc3tc3t發表於2013-12-28

IndexReader。使用過程中有時會出現document被刪除,reader還是原來的reader沒有改變,所以使用openifchanged保證,

又因為IndexReader 初始化很耗費資源所以放在靜態程式碼塊裡

private static Directory directory = null;
    private static IndexReader reader = null;
    static {
        try {
            directory = FSDirectory.open(new File("E:/lucene/index02"));
            reader = IndexReader.open(directory);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

public IndexSearcher getIndexSearcher() {
        try {
            IndexReader tr = IndexReader.openIfChanged(reader);
            if (tr != null) {

      reader.close();
                reader = tr;
            }
            return new IndexSearcher(reader);
        } catch (IOException e) {

            e.printStackTrace();
        }
        return null;
    }

相關文章