hibernate search中使用SortField

场景:使用hibernate search,先按某字段rank排序,然后按相关度即文档得分进行排序。
model:
@Indexed
@Entity
@Table(name="book")
public class Book{

@DocumentId(name="book_id")
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="book_id", nullable=false)
private int bookId;

@Field(name="rank",index=Index.UN_TOKENIZED,store=Store.YES)
@Column(name="rank")
private String rank;

...(省略get,set方法及其他属性)
}


SortField[] sortFields = new SortField[2];
sortFields[0] = new SortField(“rank",SortField.STRING,true);//参数true表示将排序的结果反转
sortFields[1] = new SortField(null,SortField.SCORE);
Sort sort = new Sort(sortFields);

注意几点:
1.想要先按name排序再按相关度排序的话,必须得加上store=Store.YES,否则无论如何也
达不到要求,不知是只在hibernate search中这样,还是单纯用lucene也是这样

2.若只设置了一个sortfield的话,即只有
new SortField(“name",SortField.STRING,true),则会先按这个字段排序,然后按document添加到索引中的顺序排列

3.new SortField()的第三个参数为true表示是natural order的逆顺序,默认为false《lucene in
action 2nd edition》中介绍:
The default sort direction for sort fields (including relevance and document ID) is natural
ordering. Natural order is descending for relevance but increasing for all other
fields
. The natural order can be reversed per Sort object by specifying true for the second
argument.

4.若Book类是嵌在其它类中时,注意在字段前加上"book.",即
sortFields[0] = new SortField(“book.name",SortField.STRING,true);

5.另参见http://lucene-group.group.iteye.com/group/topic/17748

你可能感兴趣的:(Hibernate,ITeye,Lucene)