Hibernate 按子计数休眠搜索顺序

Hibernate 按子计数休眠搜索顺序,hibernate,lucene,hibernate-search,Hibernate,Lucene,Hibernate Search,考虑: @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") p

考虑:

@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;
 }
如何实现按子计数排序

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

我知道一个 @公式 可以在SQL环境中使用。我不确定类似的东西是否可以用于Lucene

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

非常感谢
John

在hibernate搜索中,您可以为此进行自定义

大致如下:

@FieldBridge(impl = com.myco.myapp.CollectionCountBridge.class)
@ContainedIn
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="TParent")
public Set<TChild> getTChildSet() {
     return this.TChildSet;
}
@FieldBridge(impl=com.myco.myapp.CollectionCountBridge.class)
@包含
@OneToMany(cascade=CascadeType.ALL,fetch=FetchType.LAZY,mappedBy=“TParent”)
公共集getChildSet(){
返回此.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());
    }
}
公共类集合CountBridge扩展填充的TegerBridge{
@凌驾
公共字符串objectToString(对象对象){
if(object==null | |(!(集合的对象实例))){
返回null;
}
Collection coll=(Collection)对象;
返回super.objectToString(coll.size());
}
}

非常感谢Don,这非常有帮助,非常感谢。为了让它工作,我不得不使用IntegerBridge而不是paddentegerbridge,这对我来说似乎不可用(HS v4.2.0)。这一切都很好,除了当我添加一个新的TChild(调用桥代码,我可以看到集合大小增加)时,排序在我重建索引之前不会更新。为了获得添加新TChild时要调用的桥代码,我在getChildSet()方法中添加了@Field(analysis=analysis.No)。知道为什么排序顺序没有改变吗?谢谢。下面是问题的第二部分:
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());
    }
}