Skip to content

Commit

Permalink
int64 variants of bitmap freq aggs
Browse files Browse the repository at this point in the history
  • Loading branch information
mkavanagh committed Nov 3, 2021
1 parent 5c8de91 commit d3bac6b
Show file tree
Hide file tree
Showing 6 changed files with 1,091 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@
import org.apache.solr.search.facet.AvgAgg;
import org.apache.solr.search.facet.BitmapCollectorAgg;
import org.apache.solr.search.facet.BitmapFrequencyAgg;
import org.apache.solr.search.facet.BitmapFrequencyAgg64;
import org.apache.solr.search.facet.CountAgg;
import org.apache.solr.search.facet.FrequencyOfFrequenciesAgg;
import org.apache.solr.search.facet.FrequencyOfFrequenciesAgg64;
import org.apache.solr.search.facet.HLLAgg;
import org.apache.solr.search.facet.MinMaxAgg;
import org.apache.solr.search.facet.PercentileAgg;
Expand Down Expand Up @@ -1066,8 +1068,12 @@ public ValueSource parse(FunctionQParser fp) throws SyntaxError {

addParser("agg_bitmapfreq", new BitmapFrequencyAgg.Parser());

addParser("agg_bitmapfreq64", new BitmapFrequencyAgg64.Parser());

addParser("agg_bitmapfreqfreq", new FrequencyOfFrequenciesAgg.Parser());

addParser("agg_bitmapfreqfreq64", new FrequencyOfFrequenciesAgg64.Parser());

addParser("childfield", new ChildFieldValueSourceParser());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.apache.solr.search.facet;

import org.apache.lucene.queries.function.ValueSource;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.search.FunctionQParser;
import org.apache.solr.search.SyntaxError;
import org.apache.solr.search.ValueSourceParser;

/**
* Calculates the frequency of ordinal values using Roaring Bitmaps.
*
* The response is a map with the following fields:
* - bitmaps: an array of bitmaps, where the frequency of a value x is given by the sum of {@code 2^i} for all values
* of {@code i} where {@code bitmaps[i].contains(x)}
* - overflow: a map of ordinal values to frequencies, for values with {@code frequency >= 2^(bitmaps.length)}
*
* Lacking a coherent definition of magnitude other than the raw count, this aggregate cannot be used for sorting.
*/
public class BitmapFrequencyAgg64 extends SimpleAggValueSource {
private final int size;

public BitmapFrequencyAgg64(ValueSource vs, int size) {
super("bitmapfreq64", vs);

this.size = size;
}

@Override
public SlotAcc createSlotAcc(FacetContext fcontext, int numDocs, int numSlots) {
return new BitmapFrequencySlotAcc64(getArg(), fcontext, numSlots, size);
}

@Override
public FacetMerger createFacetMerger(Object prototype) {
return new Merger(size);
}

public static class Parser extends ValueSourceParser {
@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
ValueSource valueSource = fp.parseValueSource();

int size = 16;
if (fp.hasMoreArguments()) {
size = fp.parseInt();
}

return new BitmapFrequencyAgg64(valueSource, size);
}
}

private static class Merger extends FacetMerger {
private final int size;
private BitmapFrequencyCounter64 result;

public Merger(int size) {
this.size = size;
this.result = new BitmapFrequencyCounter64(size);
}

@Override
public void merge(Object facetResult, Context mcontext) {
if (facetResult instanceof SimpleOrderedMap) {
BitmapFrequencyCounter64 deserialized = new BitmapFrequencyCounter64(size);
deserialized.deserialize((SimpleOrderedMap<Object>) facetResult);

result = result.merge(deserialized);
}
}

@Override
public void finish(Context mcontext) {
// never called
}

@Override
public Object getMergedResult() {
result.normalize();
return result.serialize();
}
}
}
Loading

0 comments on commit d3bac6b

Please sign in to comment.