Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 706
SoluableNonagon Avatar asked Jan 10 '14 16:01

SoluableNonagon


2 Answers

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.

like image 113
mindas Avatar answered Nov 15 '22 03:11

mindas


Use doc.getValues("categories").

like image 21
rdm Avatar answered Nov 15 '22 03:11

rdm