Search in sources :

Example 1 with Bucket

use of com.xiaomi.linden.thrift.common.Bucket in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitAggregation_spec.

@Override
public void exitAggregation_spec(BQLParser.Aggregation_specContext ctx) {
    String col = unescapeColumnName(ctx.column_name());
    Map.Entry<String, LindenType> fieldNameAndType = getFieldNameAndType(col);
    LindenType type = fieldNameAndType.getValue();
    if (type != LindenType.INTEGER && type != LindenType.LONG && type != LindenType.DOUBLE) {
        throw new ParseCancellationException(new SemanticException(ctx.column_name(), "Aggregation doesn't support the type of the field \"" + col + "\"."));
    }
    col = fieldNameAndType.getKey();
    Aggregation aggregation = new Aggregation();
    aggregation.setField(col);
    aggregation.setType(type);
    for (BQLParser.Bucket_specContext specContext : ctx.bucket_spec()) {
        Bucket bucket = (Bucket) valProperty.get(specContext);
        if (bucket != null) {
            aggregation.addToBuckets(bucket);
        }
    }
    facetRequest.addToAggregations(aggregation);
}
Also used : Aggregation(com.xiaomi.linden.thrift.common.Aggregation) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) Bucket(com.xiaomi.linden.thrift.common.Bucket) LindenType(com.xiaomi.linden.thrift.common.LindenType) Map(java.util.Map) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Example 2 with Bucket

use of com.xiaomi.linden.thrift.common.Bucket in project linden by XiaoMi.

the class LindenResultParser method parseFacets.

private void parseFacets(LindenResult result, Facets facets, FacetsCollector facetsCollector) throws IOException {
    // Set facets
    if (request.isSetFacet()) {
        if (request.getFacet().isSetFacetParams() && facets != null) {
            List<LindenFacetParam> facetParams = request.getFacet().getFacetParams();
            for (LindenFacetParam facetParam : facetParams) {
                LindenFacetResult lindenFacetResult = new LindenFacetResult();
                lindenFacetResult.setDim(facetParam.facetDimAndPath.dim);
                lindenFacetResult.setPath(facetParam.facetDimAndPath.path);
                FacetResult facetResult;
                if (facetParam.facetDimAndPath.path != null) {
                    facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim, facetParam.facetDimAndPath.path.split("/"));
                } else {
                    facetResult = facets.getTopChildren(facetParam.topN, facetParam.facetDimAndPath.dim);
                }
                if (facetResult != null) {
                    lindenFacetResult.setValue(facetResult.value.intValue());
                    lindenFacetResult.setChildCount(facetResult.childCount);
                    int sumValue = 0;
                    for (int j = 0; j < facetResult.labelValues.length; ++j) {
                        LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
                        labelAndValue.setLabel(facetResult.labelValues[j].label);
                        int value = facetResult.labelValues[j].value.intValue();
                        labelAndValue.setValue(value);
                        sumValue += value;
                        lindenFacetResult.addToLabelValues(labelAndValue);
                    }
                    if (sumValue > lindenFacetResult.getValue() || facetResult.labelValues.length < facetParam.topN) {
                        lindenFacetResult.setValue(sumValue);
                    }
                }
                result.addToFacetResults(lindenFacetResult);
            }
        } else if (request.getFacet().isSetAggregations() && facetsCollector != null) {
            List<Aggregation> aggregations = request.getFacet().getAggregations();
            for (int i = 0; i < aggregations.size(); ++i) {
                Aggregation aggregation = aggregations.get(i);
                String fieldName = aggregation.getField();
                LindenType type = aggregation.getType();
                AggregationResult aggregationResult = new AggregationResult();
                aggregationResult.setField(fieldName);
                Facets aggFacets;
                if (type == LindenType.INTEGER || type == LindenType.LONG) {
                    LongRange[] ranges = new LongRange[aggregation.getBucketsSize()];
                    for (int j = 0; j < ranges.length; ++j) {
                        Bucket bucket = aggregation.getBuckets().get(j);
                        String label = generateBucketLabel(bucket);
                        long minValue = bucket.getStartValue().equals("*") ? Long.MIN_VALUE : Long.parseLong(bucket.getStartValue());
                        long maxValue = bucket.getEndValue().equals("*") ? Long.MAX_VALUE : Long.parseLong(bucket.getEndValue());
                        ranges[j] = new LongRange(label, minValue, bucket.isStartClosed(), maxValue, bucket.isEndClosed());
                    }
                    aggFacets = new LongRangeFacetCounts(fieldName, facetsCollector, ranges);
                } else if (type == LindenType.DOUBLE) {
                    DoubleRange[] ranges = new DoubleRange[aggregation.getBucketsSize()];
                    for (int j = 0; j < ranges.length; ++j) {
                        Bucket bucket = aggregation.getBuckets().get(j);
                        String label = generateBucketLabel(bucket);
                        double minValue = bucket.getStartValue().equals("*") ? -Double.MAX_VALUE : Double.parseDouble(bucket.getStartValue());
                        double maxValue = bucket.getEndValue().equals("*") ? Double.MAX_VALUE : Double.parseDouble(bucket.getEndValue());
                        ranges[j] = new DoubleRange(label, minValue, bucket.isStartClosed(), maxValue, bucket.isEndClosed());
                    }
                    aggFacets = new DoubleRangeFacetCounts(fieldName, facetsCollector, ranges);
                } else {
                    throw new IOException(type + " type is not supported in aggregation");
                }
                FacetResult facetResult = aggFacets.getTopChildren(aggregation.getBucketsSize(), fieldName);
                for (int j = 0; j < facetResult.labelValues.length; ++j) {
                    LindenLabelAndValue labelAndValue = new LindenLabelAndValue();
                    labelAndValue.setLabel(facetResult.labelValues[j].label);
                    labelAndValue.setValue(facetResult.labelValues[j].value.intValue());
                    aggregationResult.addToLabelValues(labelAndValue);
                }
                result.addToAggregationResults(aggregationResult);
            }
        }
    }
}
Also used : LindenFacetParam(com.xiaomi.linden.thrift.common.LindenFacetParam) Facets(org.apache.lucene.facet.Facets) LindenType(com.xiaomi.linden.thrift.common.LindenType) IOException(java.io.IOException) LindenLabelAndValue(com.xiaomi.linden.thrift.common.LindenLabelAndValue) Aggregation(com.xiaomi.linden.thrift.common.Aggregation) DoubleRange(org.apache.lucene.facet.range.DoubleRange) AggregationResult(com.xiaomi.linden.thrift.common.AggregationResult) LongRange(org.apache.lucene.facet.range.LongRange) Bucket(com.xiaomi.linden.thrift.common.Bucket) LongRangeFacetCounts(org.apache.lucene.facet.range.LongRangeFacetCounts) LindenFacetResult(com.xiaomi.linden.thrift.common.LindenFacetResult) ArrayList(java.util.ArrayList) List(java.util.List) LindenFacetResult(com.xiaomi.linden.thrift.common.LindenFacetResult) FacetResult(org.apache.lucene.facet.FacetResult) DoubleRangeFacetCounts(org.apache.lucene.facet.range.DoubleRangeFacetCounts)

