0

我正在实施@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类中所做的是当metaentity评论> 0然后将hascomments字段设置为 true,否则设置为 false。这会在索引中正确创建hascomment字段。

但是当我列出它时,它只显示Metaentity类中的字段,而不是我索引的hascomment字段。我想要带有metaentity的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 类中,我在 metaentity 的索引中添加了新字段“hascomment”。当我进行搜索时,我希望 metaentity 类的所有字段都带有我拥有的“hascomment”字段添加到索引文档中。

4

0 回答 0