首页 > 代码库 > jeecms搜索结果排序-二次开发
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搜索结果排序-二次开发
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。