2013-07-08期-范围查询及结果排序

调整了SearchAction.java代码

    public String searcheContent() throws Exception {  
        searcheResult =  new ArrayList<ContentObject>();
        try{
            //当查询条件不存在时,查询数据库
            if(StringUtils.isEmpty(keyWord)){
                searcheResult = contentService.getContentList();
                return SUCCESS;
            }
            Version v = Version.LUCENE_43;
            //取得查询对象
            IndexReader[] readers =  SearchObject.getInstance().getSearcherReads();
            MultiReader mReaders = new MultiReader(readers);
            IndexSearcher indexSearch = new  IndexSearcher(mReaders);
            Analyzer analyzer =new StandardAnalyzer(v);
            //创建boolean查询
            BooleanQuery query = new BooleanQuery();
            String[] field = { "title", "content"};
            BooleanClause.Occur[] flags = new BooleanClause.Occur[2];
            flags[0] = BooleanClause.Occur.SHOULD;
            flags[1] = BooleanClause.Occur.SHOULD;
            Query query1 = MultiFieldQueryParser.parse(v, QueryParser.escape(keyWord), field, flags, analyzer);
            //query.add(query1, Occur.MUST);

            //日期范围查询
            Calendar calendar = Calendar.getInstance();
            //查询两个月以内的数据
            calendar.add(Calendar.DAY_OF_MONTH, -60);
            Date lastMonth = calendar.getTime();
            query.add(NumericRangeQuery.newLongRange("createDate", lastMonth.getTime(), new Date().getTime(), true, true), Occur.MUST);
            //根据ID范围查询
            //query.add(NumericRangeQuery.newIntRange("id", 0, 20, true, true), Occur.MUST);
            //true为降序,false为升序
            SortField field1 = new SortField("createDate", SortField.Type.LONG,true); 
            Sort sort = new Sort(field1);
            //取得查询结果
            TopDocs topDocs =   indexSearch.search(query,100000, sort);  

            //TopScoreDocCollector topCollector = TopScoreDocCollector.create(10000, true,sort);
           // indexSearch.search(query, topCollector); 
           // TopDocs topDocs = topCollector.topDocs(); 
            int resultCount=topDocs.totalHits;
            for(int i=0;i<resultCount;i++){
                Document doc = indexSearch.doc(topDocs.scoreDocs[i].doc);
                //转换Document对象为内容对象
                //ContentObject content = PackContentObject.convertDocToContent(doc);
                ContentObject content = PackContentObject.convertDocToContent(doc, query1, analyzer);
                //加入到结果列表 返回给前台页面获取
                searcheResult.add(content);
            }
        }catch(Exception e){
            e.printStackTrace();
        }

       return SUCCESS;  
    }

降序结果页面

升序结果页面

你可能感兴趣的:(排序)