Search in sources :

Example 1 with LindenFacetResult

use of com.xiaomi.linden.thrift.common.LindenFacetResult 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 2 with LindenFacetResult

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

the class ResultMerger method mergeFacet.

private static void mergeFacet(final LindenSearchRequest lindenRequest, final List<LindenResult> resultList, LindenResult mergedResult) {
    if (resultList.size() == 1) {
        mergedResult.setFacetResults(resultList.get(0).getFacetResults());
        mergedResult.setAggregationResults(resultList.get(0).getAggregationResults());
        return;
    }
    Ordering<LindenLabelAndValue> labelAndValueOrdering = new Ordering<LindenLabelAndValue>() {

        @Override
        public int compare(@Nullable LindenLabelAndValue lv1, @Nullable LindenLabelAndValue lv2) {
            int cmp = Integer.compare(lv1.getValue(), lv2.getValue());
            if (cmp != 0) {
                return cmp;
            }
            return lv2.getLabel().compareTo(lv1.getLabel());
        }
    };
    // merge browse result
    for (int i = 0; i < lindenRequest.getFacet().getFacetParamsSize(); ++i) {
        LindenFacetResult mergedFacetResult = new LindenFacetResult();
        mergedFacetResult.setDim(lindenRequest.getFacet().getFacetParams().get(i).facetDimAndPath.dim);
        mergedFacetResult.setPath(lindenRequest.getFacet().getFacetParams().get(i).facetDimAndPath.path);
        Map<String, Integer> labelValueMap = new HashMap<>();
        for (int j = 0; j < resultList.size(); ++j) {
            if (resultList.get(j).getFacetResults() == null) {
                continue;
            }
            LindenFacetResult lindenFacetResult = resultList.get(j).getFacetResults().get(i);
            if (lindenFacetResult == null) {
                continue;
            }
            mergedFacetResult.setValue(mergedFacetResult.getValue() + lindenFacetResult.getValue());
            if (lindenFacetResult.getChildCount() > mergedFacetResult.getChildCount()) {
                mergedFacetResult.setChildCount(lindenFacetResult.getChildCount());
            }
            for (int k = 0; k < lindenFacetResult.getLabelValuesSize(); ++k) {
                String label = lindenFacetResult.getLabelValues().get(k).getLabel();
                int previous = labelValueMap.containsKey(label) ? labelValueMap.get(label) : 0;
                labelValueMap.put(label, previous + lindenFacetResult.getLabelValues().get(k).getValue());
            }
        }
        if (labelValueMap.size() > 0) {
            List<LindenLabelAndValue> labelAndValues = new ArrayList<>();
            Set<Map.Entry<String, Integer>> entrySet = labelValueMap.entrySet();
            for (Iterator<Map.Entry<String, Integer>> it = entrySet.iterator(); it.hasNext(); ) {
                Map.Entry<String, Integer> entry = it.next();
                labelAndValues.add(new LindenLabelAndValue(entry.getKey(), entry.getValue()));
            }
            if (labelAndValues.size() > mergedFacetResult.getChildCount()) {
                mergedFacetResult.setChildCount(labelAndValues.size());
            }
            List<LindenLabelAndValue> topLabelAndValues = labelAndValueOrdering.greatestOf(labelAndValues, lindenRequest.getFacet().getFacetParams().get(i).getTopN());
            mergedFacetResult.setLabelValues(topLabelAndValues);
        }
        mergedResult.addToFacetResults(mergedFacetResult);
    }
    // merge aggregation result
    for (int i = 0; i < lindenRequest.getFacet().getAggregationsSize(); ++i) {
        AggregationResult mergedAggregationResult = new AggregationResult();
        mergedAggregationResult.setField(lindenRequest.getFacet().getAggregations().get(i).getField());
        List<LindenLabelAndValue> lindenLabelAndValues = new ArrayList<>();
        for (int j = 0; j < lindenRequest.getFacet().getAggregations().get(i).getBucketsSize(); ++j) {
            LindenLabelAndValue lindenLabelAndValue = new LindenLabelAndValue();
            lindenLabelAndValue.setLabel(resultList.get(0).getAggregationResults().get(i).getLabelValues().get(j).getLabel());
            int value = 0;
            for (int k = 0; k < resultList.size(); ++k) {
                value += resultList.get(k).getAggregationResults().get(i).getLabelValues().get(j).getValue();
            }
            lindenLabelAndValue.setValue(value);
            lindenLabelAndValues.add(lindenLabelAndValue);
        }
        mergedAggregationResult.setLabelValues(lindenLabelAndValues);
        mergedResult.addToAggregationResults(mergedAggregationResult);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) LindenLabelAndValue(com.xiaomi.linden.thrift.common.LindenLabelAndValue) AggregationResult(com.xiaomi.linden.thrift.common.AggregationResult) Ordering(com.google.common.collect.Ordering) LindenFacetResult(com.xiaomi.linden.thrift.common.LindenFacetResult) HashMap(java.util.HashMap) Map(java.util.Map) Nullable(javax.annotation.Nullable)

Aggregations

AggregationResult (com.xiaomi.linden.thrift.common.AggregationResult)2 LindenFacetResult (com.xiaomi.linden.thrift.common.LindenFacetResult)2 LindenLabelAndValue (com.xiaomi.linden.thrift.common.LindenLabelAndValue)2 ArrayList (java.util.ArrayList)2 Ordering (com.google.common.collect.Ordering)1 Aggregation (com.xiaomi.linden.thrift.common.Aggregation)1 Bucket (com.xiaomi.linden.thrift.common.Bucket)1 LindenFacetParam (com.xiaomi.linden.thrift.common.LindenFacetParam)1 LindenType (com.xiaomi.linden.thrift.common.LindenType)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)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