Hibernate搜索4.4.2按日期排序并不起作用

时间:2015-12-31 07:11:03

标签: java hibernate sorting lucene hibernate-search

我正在使用带有lucene的hibernate搜索4.4.2进行全文搜索。请参阅以下方法,我在其中添加日期字段的排序条件并添加到全文搜索查询

执行全文搜索的方法

public List<TransactionDetails> getRechargeListBySearch(int start,int range,String searchinput,User user){
    Session session = CommonUtil.getSession(sessionFactory);
    FullTextSession fullTextSession = Search.getFullTextSession(session);
    Transaction tx = fullTextSession.beginTransaction();
    QueryBuilder qb = fullTextSession.getSearchFactory()
        .buildQueryBuilder().forEntity( TransactionDetails.class ).get();
    org.apache.lucene.search.Query query = null;
    if(user.getUserType().equals("R"))
    query = qb.bool().
            should(qb.keyword().onField("userId").matching(user.getUserId().toString()).createQuery())
            .should(qb.keyword().onField("user.distrId").matching(user.getDistrId()).createQuery())
            .must(qb.keyword().onFields("custMobNo", "user.name", "user.orgName","rechargeAmt","createdDate")
            .matching(searchinput).createQuery())
            .createQuery();
    else if(user.getUserType().equals("D"))
        query = qb.bool().
        should(qb.keyword().onField("user.distrId").matching(user.getUserId().toString()).createQuery())
        .must(qb.keyword().onFields("custMobNo", "user.name", "user.orgName","rechargeAmt")
        .matching(searchinput).createQuery())
        .createQuery();
    // Adding sort criteria by createdDate desc
    Criteria c = session.createCriteria(TransactionDetails.class).addOrder(Order.desc("createdDate"));
    org.hibernate.Query hibQuery = 
        fullTextSession.createFullTextQuery(query).setCriteriaQuery(c);
    hibQuery.setFirstResult(start);
    hibQuery.setMaxResults(range);
    // execute search
    List<TransactionDetails> result = (List<TransactionDetails>) hibQuery.list();
    tx.commit();
    session.close();
    return result;
}

Hibernate实体类 - TransactionDetails

@Entity
@Table(name="mr_transaction_tbl")
@Indexed
public class TransactionDetails {

    @DocumentId
    @Id @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name="TransID")
    private Long transID;


    @Column(name="MsgTransID",insertable=false,updatable=false)
    private Long msgTransID;

    @Field(index= Index.YES,analyze=Analyze.YES,store=Store.NO)
    @Column(name="UserID")
    private Long userId;

    @Field(index= Index.YES,analyze=Analyze.YES,store=Store.NO)
    @Column(name="CustMobNo")
    private String custMobNo;

    @Column(name="ServPdr")
    private String servPdr;

    @Field(index= Index.YES,analyze=Analyze.YES,store=Store.NO)
    @Column(name="RechargeAmt")
    private BigDecimal rechargeAmt;

    @Column(name="MarsReferences")
    private String marsReferences;

    @Column(name="CreatedDate",insertable=false,updatable=false)
    private Date createdDate;

    @Column(name="Status")
    private String status;

    @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
    @JoinColumn(name="MsgTransID",insertable=true,updatable=true,nullable=false,unique=true)
    private TransactionMsg transactionMsg;

     @IndexedEmbedded
     @OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
     @JoinColumn(name="UserID",insertable=false,updatable=false)
     private User user;

搜索结果按降序排序并不会发生。我感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

您需要使用Criteria

,而不是添加新的SortField
org.hibernate.search.FullTextQuery query = s.createFullTextQuery( query, Book.class );
org.apache.lucene.search.Sort sort = new Sort(
new SortField("title", SortField.STRING));
query.setSort(sort);
List results = query.list();

documentation就在这里。