我目前正在嘗試從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也沒有幫助。 查詢返回沒有結果。
希望有人能幫助找到我的錯誤。 謝謝
MatchAllQuery使它工作! - 也感謝關於Int-Field的信息,我會改變這一點 – dburgmann