jeecms搜索结果排序-二次开发
jeecms搜索用的是apache Lucene,要实现此功能得先去学习它。
直接上代码
package com.jeecms.cms.lucene; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date; import org.apache.lucene.index.IndexReader;
import org.apache.lucene.search.FieldCache;
import org.apache.lucene.search.FieldComparator;
import org.apache.lucene.search.FieldComparatorSource; public class LuceneContentComparatorSource extends FieldComparatorSource { private int lmId = 0;
public LuceneContentComparatorSource() {
}
public LuceneContentComparatorSource(int lmId) {
this.lmId = lmId;
}
@Override
public FieldComparator newComparator(String field, int allCounts, int newCount,
boolean reversed) throws IOException {
return new LuceneContentComparatorSource.LuceneContentFieldComparator(allCounts, field);
}
class LuceneContentFieldComparator extends FieldComparator{
private String[] values;
private String[] currentReaderValues;
private final String field;
private String bottom;
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
public LuceneContentFieldComparator(int numHits, String field) {
this.values = new String[numHits];
this.field = field;
}
@Override
public int compare(int slot1, int slot2) {
try {
if("releaseDate".equals(field)){
Date val1 = format.parse(this.values[slot1]);
Date val2 = format.parse(this.values[slot2]);
if(null == val1) {
if(null == val2) {
return 0;
}
return -1;
}
if(null == val2) {
return 1;
} if(val1.after(val2)) {
return 1;
}
if(val2.after(val1)) {
return -1;
}
}else if("channelIdArray".equals(field)){
if(this.values[slot1].equals(this.values[slot2])){
return 0;
}else if((lmId + "").equals(this.values[slot1])){
return 1;
}else if((lmId + "").equals(this.values[slot2])){
return -1;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public int compareBottom(int doc) throws IOException {
try {
if("releaseDate".equals(field)){
Date val2 = format.parse(this.currentReaderValues[doc]);
Date tempBottom = format.parse(this.bottom);
if (tempBottom == null) {
if (val2 == null) {
return 0;
}
return -1;
}
if (val2 == null) {
return 1;
}
if(val2.after(tempBottom)) {
return 1;
}
if(tempBottom.after(val2)) {
return -1;
}
}else if("channelIdArray".equals(field)){
if(this.bottom.equals(this.currentReaderValues[doc])){
return 0;
}else if((lmId + "").equals(this.bottom)){
return 1;
}else if((lmId + "").equals(this.currentReaderValues[doc])){
return -1;
}
}
} catch(Exception e) {
e.printStackTrace();
}
return 0;
}
@Override
public void copy(int slot, int doc) throws IOException {
this.values[slot] = this.currentReaderValues[doc]; }
@Override
public void setBottom(int bottom) {
this.bottom = this.values[bottom];
}
@Override
public void setNextReader(IndexReader reader, int arg1)
throws IOException {
this.currentReaderValues = FieldCache.DEFAULT.getStrings(reader, this.field);
}
@Override
public Comparable value(int slot) {
return this.values[slot];
} } }
我的需求是需呀根据栏目ID 与日期排序 所以以上代码就如此写的
至此 基本已经完成,之后只需要在 search方法中 传入 sort对象即可
//栏目排序类
SortField sortFieldLm = new SortField("channelIdArray", new LuceneContentComparatorSource(lmId), true);
//日期排序
SortField sortFieldDate = new SortField("releaseDate", new LuceneContentComparatorSource(), true);
Sort sort = new Sort ();
sort.setSort(sortFieldLm,sortFieldDate);
//排序类
TopDocs docs = searcher.search(query,null, pageNo * pageSize,sort);
jeecms搜索结果排序-二次开发的更多相关文章
- 原 JEECMS导入IDEA进行二次开发图文教程
JEECMS导入IDEA进行二次开发图文教程 2017年05月15日 17:03:53 Swain_Ho 阅读数 3257 版权声明:本文为博主原创文章,未经博主允许不得转载. https:// ...
- 【Yom框架】漫谈个人框架的设计之二:新的IRepository接口+搜索和排序解耦(+基于Castle实现)
经过了上篇IRepository和IRepository<T>的讨论[文章地址为:http://www.cnblogs.com/yomho/p/3296759.html] 我选择了IRep ...
- jeecms系统使用介绍——通过二次开发实现对word、pdf、txt等上传附件的全文检索
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/76912307 本文出自[我是干勾鱼的博客] 之前在文章<基于Java的门户 ...
- 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单、书签等相关问题
2015-03-02 更新文章,由于需求修改,更改了flexpaper插件,故增加第9.10.11小节,下载代码时请注意. 先废话几句.最近用到文档在线浏览功能,之前用的是print2flash(一个 ...
- FlexPaper二次开发问题及搜索高亮显示
原文:FlexPaper二次开发问题及搜索高亮显示 最近有个需求,做一个IT知识库,类似于文库,说到文库肯定会用到在线浏览文档了,所有在网上翻阅了一下类似豆丁的在线浏览器插件的资料,将其进行了二次开发 ...
- 【IOS开发】搜索和排序(好友列表,通讯录的实现,searchbar)
一.效果图: 二.概述 实现一个好友列表,可以分为男女两个选项,并且实现搜索和排序功能.我的数据是放在plist文件中. 三.代码简述 代码结构如图,首先自定义一个Cell. cell.h #impo ...
- Java实现 LeetCode 81 搜索旋转排序数组 II(二)
81. 搜索旋转排序数组 II 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判 ...
- #研发解决方案介绍#基于ES的搜索+筛选+排序解决方案
郑昀 基于胡耀华和王超的设计文档 最后更新于2014/12/3 关键词:ElasticSearch.Lucene.solr.搜索.facet.高可用.可伸缩.mongodb.SearchHub.商品中 ...
- Android VLC播放器二次开发1——程序结构分析
最近因为一个新项目需要一个多媒体播放器,所以需要做个视频.音频.图片方面的播放器.也查阅了不少这方面的资料,如果要从头做一个播放器工作量太大了,而且难度也很大.所以最后选择了VLC作为基础,进行二次开 ...
随机推荐
- nodejs微服务
近来公司增加了nodejs微服务 它的主要任务是接收来自于现场的采集数据:作业记录和流转记录,动态构建一个基地的全景实时数据 暂时不涉及数据库. 如果要进行数据库操作,不建议使用本模块, ...
- bmi健康指数
#!/usr/bin/python # -*- coding: utf-8 -*- #小明身高1.75,体重80.5kg.请根据BMI公式(体重除以身高的平方) #帮小明计算他的BMI指数,并根据BM ...
- [Luogu1429]平面最近点对(加强版)
题目大意: 平面最近点对. 思路: 分治. 首先将所有点排序 每次把当前区间分为两半,递归求解两个区间内部的情况,然后枚举区间两边的点. #include<cmath> #include& ...
- JSP(待更新)
1.概念: 所谓JSP就是在HTML中嵌入大量的java代码而已.
- VisualStudio Shell简介 — 集成插件
Visual Studio Shell只是提供了一个内核,我们还需要在其基础上补充功能,从而实现我们自己的IDE.Visual Studio Shell的插件开发和Visual Studio插件开发是 ...
- What is Mocking?
Mocking is primarily used in unit testing. An object under test may have dependencies on other (comp ...
- android intent 跳转
转自:http://blog.sina.com.cn/s/blog_7309444701014u2d.html 一.不需要返回值的跳转 Intent intent=new Intent(); inte ...
- 利用Yum彻底移除docker
yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-la ...
- ElasticSearch refresh API
在 Elasticsearch 中,写入和打开一个新段的轻量的过程叫做 refresh . 默认情况下每个分片会每秒自动刷新一次.这就是为什么我们说 Elasticsearch 是 近 实时搜索: 文 ...
- cmake 生成VS项目文件夹
SET_PROPERTY(GLOBAL PROPERTY USE_FOLDERS ON) SET_TARGET_PROPERTIES(test_tcp_client test_tcp_server P ...