2012-04-28 86 views
1

我正在實現基於hibernate search3.2的圖書搜索功能。如何用逗號分隔的文本值索引字段 - hibernate搜索

Book對象包含一個名爲authornames的字段。 Authornames值是名稱的列表和逗號是分路器,說:「約翰·威爾,羅賓·羅德,詹姆斯Timerberland」

@Field(index = org.hibernate.search.annotations.Index.UN_TOKENIZED,store=Store.YES) 
@FieldBridge(impl=CollectionToCSVBridge.class) 
private Set<String> authornames; 

我需要每名被UN_TOKENIZED,讓用戶搜索單作者姓名書:約翰,羅賓羅德或James Timerberland。

我用盧克檢查指數法,並在authornames字段值存儲爲「約翰·威爾,羅賓·羅德,詹姆斯Timerberland」,但我不能得到通過查詢結果「authornames:約翰將」

任何人都可以告訴我我該怎麼做?

回答

1

我想CollectionToCSVBridge將所有名稱與大字符串中的「,」連接起來。 你應該讓他們分開,而不是與單獨添加每個元素的索引:

@Override 
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) { 
    if (value == null) { 
     return; 
    } 
    if (!(value instanceof Collection)) { 
     throw new IllegalArgumentException("This FieldBridge only supports collections."); 
    } 
    Collection<?> objects = (Collection<?>) value; 

    for (Object object : objects) { 
     luceneOptions.addFieldToDocument(name, objectToString(object), document); // in your case objectToString could do just a #toString 
    } 
} 

參見https://forum.hibernate.org/viewtopic.php?f=9&t=1015286&start=0

相關問題