|
| 1 | +package org.apache.solr.search.facet; |
| 2 | + |
| 3 | +import org.apache.lucene.queries.function.ValueSource; |
| 4 | +import org.apache.solr.common.util.SimpleOrderedMap; |
| 5 | +import org.apache.solr.search.FunctionQParser; |
| 6 | +import org.apache.solr.search.SyntaxError; |
| 7 | +import org.apache.solr.search.ValueSourceParser; |
| 8 | + |
| 9 | +/** |
| 10 | + * Calculates the frequency of ordinal values using Roaring Bitmaps. |
| 11 | + * |
| 12 | + * The response is a map with the following fields: |
| 13 | + * - bitmaps: an array of bitmaps, where the frequency of a value x is given by the sum of {@code 2^i} for all values |
| 14 | + * of {@code i} where {@code bitmaps[i].contains(x)} |
| 15 | + * - overflow: a bitmap of ordinal values with {@code frequency >= 2^(bitmaps.length)} |
| 16 | + * |
| 17 | + * Lacking a coherent definition of magnitude other than the raw count, this aggregate cannot be used for sorting. |
| 18 | + */ |
| 19 | +public class BitmapFrequencyAgg extends SimpleAggValueSource { |
| 20 | + private final int size; |
| 21 | + |
| 22 | + public BitmapFrequencyAgg(ValueSource vs, int size) { |
| 23 | + super("bitmapfreq", vs); |
| 24 | + |
| 25 | + this.size = size; |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public SlotAcc createSlotAcc(FacetContext fcontext, int numDocs, int numSlots) { |
| 30 | + return new BitmapFrequencySlotAcc(getArg(), fcontext, numSlots, size); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public FacetMerger createFacetMerger(Object prototype) { |
| 35 | + return new Merger(size); |
| 36 | + } |
| 37 | + |
| 38 | + public static class Parser extends ValueSourceParser { |
| 39 | + @Override |
| 40 | + public ValueSource parse(FunctionQParser fp) throws SyntaxError { |
| 41 | + ValueSource valueSource = fp.parseValueSource(); |
| 42 | + |
| 43 | + int size = 16; |
| 44 | + if (fp.hasMoreArguments()) { |
| 45 | + size = fp.parseInt(); |
| 46 | + } |
| 47 | + |
| 48 | + return new BitmapFrequencyAgg(valueSource, size); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private static class Merger extends FacetMerger { |
| 53 | + private final int size; |
| 54 | + private BitmapFrequencyCounter result; |
| 55 | + |
| 56 | + public Merger(int size) { |
| 57 | + this.size = size; |
| 58 | + this.result = new BitmapFrequencyCounter(size); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void merge(Object facetResult, Context mcontext) { |
| 63 | + if (facetResult instanceof SimpleOrderedMap) { |
| 64 | + BitmapFrequencyCounter deserialized = new BitmapFrequencyCounter(size); |
| 65 | + deserialized.deserialize((SimpleOrderedMap<Object>) facetResult); |
| 66 | + |
| 67 | + result = result.merge(deserialized); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void finish(Context mcontext) { |
| 73 | + // never called |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public Object getMergedResult() { |
| 78 | + return result.serialize(); |
| 79 | + } |
| 80 | + } |
| 81 | +} |
0 commit comments