假设我有一个域对象,如下所示:
@Entity
@Indexed
public class Thingie implements DomainObject {
private Long id;
private Integer version;
private String title;
private List<String> keywords = new Vector<String>();
@Id
@GeneratedValue
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Version
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Column(length=64, nullable=false)
@Field(index=Index.TOKENIZED,store=Store.NO)
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@ElementCollection
// what do I put here??
public List<String> getKeywords() {
return keywords;
}
public void setKeywords(List<String> keywords) {
this.keywords = keywords;
}
}
如何对关键字字段进行注释,以便我可以执行此类搜索,对标题和关键字进行全文搜索:
org.apache.lucene.search.Query query = qb.keyword().onFields("title","keywords")
.matching("search").createQuery();
答案 0 :(得分:5)
你可以使用StringBridge。检查4.2.2.1。
中的StringBridgehttp://docs.jboss.org/hibernate/search/3.1/reference/en/html/search-mapping-bridge.html
例如,如果您将关键字存储在数据库中,格式为:aa,bb,cc
@FieldBridge(impl=CollectionToCSVBridge.class) //your bridge implementation
private List<String> keywords;
一个实现可能是:
public class CollectionToCSVBridge implements StringBridge
{
public String objectToString(Object value)
{
if(value != null)
{
StringBuffer buf = new StringBuffer();
Collection<?> col = (Collection<?>)value;
Iterator<?> it = col.iterator();
while(it.hasNext())
{
String next = it.next().toString();
buf.append(next);
if(it.hasNext())
buf.append(", ");
}
return buf.toString();
}
return null;
}
}