开发者

How to annotate to allow for searching a List<String> field using Hibernate Search

开发者 https://www.devze.com 2023-02-23 01:35 出处:网络
Let\'s say I have a domain object that looks like this: @Entity @Indexed public class Thingie implements DomainObject {

Let's say I have a domain object that looks like this:

@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??
p开发者_如何学Pythonublic List<String> getKeywords() {
    return keywords;
}
public void setKeywords(List<String> keywords) {
    this.keywords = keywords;
}       
 }

How do I annotate the keywords field such that I can do a search like this that will do a full text search of the title and keywords:

 org.apache.lucene.search.Query query = qb.keyword().onFields("title","keywords")
 .matching("search").createQuery();


you can use StringBridge. check 4.2.2.1. StringBridge in

http://docs.jboss.org/hibernate/search/3.1/reference/en/html/search-mapping-bridge.html

For example, if you store keywords in database in format of: aa,bb,cc

@FieldBridge(impl=CollectionToCSVBridge.class) //your bridge implementation
private List<String> keywords;

one implementation could be:

 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;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号