Example usage for org.apache.lucene.facet Facets getTopChildren

List of usage examples for org.apache.lucene.facet Facets getTopChildren

Introduction

In this page you can find the example usage for org.apache.lucene.facet Facets getTopChildren.

Prototype

public abstract FacetResult getTopChildren(int topN, String dim, String... path) throws IOException;

Source Link

Document

Returns the topN child labels under the specified path.

Usage

From source file:com.orientechnologies.lucene.collections.LuceneResultSet.java

License:Apache License

private void fetchFacet() {
    if (queryContext.facet) {
        FacetsCollector facetsCollector = new FacetsCollector(true);

        try {/*www .  ja va  2s .  c  o m*/

            String[] pathFacet = null;
            if (queryContext.isDrillDown()) {
                DrillDownQuery drillDownQuery = new DrillDownQuery(queryContext.getFacetConfig(), query);
                String[] path = queryContext.getDrillDownQuery().split(":");
                pathFacet = path[1].split("/");
                drillDownQuery.add(path[0], pathFacet);
                FacetsCollector.search(queryContext.searcher, drillDownQuery, PAGE_SIZE, facetsCollector);
            } else {
                FacetsCollector.search(queryContext.searcher, query, PAGE_SIZE, facetsCollector);
            }

            Facets facets = new FastTaxonomyFacetCounts(queryContext.reader, queryContext.getFacetConfig(),
                    facetsCollector);

            FacetResult facetResult = null;
            if (pathFacet != null) {
                facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField(), pathFacet);
            } else {
                facetResult = facets.getTopChildren(PAGE_SIZE, queryContext.getFacetField());
            }

            if (facetResult != null) {
                List<ODocument> documents = new ArrayList<ODocument>();
                // for (FacetResult facetResult : res) {

                ODocument doc = new ODocument();

                doc.field("childCount", facetResult.childCount);
                doc.field("value", facetResult.value);
                doc.field("dim", facetResult.dim);
                List<ODocument> labelsAndValue = new ArrayList<ODocument>();
                for (LabelAndValue labelValue : facetResult.labelValues) {
                    ODocument doc1 = new ODocument();
                    doc1.field("label", labelValue.label);
                    doc1.field("value", labelValue.value);
                    labelsAndValue.add(doc1);

                }
                doc.field("labelsValue", labelsAndValue);
                documents.add(doc);
                queryContext.context.setVariable("$facet", documents);
            }
            // }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:com.xiaomi.linden.core.search.LindenResultParser.java

License:Apache License

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);
                }//from w  w  w  .  ja v a2s .  co m
                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);
            }
        }
    }
}

From source file:org.efaps.admin.index.Searcher.java

License:Apache License

/**
 * Recursive method to get the sub dimension.
 *
 * @param _facets the facets/*from  w ww  . ja va 2 s .co  m*/
 * @param _dimValue the _dim value
 * @param _dim the _dim
 * @param _path the _path
 * @throws IOException Signals that an I/O exception has occurred.
 */
private void addSubDimension(final Facets _facets, final DimValue _dimValue, final String _dim,
        final String _path) throws IOException {
    final FacetResult result = _facets.getTopChildren(1000, _dim, _path);
    if (result != null) {
        LOG.debug("FacetResult {}.", result);
        for (final LabelAndValue labelValue : result.labelValues) {
            final DimValue dimValue = new DimValue().setLabel(labelValue.label)
                    .setValue(labelValue.value.intValue());
            dimValue.setPath(ArrayUtils.addAll(_dimValue.getPath(), result.path));
            _dimValue.getChildren().add(dimValue);
            addSubDimension(_facets, dimValue, _dim, _path + "/" + labelValue.label);
        }
    }
}

From source file:org.fao.geonet.kernel.search.facet.ItemBuilderTest.java

License:Open Source License

