`

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
分享到:
评论

相关推荐

    【分享:lucene学习资料】---<下载不扣分,回帖加1分,欢迎下载,童叟无欺>

    5.3.5. 改变SortField中的Locale信息 15 6. 过滤器 16 7. 分析器Analysis 16 7.1. 自带分析器和过滤器 16 7.2. 第三方过分析器 17 7.2.1. JE分词用法 17 8. 索引的合并 18 9. 各种Query 18 9.1. 概述 18 9.2. 使用...

    sortable:流星整合的Sortable

    由集合支持的带有React性可重列表: 到达集合中的新元素将按您期望的那样更新列表 从集合中删除的元素将从列表中... {{ #sortable items =&lt; collection&gt; sortField = " order " }} 服务器: Sortable . collections

    selectize-angular:用于selectize.js的AngularJS指令

    使用属性items来指定可用选项使用selectize的选项指定要用作标签和值的模型属性 例子 &lt;input type="text" selection="myModel" selectize="{valueField: 'id', labelField: 'name', sortField: 'name', ...

    基于 PHP 实现的数组转换成树状结构的帮助类

    $TreeBuild-&gt;sortField = 'sort'; //排序字段 $TreeBuild-&gt;topId='0'; //设定顶级id的值 默认为0 $TreeBuild-&gt;id='id'; //设定id 字段名 $TreeBuild-&gt;pid='pid'; //设定父id 字段名 $TreeBuild-&gt;childrens ='...

    tz

    -AuthorDto,pageNo,pageSize,sortField,sortDirection =&gt; CollectionDTO ОPOST / authors / {id} / update &lt;-id,UpdateAuthorDto =&gt; AuthorDto或NotFoundException УдалениеPOST / authors / {id} ...

    Selectize.js-Plugins

    selectize.js 的插件为 selectize.js 创建的一些插件option_selected_remove 此插件将在...selected_on_top 此插件将动态地将选定的选项放在顶部注意:要使其工作,您必须设置sortField: '$order'而没有任何其他字段。

    AWS市场

    @connection ( name : " MarketProducts " , sortField : " createdAt " ) tags : [ String ] owner : String ! createdAt : String } type Product @model @auth ( rules : [{ allow : owner , identityField :...

Global site tag (gtag.js) - Google Analytics