问题描述
我在Lucene中添加了一个这样的多值字段:
I added a field in Lucene which is multi-valued as such:
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
}
后来,当我搜索某个类别中的项目时,一切都会按预期运行,但是当我获得文档并执行以下操作时:
later when I am searching for items in a category everything works as expected, but when I get a Document and do:
String categories= doc.getField("categories").stringValue();
我只会得到该文档的最后一个插入值,而不是为该文档添加的所有值.
I only get the last inserted value for that document rather than all the values that were added for that document.
如何获取为该文档添加的所有值?
How can I get all the values which were added for that document?
推荐答案
要添加到文档中的不是多值单个字段,而是具有相同名称的多个字段.最后,您只检索一个字段.
What you are adding to the document is not multi-valued single field, but multiple fields with the same name. At the end you are only retrieving one field.
改为使用Document
中的public final List<IndexableField> getFields()
.
这篇关于Lucene-检索文档中多值字段的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!