public List<BrowseFacet> getFacets() {
    Map<String, BrowseFacet> facetMap;
    if (FacetSortSpec.OrderValueAsc.equals(_fspec.getOrderBy())) {
      facetMap = new TreeMap<String, BrowseFacet>();
    } else {
      facetMap = new HashMap<String, BrowseFacet>();
    }

    for (FacetAccessible facetAccessor : _list) {
      Iterator<BrowseFacet> iter = facetAccessor.getFacets().iterator();
      if (facetMap.size() == 0) {
        while (iter.hasNext()) {
          BrowseFacet facet = iter.next();
          facetMap.put(facet.getValue(), facet);
        }
      } else {
        while (iter.hasNext()) {
          BrowseFacet facet = iter.next();
          BrowseFacet existing = facetMap.get(facet.getValue());
          if (existing == null) {
            facetMap.put(facet.getValue(), facet);
          } else {
            existing.setHitCount(existing.getHitCount() + facet.getHitCount());
          }
        }
      }
    }

    int cnt = 0;
    int maxCnt = _fspec.getMaxCount();
    if (maxCnt <= 0) maxCnt = Integer.MAX_VALUE;
    int minHits = _fspec.getMinHitCount();
    List<BrowseFacet> list = new LinkedList<BrowseFacet>();

    if (FacetSortSpec.OrderValueAsc.equals(_fspec.getOrderBy())) {
      for (BrowseFacet facet : facetMap.values()) {
        if (facet.getHitCount() >= minHits) {
          list.add(facet);
          if (++cnt >= maxCnt) break;
        }
      }
    } else {
      Comparator<BrowseFacet> comparator;
      if (FacetSortSpec.OrderHitsDesc.equals(_fspec.getOrderBy())) {
        comparator =
            new Comparator<BrowseFacet>() {
              public int compare(BrowseFacet f1, BrowseFacet f2) {
                int val = f2.getHitCount() - f1.getHitCount();
                if (val == 0) {
                  val = (f1.getValue().compareTo(f2.getValue()));
                }
                return val;
              }
            };
      } else // FacetSortSpec.OrderByCustom.equals(_fspec.getOrderBy()
      {
        comparator = _fspec.getCustomComparatorFactory().newComparator();
      }
      ArrayList<BrowseFacet> facets = new ArrayList<BrowseFacet>(facetMap.values());
      Collections.sort(facets, comparator);
      for (BrowseFacet facet : facets) {
        if (facet.getHitCount() >= minHits) {
          list.add(facet);
          if (++cnt >= maxCnt) break;
        }
      }
    }
    return list;
  }
示例#2
0
  public static Map<String, FacetAccessible> mergeSimpleFacetContainers(
      Collection<Map<String, FacetAccessible>> subMaps, BrowseRequest req) {
    Map<String, Map<String, BrowseFacet>> counts = new HashMap<String, Map<String, BrowseFacet>>();
    for (Map<String, FacetAccessible> subMap : subMaps) {
      for (Map.Entry<String, FacetAccessible> entry : subMap.entrySet()) {
        Map<String, BrowseFacet> count = counts.get(entry.getKey());
        if (count == null) {
          count = new HashMap<String, BrowseFacet>();
          counts.put(entry.getKey(), count);
        }
        for (BrowseFacet facet : entry.getValue().getFacets()) {
          String val = facet.getValue();
          BrowseFacet oldValue = count.get(val);
          if (oldValue == null) {
            count.put(val, new BrowseFacet(val, facet.getHitCount()));
          } else {
            oldValue.setHitCount(oldValue.getHitCount() + facet.getHitCount());
          }
        }
      }
    }

    Map<String, FacetAccessible> mergedFacetMap = new HashMap<String, FacetAccessible>();
    for (String facet : counts.keySet()) {
      FacetSpec fs = req.getFacetSpec(facet);

      FacetSpec.FacetSortSpec sortSpec = fs.getOrderBy();

      Comparator<BrowseFacet> comparator;
      if (FacetSortSpec.OrderValueAsc.equals(sortSpec)) comparator = FACET_VAL_COMPARATOR;
      else if (FacetSortSpec.OrderHitsDesc.equals(sortSpec))
        comparator = FacetHitcountComparatorFactory.FACET_HITS_COMPARATOR;
      else comparator = fs.getCustomComparatorFactory().newComparator();

      Map<String, BrowseFacet> facetValueCounts = counts.get(facet);
      BrowseFacet[] facetArray =
          facetValueCounts.values().toArray(new BrowseFacet[facetValueCounts.size()]);
      Arrays.sort(facetArray, comparator);

      int numToShow = facetArray.length;
      if (req != null) {
        FacetSpec fspec = req.getFacetSpec(facet);
        if (fspec != null) {
          int maxCount = fspec.getMaxCount();
          if (maxCount > 0) {
            numToShow = Math.min(maxCount, numToShow);
          }
        }
      }

      BrowseFacet[] facets;
      if (numToShow == facetArray.length) {
        facets = facetArray;
      } else {
        facets = new BrowseFacet[numToShow];
        System.arraycopy(facetArray, 0, facets, 0, numToShow);
      }

      MappedFacetAccessible mergedFacetAccessible = new MappedFacetAccessible(facets);
      mergedFacetMap.put(facet, mergedFacetAccessible);
    }
    return mergedFacetMap;
  }