Search in sources :

Example 1 with DoubleRange

use of org.apache.lucene.facet.range.DoubleRange 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 DoubleRange

use of org.apache.lucene.facet.range.DoubleRange in project nrtsearch by Yelp.

the class DrillSidewaysImpl method getFieldFacetResult.

private static com.yelp.nrtsearch.server.grpc.FacetResult getFieldFacetResult(FacetsCollector drillDowns, Map<String, FacetsCollector> dsDimMap, ShardState shardState, Facet facet, Map<String, FieldDef> dynamicFields, SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomyManager, Map<String, Facets> indexFieldNameToFacets) throws IOException {
    IndexState indexState = shardState.indexState;
    String fieldName = facet.getDim();
    FieldDef fieldDef = dynamicFields.get(fieldName);
    if (fieldDef == null) {
        throw new IllegalArgumentException(String.format("field %s was not registered and was not specified as a dynamic field ", fieldName));
    }
    FacetResult facetResult;
    if (!(fieldDef instanceof IndexableFieldDef) && !(fieldDef instanceof VirtualFieldDef)) {
        throw new IllegalArgumentException(String.format("field %s is neither a virtual field nor registered as an indexable field. Facets are supported only for these types", fieldName));
    }
    if (!facet.getNumericRangeList().isEmpty()) {
        if (fieldDef.getFacetValueType() != IndexableFieldDef.FacetValueType.NUMERIC_RANGE) {
            throw new IllegalArgumentException(String.format("field %s was not registered with facet=numericRange", fieldDef.getName()));
        }
        if (fieldDef instanceof IntFieldDef || fieldDef instanceof LongFieldDef) {
            List<NumericRangeType> rangeList = facet.getNumericRangeList();
            LongRange[] ranges = new LongRange[rangeList.size()];
            for (int i = 0; i < ranges.length; i++) {
                NumericRangeType numericRangeType = rangeList.get(i);
                ranges[i] = new LongRange(numericRangeType.getLabel(), numericRangeType.getMin(), numericRangeType.getMinInclusive(), numericRangeType.getMax(), numericRangeType.getMaxInclusive());
            }
            FacetsCollector c = dsDimMap.get(fieldDef.getName());
            if (c == null) {
                c = drillDowns;
            }
            LongRangeFacetCounts longRangeFacetCounts = new LongRangeFacetCounts(fieldDef.getName(), c, ranges);
            facetResult = longRangeFacetCounts.getTopChildren(0, fieldDef.getName(), facet.getPathsList().toArray(new String[facet.getPathsCount()]));
        } else if (fieldDef instanceof FloatFieldDef) {
            throw new IllegalArgumentException(String.format("field %s is of type float with FloatFieldDocValues which do not support numeric_range faceting", fieldDef.getName()));
        } else if (fieldDef instanceof DoubleFieldDef || fieldDef instanceof VirtualFieldDef) {
            List<NumericRangeType> rangeList = facet.getNumericRangeList();
            DoubleRange[] ranges = new DoubleRange[rangeList.size()];
            for (int i = 0; i < ranges.length; i++) {
                NumericRangeType numericRangeType = rangeList.get(i);
                ranges[i] = new DoubleRange(numericRangeType.getLabel(), numericRangeType.getMin(), numericRangeType.getMinInclusive(), numericRangeType.getMax(), numericRangeType.getMaxInclusive());
            }
            FacetsCollector c = dsDimMap.get(fieldDef.getName());
            if (c == null) {
                c = drillDowns;
            }
            DoubleRangeFacetCounts doubleRangeFacetCounts;
            if (fieldDef instanceof VirtualFieldDef) {
                VirtualFieldDef virtualFieldDef = (VirtualFieldDef) fieldDef;
                doubleRangeFacetCounts = new DoubleRangeFacetCounts(virtualFieldDef.getName(), virtualFieldDef.getValuesSource(), c, ranges);
            } else {
                doubleRangeFacetCounts = new DoubleRangeFacetCounts(fieldDef.getName(), c, ranges);
            }
            facetResult = doubleRangeFacetCounts.getTopChildren(0, fieldDef.getName(), facet.getPathsList().toArray(new String[facet.getPathsCount()]));
        } else {
            throw new IllegalArgumentException(String.format("numericRanges must be provided only on field type numeric e.g. int, double, flat"));
        }
    } else if (fieldDef.getFacetValueType() == IndexableFieldDef.FacetValueType.SORTED_SET_DOC_VALUES) {
        FacetsCollector c = dsDimMap.get(fieldDef.getName());
        if (c == null) {
            c = drillDowns;
        }
        if (facet.getLabelsCount() > 0) {
            // filter facet if a label list is provided
            FilteredSSDVFacetCounts filteredSSDVFacetCounts = new FilteredSSDVFacetCounts(facet.getLabelsList(), fieldDef.getName(), shardState.getSSDVState(searcherAndTaxonomyManager, fieldDef), c);
            facetResult = filteredSSDVFacetCounts.getTopChildren(facet.getTopN(), fieldDef.getName());
        } else {
            SortedSetDocValuesFacetCounts sortedSetDocValuesFacetCounts = new SortedSetDocValuesFacetCounts(shardState.getSSDVState(searcherAndTaxonomyManager, fieldDef), c);
            facetResult = sortedSetDocValuesFacetCounts.getTopChildren(facet.getTopN(), fieldDef.getName());
        }
    } else if (fieldDef.getFacetValueType() != IndexableFieldDef.FacetValueType.NO_FACETS) {
        // Taxonomy  facets
        if (fieldDef.getFacetValueType() == IndexableFieldDef.FacetValueType.NUMERIC_RANGE) {
            throw new IllegalArgumentException(String.format("%s was registered with facet = numericRange; must pass numericRanges in the request", fieldDef.getName()));
        }
        String[] path;
        if (!facet.getPathsList().isEmpty()) {
            ProtocolStringList pathList = facet.getPathsList();
            path = new String[facet.getPathsList().size()];
            for (int idx = 0; idx < path.length; idx++) {
                path[idx] = pathList.get(idx);
            }
        } else {
            path = new String[0];
        }
        FacetsCollector c = dsDimMap.get(fieldDef.getName());
        boolean useCachedOrds = facet.getUseOrdsCache();
        Facets luceneFacets;
        if (c != null) {
            // This dimension was used in
            // drill-down; compute its facet counts from the
            // drill-sideways collector:
            String indexFieldName = indexState.facetsConfig.getDimConfig(fieldDef.getName()).indexFieldName;
            if (useCachedOrds) {
                luceneFacets = new TaxonomyFacetCounts(shardState.getOrdsCache(indexFieldName), searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, c);
            } else {
                luceneFacets = new FastTaxonomyFacetCounts(indexFieldName, searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, c);
            }
        } else {
            // nocommit test both normal & ssdv facets in same index
            // See if we already computed facet
            // counts for this indexFieldName:
            String indexFieldName = indexState.facetsConfig.getDimConfig(fieldDef.getName()).indexFieldName;
            Map<String, Facets> facetsMap = indexFieldNameToFacets;
            luceneFacets = facetsMap.get(indexFieldName);
            if (luceneFacets == null) {
                if (useCachedOrds) {
                    luceneFacets = new TaxonomyFacetCounts(shardState.getOrdsCache(indexFieldName), searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, drillDowns);
                } else {
                    luceneFacets = new FastTaxonomyFacetCounts(indexFieldName, searcherAndTaxonomyManager.taxonomyReader, indexState.facetsConfig, drillDowns);
                }
                facetsMap.put(indexFieldName, luceneFacets);
            }
        }
        if (facet.getTopN() != 0) {
            facetResult = luceneFacets.getTopChildren(facet.getTopN(), fieldDef.getName(), path);
        } else if (!facet.getLabelsList().isEmpty()) {
            List<LabelAndValue> results = new ArrayList<LabelAndValue>();
            for (String label : facet.getLabelsList()) {
                results.add(new LabelAndValue(label, luceneFacets.getSpecificValue(fieldDef.getName(), label)));
            }
            facetResult = new FacetResult(fieldDef.getName(), path, -1, results.toArray(new LabelAndValue[results.size()]), -1);
        } else {
            throw new IllegalArgumentException(String.format("each facet request must have either topN or labels"));
        }
    } else {
        // if no facet type is enabled on the field, try using the field doc values
        if (!(fieldDef instanceof IndexableFieldDef)) {
            throw new IllegalArgumentException("Doc values facet requires an indexable field : " + fieldName);
        }
        IndexableFieldDef indexableFieldDef = (IndexableFieldDef) fieldDef;
        if (!indexableFieldDef.hasDocValues()) {
            throw new IllegalArgumentException("Doc values facet requires doc values enabled : " + fieldName);
        }
        return getDocValuesFacetResult(facet, drillDowns, indexableFieldDef);
    }
    return buildFacetResultGrpc(facetResult, facet.getName());
}
Also used : FastTaxonomyFacetCounts(org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts) Facets(org.apache.lucene.facet.Facets) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) LabelAndValue(org.apache.lucene.facet.LabelAndValue) IndexState(com.yelp.nrtsearch.server.luceneserver.IndexState) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) FastTaxonomyFacetCounts(org.apache.lucene.facet.taxonomy.FastTaxonomyFacetCounts) TaxonomyFacetCounts(org.apache.lucene.facet.taxonomy.TaxonomyFacetCounts) DoubleFieldDef(com.yelp.nrtsearch.server.luceneserver.field.DoubleFieldDef) LongRange(org.apache.lucene.facet.range.LongRange) LongRangeFacetCounts(org.apache.lucene.facet.range.LongRangeFacetCounts) IntFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IntFieldDef) ProtocolStringList(com.google.protobuf.ProtocolStringList) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) DoubleRangeFacetCounts(org.apache.lucene.facet.range.DoubleRangeFacetCounts) LongFieldDef(com.yelp.nrtsearch.server.luceneserver.field.LongFieldDef) NumericRangeType(com.yelp.nrtsearch.server.grpc.NumericRangeType) ProtocolStringList(com.google.protobuf.ProtocolStringList) FacetsCollector(org.apache.lucene.facet.FacetsCollector) FloatFieldDef(com.yelp.nrtsearch.server.luceneserver.field.FloatFieldDef) VirtualFieldDef(com.yelp.nrtsearch.server.luceneserver.field.VirtualFieldDef) IndexableFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IndexableFieldDef) FieldDef(com.yelp.nrtsearch.server.luceneserver.field.FieldDef) IntFieldDef(com.yelp.nrtsearch.server.luceneserver.field.IntFieldDef) DoubleFieldDef(com.yelp.nrtsearch.server.luceneserver.field.DoubleFieldDef) LongFieldDef(com.yelp.nrtsearch.server.luceneserver.field.LongFieldDef) DoubleRange(org.apache.lucene.facet.range.DoubleRange) FloatFieldDef(com.yelp.nrtsearch.server.luceneserver.field.FloatFieldDef) SortedSetDocValuesFacetCounts(org.apache.lucene.facet.sortedset.SortedSetDocValuesFacetCounts) FacetResult(org.apache.lucene.facet.FacetResult)

