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

 

转载于:https://www.cnblogs.com/lovezhaolei/p/4213590.html

  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 扫一扫,分享海报

参与评论 您还未登录,请先 登录 后发表或查看评论
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值