在hibernate搜索中使用@classbridge获取索引中新添加的字段

时间:2012-10-11 13:52:45

标签: lucene hibernate-search

我正在实施@ClassBridge,我的课程是:

@Entity
@Table(name = "metaentity")
@Indexed
@ClassBridge(name="metaentityinedx",index=Index.TOKENIZED,store=Store.YES,impl = CustomBridge.class)
public class Metaentity implements java.io.Serializable {
private Long id;
@Field
private String type;
@Field
private String title;
private Set<Comment> comments = new HashSet<Comment>(0);

public Metaentity() {
}

public Metaentity(ntityByProject, String type, String title) {
    this.type = type;
    this.title = title;
}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Long getId() {
    return this.id;
}

public void setId(Long id) {
    this.id = id;
}

@Column(name = "type", nullable = false, length = 45)
public String getType() {
    return this.type;
}

public void setType(String type) {
    this.type = type;
}

@Column(name = "title", nullable = false, length = 45)
public String getTitle() {
    return this.title;
}

public void setTitle(String title) {
    this.title = title;
}

@OneToMany(fetch = FetchType.LAZY, mappedBy = "metaentity")
public Set<Comment> getComments() {
    return this.comments;
}

public void setComments(Set<Comment> comments) {
    this.comments = comments;
}

}

我的CustomBridge.java是:

public class CustomBridge implements FieldBridge {

    @Override
    public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
        String hasComment="false";
        Metaentity metaentity = (Metaentity) value;
        Set<Comment> commentset = metaentity.getComments();
        if(commentset.size()>0)
            hasComment = "true";
        luceneOptions.addFieldToDocument("hasComments",hasComment, document);
    }
}

我在 CustomBridge 类中所做的是当元实体 注释&gt; 0 然后将 hascomments 字段设置为true时,否则为假。这会在索引中正确创建 hascomment 字段。

但是当我进行列表时,它只显示 Metaentity 类中的字段而不是我已编入索引的 hascomment 字段。我希望 hascomment 字段包含元实体

如果我想要使用 hasComment 字段定义元实体字段,我该怎么办。要做列表我用过:

   FullTextSession fullTextSession = Search.getFullTextSession(session);
     Transaction tx = fullTextSession.beginTransaction();
       QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Metaentity.class).get();                              
       org.apache.lucene.search.Query luceneQuery =  qb.keyword().onFields("hasComments").matching("true").createQuery();
        org.hibernate.Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery, Metaentity.class);
        List result = hibQuery.list();

        Iterator<Metaentity> itr = result.iterator();

在不使用查询解析器的情况下查看我的lucene查询,简而言之,在我的customBridge类中,我在元实体索引中添加了新字段“hascomment”。当我在进行搜索时我希望metaentity类的所有字段都包含“ hascomment“我已添加到索引文档的字段。

0 个答案:

没有答案