問題描述
我在 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.
使用 public final List
代替.Document
的 getFields()
Use public final List<IndexableField> getFields()
of Document
instead.
這篇關于Lucene - 檢索文檔中多值字段的所有值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!