zoukankan      html  css  js  c++  java
  • 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);
  • 相关阅读:
    使用bink播放视频
    Vertex Shader And Pixel Shader
    Points Versus Pixels in Cocos2D
    ogre_机器人在10个位置循环走动
    简单播放声音PlaySound
    Vertex Shader 入门代码
    加载地形的类
    Animation Cache in Cocos2diphone v2.0.0
    [转]Rad Video Tools使用手册
    CSound类源文件
  • 原文地址:https://www.cnblogs.com/lovezhaolei/p/4213590.html
Copyright © 2011-2022 走看看