皮杜妮

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  887 Posts :: 3 Stories :: 7 Comments :: 0 Trackbacks
lucene查询结果需要先分组后排序, 看了相关文档,lucene提供了分组 排序功能,但是比较消耗内容,实际情况是 我们一般只需要前面的1000条, 所以我自己实现了排序算法,尽量少占用内容。
方法如下:


// 分组统计方式
    /**
     *
     * @param topnumber  取分组后的前几条数据
     * @return
     */
    public static List cachegroup(int topnumber) {
        try {
            int page = 30000;
            String filePath = "D://lucene/index2";
            File dirfile = new File(filePath);
            Directory dir = FSDirectory.open(dirfile);
            IndexReader reader = DirectoryReader.open(dir);
            IndexSearcher indexSearcher = new IndexSearcher(reader);
            String searchTerm = "aaa";
            boolean getScores = true;
            boolean getMaxScores = true;
            boolean fillFields = true;
            GroupingSearch groupingSearch = new GroupingSearch("author");
            groupingSearch.setGroupSort(Sort.RELEVANCE);
            groupingSearch.setFillSortFields(true);
            groupingSearch.setCachingInMB(40.0, true);
            groupingSearch.setAllGroups(true);
            TermQuery query = new TermQuery(new Term("content", searchTerm));
            TopDocs hits = indexSearcher.search(query, 100);
            int count = hits.totalHits;
            int mode = count % page;
            int times = count / page;
            if (mode != 0)
                times = times + 1;
            List indexvalue = new LinkedList();
            for (int i = 0; i < times; i++) {
                int groupoffset = i * page;
                TopGroups<BytesRef> result = groupingSearch.search(
                        indexSearcher, query, groupoffset, page);
                GroupDocs<BytesRef>[] gds = result.groups;
                for (GroupDocs<BytesRef> gd : gds) {
                    indexvalue.add(new FieldSort(gd.totalHits, gd.groupValue
                            .utf8ToString()));
                }
                Comparator comp = Collections.reverseOrder();
                Collections.sort(indexvalue, comp);
                indexvalue = indexvalue.subList(0, topnumber);
            }
            return indexvalue;
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    
    public static void  readList(List list)
    {
        Iterator it=list.iterator();
        while(it.hasNext())
        {
            FieldSort fieldsort=(FieldSort)it.next();
            System.out.println(fieldsort.getFieldvalue()+"/"+fieldsort.getFieldname());
        }
    }





package speed;

import java.util.Comparator;



public class FieldSort implements Comparable<FieldSort>{

    private int    fieldvalue;
    private String fieldname;
    public FieldSort() {
        super();
    }
    public FieldSort(int fieldvalue,String fieldname) {
        super();
        this.setFieldvalue(fieldvalue);
        this.setFieldname(fieldname);
    }

    public int getFieldvalue() {
        return fieldvalue;
    }
    public void setFieldvalue(int fieldvalue) {
        this.fieldvalue = fieldvalue;
    }
    public String getFieldname() {
        return fieldname;
    }
    public void setFieldname(String fieldname) {
        this.fieldname = fieldname;
    }
    @Override
    public String toString(){
        return fieldname+"/"+fieldvalue;
    }
    public int compareTo(FieldSort t){
        if(t==null)return 0;
        else{
            return this.fieldvalue-t.fieldvalue;
        }
    }

    

}
posted on 2013-09-24 16:34 猪眼睛 阅读(620) 评论(0)  编辑  收藏 所属分类: 搜索引擎