2012-04-28 34 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

相关问题