Hibernate搜索排序 [英] Hibernate Search sorting
问题描述
除此之外,如果两个文档的分数相同,则它们按主键排序。
例如,
book1:id = 1,bookTitle =hibernate搜索范例。
book2:id = 2,bookTitle =hibernate search在行动中
如果我正在查询查找术语hibernate search,我会得到这样的命令:book1然后book2
我想反转这个顺序:book2然后book1。
这意味着反转主键顺序。
有没有可能的方式来做到这一点,而不实施自定义相似性?同时保持相关性顺序。
是的,您需要创建一个 Sort
所需的排序,并将其设置在您的查询中。请参阅 Hibernate文档的第5.1.3.3节。然后,在 SortFields
传递 SortField.FIELD_SCORE
。
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);
列表结果= query.list();
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.
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();
这篇关于Hibernate搜索排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!