use of com.xiaomi.linden.thrift.common.AggregationResult 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);
}
}
}
}
use of com.xiaomi.linden.thrift.common.AggregationResult 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);
}
}
Aggregations