SenseiTermFilter extends SenseiFilter { static String[] getValsByFrequency(String[] vals, int[] freqs, int maxDoc, DocIdSetCardinality total, TermValueList valArray, List<String> optimizedOut, boolean isAnd) { List<Pair<String, DocIdSetCardinality>> valsAndFreqs = new ArrayList<Pair<String, DocIdSetCardinality>>(vals.length); for (String val : vals) { int i = valArray.indexOf(val); if (i >=0) { DocIdSetCardinality docIdSetCardinality = DocIdSetCardinality.exact(((double) freqs[i]) / (maxDoc + 1)); if (isAnd) { if (docIdSetCardinality.isOne()) { optimizedOut.add(val); continue; } total.andWith(docIdSetCardinality); } else { if (docIdSetCardinality.isZero()) { optimizedOut.add(val); continue; } total.orWith(docIdSetCardinality); } valsAndFreqs.add(new Pair<String, DocIdSetCardinality>(valArray.get(i), docIdSetCardinality)); } } Collections.sort(valsAndFreqs, isAnd ? INCREASING_CARDINALITY_COMPARATOR : DECREASING_CARDINALITY_COMPARATOR); String[] sortedVals = new String[valsAndFreqs.size()]; int i = 0; while (i < sortedVals.length) { sortedVals[i] = valsAndFreqs.get(i).getFirst(); ++i; } return sortedVals; } SenseiTermFilter(String name,String vals[],String[] not,boolean isAnd,boolean noAutoOptimize); String planString(String type, String[] vals, String[] nots, List<String> optimizedVals, List<String> optimizedNots); @Override SenseiDocIdSet getSenseiDocIdSet(IndexReader reader); static final Comparator<Pair<String, DocIdSetCardinality>> INCREASING_CARDINALITY_COMPARATOR; }
@Test public void testGetValsByFrequency() { List<String> dummy = new ArrayList<String>(); DocIdSetCardinality andCardinality = DocIdSetCardinality.one(); String andVals[] = SenseiTermFilter.getValsByFrequency(vals, freqs, 26, andCardinality, dictionary, dummy, true); assertArrayEquals(andVals, new String[]{"e", "c", "a"}); DocSetAssertions.assertRange(18, 22, 26, andCardinality); DocIdSetCardinality orCardinality = DocIdSetCardinality.zero(); String orgVals[] = SenseiTermFilter.getValsByFrequency(vals, freqs, 26, orCardinality, dictionary, dummy, false); assertArrayEquals(orgVals, new String[]{"a", "c", "e"}); DocSetAssertions.assertRange(26, 26, 26, orCardinality); }