Hibernate Search Order by child-count [英] Hibernate Search Order by child-count

查看:12
本文介绍了Hibernate Search Order by child-count的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑:

@Indexed
@Entity
public class TParent  implements java.io.Serializable {

 .....
 private Set<TChild> TChildSet = new HashSet<TChild>(0);

 @ContainedIn
 @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="TParent")
 public Set<TChild> getTChildSet() {
     return this.TChildSet;
 }

查询应该是这样的:

FullTextQuery hibQuery = fullTextSession.createFullTextQuery( luceneQuery );
hibQuery.setSort( ... ) 

如何实现按子数排序?

换句话说,返回的 TParent 列表的顺序将由 TChildSet 计数决定.

In other words, the order of the TParent list returned would be dictated by the TChildSet count.

我知道一个@公式可以在 SQL 的情况下使用.我不确定Lucene是否可以使用类似的东西?

I know an @Formula can be used in SQL circumstances. I'm not sure if something similar can be used for Lucene?

欢迎任何帮助、指点、评论甚至批评.

Any help, pointers, comments even critique welcome.

非常感谢约翰

推荐答案

在hibernate搜索中,可以自定义Bridge 用于此目的.

In hibernate search, you can make a custom Bridge for this purpose.

类似的东西:

@FieldBridge(impl = com.myco.myapp.CollectionCountBridge.class)
@ContainedIn
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="TParent")
public Set<TChild> getTChildSet() {
     return this.TChildSet;
}

使用自定义桥接实现:

public class CollectionCountBridge extends PaddedIntegerBridge {

    @Override
    public String objectToString(Object object) {
        if (object == null || (!(object instanceof Collection))) {
            return null;
        }
        Collection<?> coll = (Collection<?>) object;
        return super.objectToString(coll.size());
    }
}

这篇关于Hibernate Search Order by child-count的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