Aggregations

ArrayList (java.util.ArrayList)2 List (java.util.List)2 FacetResult (org.apache.lucene.facet.FacetResult)2 Facets (org.apache.lucene.facet.Facets)2 DoubleRange (org.apache.lucene.facet.range.DoubleRange)2 DoubleRangeFacetCounts (org.apache.lucene.facet.range.DoubleRangeFacetCounts)2 LongRange (org.apache.lucene.facet.range.LongRange)2 LongRangeFacetCounts (org.apache.lucene.facet.range.LongRangeFacetCounts)2 ProtocolStringList (com.google.protobuf.ProtocolStringList)1 Aggregation (com.xiaomi.linden.thrift.common.Aggregation)1 AggregationResult (com.xiaomi.linden.thrift.common.AggregationResult)1 Bucket (com.xiaomi.linden.thrift.common.Bucket)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 LindenType (com.xiaomi.linden.thrift.common.LindenType)1 NumericRangeType (com.yelp.nrtsearch.server.grpc.NumericRangeType)1 IndexState (com.yelp.nrtsearch.server.luceneserver.IndexState)1 DoubleFieldDef (com.yelp.nrtsearch.server.luceneserver.field.DoubleFieldDef)1 FieldDef (com.yelp.nrtsearch.server.luceneserver.field.FieldDef)1