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作为基础,进行二次开 ...
随机推荐
- NOIP2016_day1_No1玩具谜题
#include <iostream> #include <cstdio> using namespace std; int main () { freopen("t ...
- 如何正确使用const(常量),define(宏)
前言 在开发中,也许我们会经常使用到宏定义,或者用const修饰一些数据类型,经常有开发者不知道怎么正确使用,导致项目中乱用宏定义与const修饰符.本篇主要介绍在开发中怎么正确使用const与def ...
- [BZOJ2142]礼物(扩展Lucas)
2142: 礼物 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2286 Solved: 1009[Submit][Status][Discuss] ...
- exports 与 module.exports 的区别
exports与module.exports的作用就是将方法或者是变量暴露出去,以便给其他模块调用,再直接点,就是给其他模块通过require()的方式引用. 那么require()一个模块时,到底做 ...
- ife2015-task2-4-5
task2-4.html<!DOCTYPE html><html><head lang="en"> <meta charset=" ...
- Vue表单和组件
一.表单 v-model 指令在表单控件元素上创建双向数据绑定,v-model 会根据控件类型自动选取正确的方法来更新元素. <input v-model="message" ...
- ES集群爆红,有未分配的片
curl GET http://192.168.46.166:9200/_cluster/health?level=indices curl -XPUT '192.168.46.166:9200/_c ...
- 【转】docker安装PostgreSQL
原文:https://blog.csdn.net/liuyueyi1995/article/details/61204205 你先确认这两件事:1)执行 docker exec -it dream.p ...
- python实现大文件分割与合并
小U盘传大电影时可以免去用winrar分割文件时的压缩和解压缩过程. file.py import sys from os.path import exists fileCount = 0 def s ...
- go语言25个关键字总结
var和const :变量和常量的声明var varName type 或者 varName : = valuepackage and import: 导入func: 用于定义函数和方法return ...