0
votes

I'm doing full text search using hibernate search 4.4.2 with lucene. Please see the below method, where i add the sort criteria for the date field and add to the full text search query

Method that performs the Full text search

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 entity class - 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;

The sorting of search results in descending order doesn't happens. I appreciate all your help.

1

1 Answers

2
votes

Instead of adding a new Criteria, you need to use 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();

The documentation is here.