private Facets buildFacets() throws IOException {
    Facets mockFacets = mock(Facets.class);

    LabelAndValue facet1 = new LabelAndValue("test1", 2);
    LabelAndValue facet2 = new LabelAndValue("test2", 1);
    LabelAndValue facet3 = new LabelAndValue("test3", 5);

    when(mockFacets.getTopChildren(MAX_RESULTS, DIMENSION_NAME, path()))
            .thenReturn(buildFacetResult(7, path(), facet1, facet2, facet3));
    when(mockFacets.getTopChildren(MAX_RESULTS, DIMENSION_NAME, path(facet1.label)))
            .thenReturn(buildFacetResult(facet1, path(facet1.label)));
    when(mockFacets.getTopChildren(MAX_RESULTS, DIMENSION_NAME, path(facet2.label)))
            .thenReturn(buildFacetResult(facet2, path(facet2.label)));
    when(mockFacets.getTopChildren(MAX_RESULTS, DIMENSION_NAME, path(facet3.label)))
            .thenReturn(buildFacetResult(facet3, path(facet3.label)));

    return mockFacets;
}

From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java

License:Open Source License

private CategoryDrillDownResponse getCategoryDrillDownResponse(CategoryDrillDownRequest drillDownRequest,
        List<CategorySearchResultEntry> searchResults, String[] path, Facets facets) throws IOException {
    FacetResult facetResult = facets.getTopChildren(Integer.MAX_VALUE, drillDownRequest.getFieldName(), path);
    CategoryDrillDownResponse response;/*from   ww  w  .j a v a 2s . co m*/
    if (facetResult != null) {
        LabelAndValue[] categories = facetResult.labelValues;
        for (LabelAndValue category : categories) {
            searchResults.add(new CategorySearchResultEntry(category.label, category.value.doubleValue()));
        }
        response = new CategoryDrillDownResponse(searchResults);
    } else {
        response = new CategoryDrillDownResponse(new ArrayList<CategorySearchResultEntry>(0));
    }
    return response;
}

From source file:org.wso2.carbon.analytics.dataservice.core.indexing.AnalyticsDataIndexer.java

License:Open Source License

private double getDrillDownRecordCount(int tenantId, AnalyticsDrillDownRequest drillDownRequest,
        IndexReader indexReader, TaxonomyReader taxonomyReader, String rangeField,
        AnalyticsDrillDownRange range) throws AnalyticsIndexException {
    try {// ww w.j  a  v  a 2 s  .co  m
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        Map<String, ColumnDefinition> indices = this.lookupIndices(tenantId, drillDownRequest.getTableName());
        FacetsConfig config = this.getFacetsConfigurations(indices);
        DrillDownQuery drillDownQuery = this.createDrillDownQuery(drillDownRequest, indices, config, rangeField,
                range);
        ValueSource scoreFunction = this.getCompiledScoreFunction(drillDownRequest.getScoreFunction(), indices);
        FacetsCollector facetsCollector = new FacetsCollector(true);
        Map<String, List<String>> categoryPaths = drillDownRequest.getCategoryPaths();
        double count = 0;
        if (!categoryPaths.isEmpty()) {
            Map.Entry<String, List<String>> aCategory = categoryPaths.entrySet().iterator().next();
            String categoryName = aCategory.getKey();
            FacetsCollector.search(indexSearcher, drillDownQuery, Integer.MAX_VALUE, facetsCollector);
            Facets facets = new TaxonomyFacetSumValueSource(taxonomyReader, config, facetsCollector,
                    scoreFunction);
            FacetResult facetResult = facets.getTopChildren(Integer.MAX_VALUE, categoryName, new String[0]);
            if (facetResult != null) {
                LabelAndValue[] subCategories = facetResult.labelValues;
                for (LabelAndValue category : subCategories) {
                    count += category.value.doubleValue();
                }
            }
        } else {
            count = indexSearcher.search(drillDownQuery, Integer.MAX_VALUE).totalHits;
        }
        return count;
    } catch (IndexNotFoundException ignore) {
        return 0;
    } catch (IOException e) {
        throw new AnalyticsIndexException("Error while getting drilldownCount: " + e.getMessage(), e);
    } finally {
        this.closeTaxonomyIndexReaders(indexReader, taxonomyReader);
    }
}