系列汇总:

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. 利用Boost影响Lucene查询结果的排序

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

  2. lucene查询排序结果原理总结

    参考文章 Lucene3.0结果排序原理+操作+示例 Lucene的排序算法 一句话总结lucene排序算法是什么样的 关键几个概念 参考文档: http://lucene.apache.org/co ...

  3. 【lucene系列学习】排序

    用lucene3实现搜索多字段并排序功能(设置权重)    

  4. javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈

    Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...

  5. iOS可视化动态绘制八种排序过程

    前面几篇博客都是关于排序的,在之前陆陆续续发布的博客中,我们先后介绍了冒泡排序.选择排序.插入排序.希尔排序.堆排序.归并排序以及快速排序.俗话说的好,做事儿要善始善终,本篇博客就算是对之前那几篇博客 ...

  6. JavaScript实现常用的排序算法

    ▓▓▓▓▓▓ 大致介绍 由于最近要考试复习,所以学习js的时间少了 -_-||,考试完还会继续的努力学习,这次用原生的JavaScript实现以前学习的常用的排序算法,有冒泡排序.快速排序.直接插入排 ...

  7. [C#][算法] 用菜鸟的思维学习算法 -- 马桶排序、冒泡排序和快速排序

    用菜鸟的思维学习算法 -- 马桶排序.冒泡排序和快速排序 [博主]反骨仔 [来源]http://www.cnblogs.com/liqingwen/p/4994261.html  目录 马桶排序(令人 ...

  8. 算法与数据结构(十三) 冒泡排序、插入排序、希尔排序、选择排序(Swift3.0版)

    本篇博客中的代码实现依然采用Swift3.0来实现.在前几篇博客连续的介绍了关于查找的相关内容, 大约包括线性数据结构的顺序查找.折半查找.插值查找.Fibonacci查找,还包括数结构的二叉排序树以 ...

  9. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

随机推荐

  1. docker-compose搭建单机多节点es + kibana

    docker-compose.yml配置如下: version: '2.2' services: elasticsearch: image: docker.elastic.co/elasticsear ...

  2. UNITY地图寻路及服务器解决方案

    多边形寻路算法简单介绍 http://liweizhaolili.lofter.com/post/1cc70144_86a939e UNITY3D MMO服务器寻路 http://www.pathen ...

  3. ios Https问题

    HTTPS认证过程:   ① 浏览器发送一个连接请求给安全服务器.   ② 服务器将自己的证书,以及同证书相关的信息发送给客户浏览器.   ③ 客户浏览器检查服务器送过来的证书是否是由自己信赖的 CA ...

  4. 博客搬家到blog.wu8685.com

    博客园算是我最开始来的地方了吧,当时还在学校,为了找工作会看一些理论方面的东西,所以写的都是偏理论的心得. 后来参加了工作,开始忙起来,也就没有时间来更新了.其实忙都是借口,这点还是需要反思的. 大概 ...

  5. Java 设计模式系列(二十)状态模式

    Java 设计模式系列(二十)状态模式 状态模式,又称状态对象模式(Pattern of Objects for States),状态模式是对象的行为模式.状态模式允许一个对象在其内部状态改变的时候改 ...

  6. RocketMQ:Cannot allocate memory

    使用Storm本地模式消费RocketMQ数据的时候, 消费一点数据之后,就会出现如下错误: Java HotSpot(TM) 64-Bit Server VM warning: INFO: os:: ...

  7. K8S中RC与Deployment的区别

    原文:http://fx114.net/qa-81-152379.aspx replication controller与deployment的区别 replication controller Re ...

  8. Linux设备驱动模型底层架构及组织方式

    1.什么是设备驱动模型? 设备驱动模型,说实话这个概念真的不好解释,他是一个比较抽象的概念,我在网上也是没有找到关于设备驱动模型的一个定义,那么今天就我所学.所了解 到的,我对设备驱动模型的一个理解: ...

  9. Echarts.js使用

    <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>EC ...

  10. delphi跨平台SOCKET--System.Net.Socket

    delphi跨平台SOCKET--System.Net.Socket 不知始于DELPHI哪一个版本,姑且始于柏林版吧. 基于此单元的TSocket类,大家可以很方便地封装出自己的服务端和客户端的SO ...