Lucene - 检索文档中多值字段的所有值

Sol*_*gon 5 java lucene document

我在Lucene中添加了一个多值的字段:

String categoriesForItem = getCategories(); // returns "category1, category2, cat3" from a DB call

String [] categoriesForItems = categoriesForItem.split(","; 
for(String cat : categoriesForItems) {
    doc.add(new StringField("categories", cat , Field.Store.YES)); // doc is a Document 
}
Run Code Online (Sandbox Code Playgroud)

以后当我在一个类别中搜索项目时,一切都按预期工作,但是当我得到一个文档并执行:

String categories= doc.getField("categories").stringValue(); 
Run Code Online (Sandbox Code Playgroud)

我只获取该文档的最后插入值,而不是为该文档添加的所有值.

如何获取为该文档添加的所有值?

min*_*das 8

您要添加到文档的内容不是多值单个字段,而是具有相同名称的多个字段.最后,您只检索一个字段.

使用public final List<IndexableField> getFields()Document替代.