按子项计数的Hibernate搜索顺序 [英] Hibernate Search Order by child-count

查看:88
本文介绍了按子项计数的Hibernate搜索顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



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

.....
私人套餐< 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(...)

count be be?

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



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



任何帮助,指针,评论甚至批评欢迎。



非常感谢
John

解决方案

在hibernate搜索中,您可以自定义为此目的。



以下内容:

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

使用自定义网桥实现:

  public class CollectionCountBridge extends PaddedIntegerBridge {

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


Consider:

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

A query would be something like this:

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

How can a sort-by-child-count be achieved?

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

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.

Thanks Very Much John

解决方案

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

Something along the lines of:

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

With the custom bridge implementation:

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搜索顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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