皮杜妮

            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 豬眼睛 閱讀(637) 評論(0)  編輯  收藏 所屬分類: 搜索引擎
          主站蜘蛛池模板: 宽甸| 芦溪县| 江口县| 嵊泗县| 永嘉县| 乌兰县| 金川县| 双鸭山市| 彰化市| 桂阳县| 清苑县| 崇信县| 巴林左旗| 松桃| 苏尼特右旗| 通州区| 卢氏县| 咸宁市| 罗甸县| 察哈| 稷山县| 肥城市| 四川省| 福州市| 屯留县| 陆良县| 金寨县| 祁门县| 通化县| 紫云| 昭觉县| 沁源县| 黄大仙区| 永靖县| 平江县| 深泽县| 砚山县| 浙江省| 威海市| 攀枝花市| 正镶白旗|