2013-01-18 47 views
1

我目前正在嘗試從RamDirectory中的Lucene索引(v。4)中獲取所有文檔。Lucene Query不返回結果,即使它應該

上創建索引以下addDocument功能用於:

public void addDocument(int id, String[] values, String[] fields) throws IOException{ 
    Document doc = new Document(); 

    doc.add(new IntField("getAll", 1, IntField.TYPE_STORED));  
    doc.add(new IntField("ID", id, IntField.TYPE_STORED));            
    for(int i = 0; i < fields.length; i++){ 
     doc.add(new TextField(fields[i], values[i], Field.Store.NO)); 
    } 

    writer.addDocument(doc); 
} 

調用這個對作家來說,關閉所有文件後。 正如您從添加到文檔中的第一個字段可以看到的,我添加了一個附加字段「getAll」,以便輕鬆檢索所有文檔。如果我理解正確,查詢「getAll:1」應該返回索引中的所有文檔。但事實並非如此。 我使用下面的函數爲:

public List<Integer> getDocIds(int noOfDocs) throws IOException, ParseException{ 
    List<Integer> result = new ArrayList<Integer>(noOfDocs); 
    Query   query  = parser.parse("getAll:1"); 
    ScoreDoc[]  docs  = searcher.search(query, noOfDocs).scoreDocs; 

    for(ScoreDoc doc : docs){ 
     result.add(doc.doc); 
    } 

    return result; 
} 

noOfDocs是被索引的文檔的數量。當然,我在創建IndexSearcher時使用了相同的RamDirectory。 將解析的查詢替換爲手動創建的TermQuery也沒有幫助。 查詢返回沒有結果。

希望有人能幫助找到我的錯誤。 謝謝

回答

0

我覺得首先你應該運行Luke來驗證索引的內容。

另外,如果你允許*作爲查詢的第一個字符用queryParser.setAllowLeadingWildcard(true);,然後像ID:*查詢會檢索所有文檔,而不必包括GETALL場。

1

我相信你在搜索時遇到了問題,因爲你使用的是IntField,而不是StringField或TextField。 IntField和其他數字字段是爲數字範圍查詢而設計的,並且未以其原始格式編入索引。您可以使用NumericRangeQuery來搜索它們。

真的,IntField只能用於數字值,而不能用於一串數字,這就是你所看到的。通常,ID應該是關鍵字或文本字段。

至於拉所有記錄,你不需要添加一個字段來做到這一點。只需使用MatchAllDocsQuery即可。

+0

MatchAllQuery使它工作! - 也感謝關於Int-Field的信息,我會改變這一點 – dburgmann

相關問題