Lucene - retrieve all values for a multi-valued field in a document

丶灬走出姿态 提交于 2019-12-21 05:04:22

问题


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?


回答1:


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.

Use public final List<IndexableField> getFields() of Document instead.




回答2:


Use doc.getValues("categories").



来源:https://stackoverflow.com/questions/21049069/lucene-retrieve-all-values-for-a-multi-valued-field-in-a-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!