Example 3 with Bucket

use of com.xiaomi.linden.thrift.common.Bucket in project linden by XiaoMi.

the class BQLCompilerAnalyzer method exitBucket_spec.

@Override
public void exitBucket_spec(BQLParser.Bucket_specContext ctx) {
    Bucket bucket = new Bucket();
    if (ctx.LBRACE() != null) {
        bucket.setStartClosed(false);
    } else {
        bucket.setStartClosed(true);
    }
    if (ctx.n1 != null) {
        if (ctx.n1.PLACEHOLDER() != null) {
            return;
        }
        bucket.setStartValue(ctx.n1.getText());
    } else {
        bucket.setStartValue("*");
    }
    if (ctx.n2 != null) {
        if (ctx.n2.PLACEHOLDER() != null) {
            return;
        }
        bucket.setEndValue(ctx.n2.getText());
    } else {
        bucket.setEndValue("*");
    }
    if (ctx.RBRACE() != null) {
        bucket.setEndClosed(false);
    } else {
        bucket.setEndClosed(true);
    }
    valProperty.put(ctx, bucket);
}
Also used : Bucket(com.xiaomi.linden.thrift.common.Bucket)

Aggregations

Bucket (com.xiaomi.linden.thrift.common.Bucket)3 Aggregation (com.xiaomi.linden.thrift.common.Aggregation)2 LindenType (com.xiaomi.linden.thrift.common.LindenType)2 AggregationResult (com.xiaomi.linden.thrift.common.AggregationResult)1 LindenFacetParam (com.xiaomi.linden.thrift.common.LindenFacetParam)1 LindenFacetResult (com.xiaomi.linden.thrift.common.LindenFacetResult)1 LindenLabelAndValue (com.xiaomi.linden.thrift.common.LindenLabelAndValue)1 IOException (java.io.IOException)1 AbstractMap (java.util.AbstractMap)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1 FacetResult (org.apache.lucene.facet.FacetResult)1 Facets (org.apache.lucene.facet.Facets)1 DoubleRange (org.apache.lucene.facet.range.DoubleRange)1 DoubleRangeFacetCounts (org.apache.lucene.facet.range.DoubleRangeFacetCounts)1 LongRange (org.apache.lucene.facet.range.LongRange)1 LongRangeFacetCounts (org.apache.lucene.facet.range.LongRangeFacetCounts)1