2012-05-05 24 views
12

我是Java和Lucene的新手。我的代碼從文件中獲取一行並將其存儲在Lucene索引中。但是,當我創建一個IndexReader來搜索並從索引讀取它會引發異常。org.apache.lucene.index.IndexNotFoundException:在org.apache.lucene.store.RAMDirectory中找不到任何段*文件

我的java代碼如下。在創建IndexReader它拋出一個IndexNotFoundException

static String itemsfreq[]; 
static StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_35); 
static IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_35, analyzer); 

public static void index_data(Directory indexed_document,int doc_num,IndexWriter w) throws IOException 
    { 
    for(int i = 0;i < itemsfreq.length;i++) 
     { 
     Document doc = new Document(); 
     doc.add(new Field(Integer.toString(doc_num)+","+itemsfreq[i],itemsfreq[i++], Field.Store.YES, Field.Index.ANALYZED)); 
     w.addDocument(doc); 
     } 
    } 
//Gets string from a file and insert it in INDEX named indexed_document 
public static void main(String[] args) throws IOException 
    { 
    BufferedReader reader = new BufferedReader(new FileReader("fullText100.txt")); 
    String line; 
    int i = 0; 
    Directory indexed_document = new RAMDirectory(); 
    IndexWriter writer = new IndexWriter(indexed_document, config); 
    while((line=reader.readLine()) != null) 
     { 
     if(i == 1) 
      { 
      break; 
      } 
     itemsfreq = line.split(" "); 
     index_data(indexed_document,i,writer); 
     i++; 
     } 

    IndexReader r = IndexReader.open(indexed_document); 
    } 

回答

14

爲了寫你必須關閉該指數作家,然後打開的IndexReader的更改指數。

writer.close(); 

如果你有完成寫作之前打開的IndexReader,你必須告訴的IndexReader重新打開索引才能看到的變化。

+0

我應用本方案和它的工作。 –

16

前開放使用閱讀器的索引,再次呼籲writer.commit()

+1

我正面臨類似的問題,並使用'writer.commit()'修復它。如果我使用'writer.close()',我將不得不重新打開作者。你的建議比前一個更好。 – tuxdna

+2

事實上,這是正確的解決方案,因爲Lucene文檔建議重新使用IndexWriter和IndexReader的單個實例;這樣我就不會被迫關閉作家並在需要時重新創建它。 – Serg

3

你需要做的是顯式調用打開您的IndexSearcher的前提交。

directory = new RAMDirectory(); 
    iwriter = new IndexWriter(directory, config); 
    iwriter.commit(); 

現在打開搜索器

ireader = DirectoryReader.open(directory); 
isearcher = new IndexSearcher(ireader); 

還記得你需要調用添加文件另有搜索可能不會發現它後提交。搜索者需要在提交後重新打開(當然關閉舊的搜索器)。

iwriter.commit(); 
0

我得到這個錯誤(在Lucene.Net,C#),因爲我已經通過創建我的文件系統和存儲FSDirectory相應的目錄中創建的索引,但實際上並沒有添加任何文件還。

具體而言,添加新文檔的代碼正在檢查以確保它沒有向讀者添加重複內容,但是在嘗試添加第一個文檔時拋出異常,因爲還沒有任何分段。

我處理了這個像這樣:

// Make a reader to check the index for duplicates 
// The reader will only be aware of items that were in the index before it was created 
IndexReader reader = null; 
try { 
    reader = IndexReader.Open(index, true); 
} catch(IOException) { 
    // There is no segments file because the index is empty 
    reader = null; 
} 

... // inside a method called by that scope, with reader passed in 

// See if it exists already 
// If the reader is null, then the index is empty 
if(reader != null) { 
    var existsTerm = new Term(SingleFieldIndexManager.FieldName, formattedTerm); 
    var matches = reader.TermDocs(existsTerm); 
    if(matches.Next()) { 
     // It's already in there, no need to add again 
     return; 
    } 
} 

... // back to the first method after a return 

// dispose of the reader if we managed to create one 
if(reader != null) { 
    reader.Dispose(); 
} 
+0

嘿,我有和你一樣的問題。但我不明白你是如何解決它的。如果沒有文件,那麼它會進入catch塊,就是這樣,對吧? – Valentin

+0

發現問題。使用參數create = false創建IndexWriter時出現此錯誤 – Valentin

相關問題