IndexSearcher排序
本文主要讲解:
1.IndexSearcher中和排序相关的方法及sort类、SortField类(api级别);
2.按文档得分进行排序;
3.按文档内部id进行排序;
4.数值型、日期型排序注意事项;
5.多Field排序;
6.通过改变boost值来改变文档的得分。
----------------------------------------------------------------------
1.IndexSearcher中和排序相关的方法及sort类、SortField类(api级别);
用IndexSearcher直接排序一般使用方法
该方法只需传入一个sort实例。
Constructor Summary
Sort() Sorts by computed relevance. |
Sort(SortField... fields) Sorts in succession by the criteria in each SortField. |
Sort(SortField field) Sorts by the criteria in the given SortField. |
在sort实例中,决定对哪个字段进行排序,按照什么数据类型排序,是升序还是降序,由SortField说的算。
两个最基础的构造方法如下:
SortField(String field, int type) Creates a sort by terms in the given field with the type of term values explicitly given. |
SortField(String field, int type, boolean reverse) Creates a sort, possibly in reverse, by terms in the given field with the type of term values explicitly given. |
通过这些类我们能很方便的完成检索结果的排序。
简单示例:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->SortField sortF = new SortField("f", SortField.INT);
Sort sort = new Sort( sortF);
TopFieldDocs docs = searcher.search(query, null, 10, sort);
//遍历docs中的结果
2.按文档得分进行排序;
IndexSearcher默认的搜索就是按照文档得分进行排序的。
在SortField中将类型设置为SCORE即可。
static int |
SCORE Sort by document score (relevancy). |
3.按文档内部id进行排序;
每个文档进入索引的时候都会分配一个id号,有时可能会需要按照这个id号进行排序,
那么将SortField中类型设置为DOC即可。
static int |
DOC Sort by document number (index order). |
4.数值型、日期型排序注意事项;
假设莫一索引有五个文档,默认排序如下所示:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Document<stored,indexed<f:10> stored,indexed<f1:20100215> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20090512> stored,indexed<a:fox>>
Document<stored,indexed<f:5> stored,indexed<f1:20101019> stored,indexed<a:fox>>
Document<stored,indexed<f:-2> stored,indexed<f1:20000128> stored,indexed<a:fox>>
Document<stored,indexed<f:0> stored,indexed<f1:20050719> stored,indexed<a:fox>>
注意蓝色标识出来的字段是一个int型数据,红色标识出来的字段是一个8位的日期数据。默认排序中他是无序的。
使用INT类型对 f 字段进行排序:
结果:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Document<stored,indexed<f:-2> stored,indexed<f1:20000128> stored,indexed<a:fox>>
Document<stored,indexed<f:0> stored,indexed<f1:20050719> stored,indexed<a:fox>>
Document<stored,indexed<f:5> stored,indexed<f1:20101019> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20100215> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20090512> stored,indexed<a:fox>>
符合预期结果。
使用STRING类型对 f 字段进行排序:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Document<stored,indexed<f:-2> stored,indexed<f1:20000128> stored,indexed<a:fox>>
Document<stored,indexed<f:0> stored,indexed<f1:20050719> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20100215> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20090512> stored,indexed<a:fox>>
Document<stored,indexed<f:5> stored,indexed<f1:20101019> stored,indexed<a:fox>>
第五条数据排序发生异常,不符合预期结果。
因此排序时要特别注意类型的选择。
使用INT类型对 f1 字段进行排序:
结果:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Document<stored,indexed<f:-2> stored,indexed<f1:20000128> stored,indexed<a:fox>>
Document<stored,indexed<f:0> stored,indexed<f1:20050719> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20090512> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20100215> stored,indexed<a:fox>>
Document<stored,indexed<f:5> stored,indexed<f1:20101019> stored,indexed<a:fox>>
符合预期结果。
注意点:
对日期、价格等数据排序都要选择合适的排序类型,不单单是满足业务的需要,而且用INT、FLOAT等数值型的排序
比STRING效率要高。
5.多Field排序;
...实例代码:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->SortField sortF = new SortField("f", SortField.INT);
SortField sortF2 = new SortField("f1", SortField.INT);
Sort sort = new Sort(new SortField[]{sortF , sortF2});
TopFieldDocs docs = searcher.search(query, null, 10, sort);
结果:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Document<stored,indexed<f:-2> stored,indexed<f1:20000128> stored,indexed<a:fox>>
Document<stored,indexed<f:0> stored,indexed<f1:20050719> stored,indexed<a:fox>>
Document<stored,indexed<f:5> stored,indexed<f1:20101019> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20090512> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20100215> stored,indexed<a:fox>>
注意点:
先按照 f字段 进行排序,如果 f字段 值相等,再按照 f1字段 进行排序。
这个顺序由 SortField数组中 SortField实例的顺序 一致。
6.通过改变boost值来改变文档的得分。
默认排序(相关度排序),原始排序情况:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Document<stored,indexed<f:10> stored,indexed<f1:20100215> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20090512> stored,indexed<a:fox>>
Document<stored,indexed<f:5> stored,indexed<f1:20101019> stored,indexed<a:fox>>
Document<stored,indexed<f:-2> stored,indexed<f1:20000128> stored,indexed<a:fox>>
Document<stored,indexed<f:0> stored,indexed<f1:20050719> stored,indexed<a:fox>>
修改第5个文档的boost值。
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->doc5.setBoost(5);
然后再看看排序情况:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->Document<stored,indexed<f:0> stored,indexed<f1:20050719> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20100215> stored,indexed<a:fox>>
Document<stored,indexed<f:10> stored,indexed<f1:20090512> stored,indexed<a:fox>>
Document<stored,indexed<f:5> stored,indexed<f1:20101019> stored,indexed<a:fox>>
Document<stored,indexed<f:-2> stored,indexed<f1:20000128> stored,indexed<a:fox>>
可以看到 从地到天 了!
这个功能的商用价值很大,只能这么说...
分享到:
相关推荐
Lucene3.0_使__用_教_程.doc
Lucene_3.0_原理与代码分析
Lucene3.0原理与代码分析完整版.本系列文章将详细描述几乎最新版本的Lucene的基本原理和代码分析
lucene3.0 lucene3.0 lucene3.0 lucene3.0 lucene3.0
好东西,思路清晰,不错的资源,希望能帮到你.....
Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 Lucene3.0 使 用 教 程 ...
lucene 3.0 API中文帮助,学习的人懂得的
Lucene3.0特性Lucene3.0特性
Lucene3.0之查询处理(1):原理和查询类型 各种Query对象详解
传统上,人们将信息检索系统返回结果的排序称为"相关排序" (relevance ranking) ,隐含其中各条目的顺序反映结果和查询的相关程度。
Lucene 3.0 原理与代码分析完整版
lucene3.0 中文分词器, 庖丁解牛
lucene3.0 实例,在jdk1.5,lucene3.0下调式通过,可以直接运行。先运行生成索引文件的class,在运行搜索的class。
lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件,lucene3.0的核心jar包文件。
传智播客Lucene3.0课程,Lucene3.0的入门教程.
lucene3.0-highlighter.jar lucene3.0的高亮jar包,从lucene3.0源码中导出来的
lucene升级了,分词也得升级哦! 在使用lucene3与paoding集成的时候可能会出现以下错误: Exception in thread "main" java.lang.AbstractMethodError: org.apache.lucene.analysis.TokenStream.incrementToken()Z ...
Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理 Lucene 3.0 原理
全面好用的lucene 2.0 api以及lucene 3.0 api帮助文档