系列汇总:

lucene3.0_基础使用及注意事项汇总

IndexSearcher排序

本文主要讲解:

1.IndexSearcher中和排序相关的方法及sort类、SortField类(api级别);

2.按文档得分进行排序;

3.按文档内部id进行排序;

4.数值型、日期型排序注意事项;

5.多Field排序;

6.通过改变boost值来改变文档的得分。

----------------------------------------------------------------------

1.IndexSearcher中和排序相关的方法及sort类、SortField类(api级别);

用IndexSearcher直接排序一般使用方法

abstract  TopFieldDocs search(Weight weight, Filter filter, int n, Sort sort) 
          Expert: Low-level search implementation with arbitrary sorting.

该方法只需传入一个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.

通过这些类我们能很方便的完成检索结果的排序。

简单示例:

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.数值型、日期型排序注意事项;

假设莫一索引有五个文档,默认排序如下所示:

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 字段进行排序:

结果:

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 字段进行排序:

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 字段进行排序:

结果:

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排序;

...实例代码:

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);

结果:

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值来改变文档的得分。

默认排序(相关度排序),原始排序情况:

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值。

doc5.setBoost(5);

然后再看看排序情况:

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_IndexSearcher排序相关推荐

  1. 【转载保存】lucene3.0可以对docId、docField、queryParser设置Boost值来影响排序结果

    转自:http://catastiger.iteye.com/blog/803796 前提:不对结果做sort操作.     在搜索中,并不是所有的Document和Fields都是平等的.有些技术会 ...

  2. Lucene教程--维护索引、查询对象和相关度排序

    1 索引维护 1.1 添加索引 步骤: 1)创建存放索引的目录Directory 2)创建索引器配置管理类IndexWriterConfig 3)使用索引目录和配置管理类创建索引器 4)使用索引器将D ...

  3. Lucene3.5自学4--建索引相关知识总结

    Lucene简单介绍(该部分摘自网络) Lucene是一个高效的,基于Java的全文检索库. 所以在了解Lucene之前要费一番工夫了解一下全文检索. 那么什么叫做全文检索呢?这要从我们生活中的数据说 ...

  4. 数据库中自定义排序规则,Mysql中自定义字段排序规则,Oracle中自定义字段排序规则,decode函数的用法,field函数的用法

    数据库中自定义排序 场景:有一张banner表,表中有一个status字段,有0, 1, 2三个状态位,我想要 1,0,2的自定义排序(这里是重点),然后再进行之上对sequence字段进行二次排序( ...

  5. 伍六七带你学算法 进阶篇-排序算法

    给定一个整数数组 nums,将该数组升序排列. 示例 1: 输入:[5,2,3,1] 输出:[1,2,3,5] 示例 2: 输入:[5,1,1,2,0,0] 输出:[0,0,1,1,2,5] 各排序算 ...

  6. python中排序英文单词怎么写_Python实现对文件进行单词划分并去重排序操作示例...

    本文实例讲述了Python实现对文件进行单词划分并去重排序操作.,具体如下: 文件名:test1.txt 文件内容: But soft what light through yonder window ...

  7. Redis 笔记(07)— sorted set 类型(添加、删除有序集合元素、获取分数范围内成员、按score排序、返回集合元素个数)

    zset 可能是 Redis 提供的最为特色的数据结构,一方面它是一个 set,保证了内部 value 的唯一性,另一方面它可以给每个 value 赋予一个 score,代表这个 value 的排序权 ...

  8. Redis 高级特性(1)—— 事务 过期时间 排序

    1. Redis 高级特性 -- 事务 事务概念 Redis 中的事务 (transaction)是一组命令的集合.事务同命令一样是 Redis 的最小执行单位,一个事务中的命令要么都执行,要么都不执 ...

  9. 算法图解/二分查找/简单查找/选择排序/递归算法/快速排序算法/

    大 O 表示法 大 O 表示法在讨论运行时间时,log 指的都是 log2 大 O 表示法指出了算法有多快,让你能够比较操作数,它指出了算法运行时间的增速,而并非以秒为单位的速度. 大 O 表示法指出 ...

最新文章

  1. 不得不知的小程序基本知识
  2. hdu5015 矩阵快速幂233(好题)
  3. BZOJ 3224: Tyvj 1728 普通平衡树 treap
  4. 小麦盒子cdn_阿里云CDN入门使用配置
  5. 关于SharePoint部署Webpart的十个必读链接(downmoon)
  6. 习惯的力量之五让迟延见鬼去吧
  7. Android中用OpenGL ES Tracer分析绘制过程
  8. 日本的危机感:想战胜中美,要举全国之力培养AI人才
  9. asp.net + mysql
  10. linux ip命令dhcp,嵌入式linux通过DHCP自动获取IP地址实现获取
  11. Java之美[从菜鸟到高手演变]之JVM内存管理及垃圾回收
  12. java从键盘读入数据_关于Java中从键盘读入各种数据的方式
  13. Avatar Scaler
  14. onkeypress 、onkeyup 与onkeydown三者之间的区别
  15. 台式计算机网线插哪里,电脑主机网线插哪里?
  16. 扫盲!电影视频版本全方位解析(下载电影前必读)
  17. 模拟实现strcmp函数
  18. 自动控制原理 - 1 绪论内容
  19. ET大脑,打通产业升级智能之路
  20. “王思聪”才被取消又被限制,我的吐槽差评!吃瓜群众快过来

热门文章

  1. php 开发桌面应用,使用NW将开发的网站打包成桌面应用
  2. python做软件测试需要那些条件_做软件测试需要学什么
  3. raster | R语言中的空间栅格对象及其基本处理方法(Ⅳ):数据聚合、重采样
  4. Python 数据类型 布尔类型
  5. 性能测试和自动化测试选哪个?
  6. 女生适合当程序员吗?
  7. php 变量字节大小,PHP 变量
  8. props传递对象_vue-父组件传值props(对象)给子组件
  9. python输出文本内容_python如何输出文件内容
  10. mysql 导出所有表_Mysql导出(多张表)表结构及表数据 mysqldump用法