Hibernate Search sorting

别来无恙 提交于 2019-12-08 17:00:53

问题


Hibernate search is sorting results depending on relevance, it is normal. In addition to that, if two documents are having the same score, they are ordered by their primary keys.

For example,

book1 : id=1, bookTitle = "hibernate search by example".

book2 : id=2, bookTitle = "hibernate search in action"

If I am doing a query to look for terms "hibernate search", I would have this order : book1 then book2

I would like to invert this order : book2 then book1. Which means inverting primary key order. Is there a possible way to do this without implementing a custom Similarity ? At the same time keeping relevance order.


回答1:


Yes, you need to create a Sort object which specifies the desired sorting, and set it in your query. See Section 5.1.3.3 of the Hibernate docs. Then, in the list of SortFields pass SortField.FIELD_SCORE. SortField's constructor also allows you to reverse the order.

org.hibernate.search.FullTextQuery query = s.createFullTextQuery( luceneQuery, MyEntity.class );
org.apache.lucene.search.Sort sort = new Sort(
    SortField.FIELD_SCORE, 
    new SortField("id", SortField.STRING, true));
query.setSort(sort);
List results = query.list();


来源:https://stackoverflow.com/questions/30222490/hibernate-search-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!