Example usage for org.apache.lucene.facet.taxonomy TaxonomyFacetSumValueSource TaxonomyFacetSumValueSource

List of usage examples for org.apache.lucene.facet.taxonomy TaxonomyFacetSumValueSource TaxonomyFacetSumValueSource

Introduction

In this page you can find the example usage for org.apache.lucene.facet.taxonomy TaxonomyFacetSumValueSource TaxonomyFacetSumValueSource.

Prototype

public TaxonomyFacetSumValueSource(TaxonomyReader taxoReader, FacetsConfig config, FacetsCollector fc,
        DoubleValuesSource valueSource) throws IOException 

Source Link

Document

Aggreggates double facet values from the provided DoubleValuesSource , pulling ordinals using DocValuesOrdinalsReader against the default indexed facet field FacetsConfig#DEFAULT_INDEX_FIELD_NAME .

Usage

From source file:com.czw.search.lucene.example.facet.ExpressionAggregationFacetsExample.java

License:Apache License

/** User runs a query and aggregates facets. */
private FacetResult search() throws IOException, ParseException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    // Aggregate categories by an expression that combines the document's score
    // and its popularity field
    Expression expr = JavascriptCompiler.compile("_score * sqrt(popularity)");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("_score", SortField.Type.SCORE)); // the score of the document
    bindings.add(new SortField("popularity", SortField.Type.LONG)); // the value of the 'popularity' field

    // Aggregates the facet values
    FacetsCollector fc = new FacetsCollector(true);

    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

    // Retrieve results
    Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc,
            expr.getDoubleValuesSource(bindings));
    FacetResult result = facets.getTopChildren(10, "A");

    indexReader.close();/*w  ww.j  a  va2  s .  c  o  m*/
    taxoReader.close();

    return result;
}

From source file:com.search.lucene.demo.facet.ExpressionAggregationFacetsExample.java

License:Apache License

/** User runs a query and aggregates facets. */
private FacetResult search() throws IOException, ParseException {
    DirectoryReader indexReader = DirectoryReader.open(indexDir);
    IndexSearcher searcher = new IndexSearcher(indexReader);
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoDir);

    // Aggregate categories by an expression that combines the document's score
    // and its popularity field
    Expression expr = JavascriptCompiler.compile("_score * sqrt(popularity)");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("_score", SortField.Type.SCORE)); // the score of the document
    bindings.add(new SortField("popularity", SortField.Type.LONG)); // the value of the 'popularity' field

    // Aggregates the facet values
    FacetsCollector fc = new FacetsCollector(true);

    // MatchAllDocsQuery is for "browsing" (counts facets
    // for all non-deleted docs in the index); normally
    // you'd use a "normal" query:
    FacetsCollector.search(searcher, new MatchAllDocsQuery(), 10, fc);

    // Retrieve results
    Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, expr.getValueSource(bindings));
    FacetResult result = facets.getTopChildren(10, "A");

    indexReader.close();//from   w  w  w .ja  v a 2s . c o m
    taxoReader.close();

    return result;
}

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

License:Open Source License

private CategoryDrillDownResponse drilldowncategories(int tenantId, IndexReader indexReader,
        TaxonomyReader taxonomyReader, CategoryDrillDownRequest drillDownRequest)
        throws AnalyticsIndexException {
    List<CategorySearchResultEntry> searchResults = new ArrayList<>();
    try {//from  w w w.ja v a 2s . co m
        IndexSearcher indexSearcher = new IndexSearcher(indexReader);
        FacetsCollector facetsCollector = new FacetsCollector(true);
        Map<String, ColumnDefinition> indices = this.lookupIndices(tenantId, drillDownRequest.getTableName());
        FacetsConfig config = this.getFacetsConfigurations(indices);
        DrillSideways drillSideways = new DrillSideways(indexSearcher, config, taxonomyReader);
        Query queryObj = new MatchAllDocsQuery();
        if (drillDownRequest.getQuery() != null && !drillDownRequest.getQuery().isEmpty()) {
            Analyzer analyzer = getPerFieldAnalyzerWrapper(indices);
            queryObj = (new AnalyticsQueryParser(analyzer, indices)).parse(drillDownRequest.getQuery());
        }
        DrillDownQuery drillDownQuery = new DrillDownQuery(config, queryObj);
        String[] path = drillDownRequest.getPath();
        if (path == null) {
            path = new String[] {};
        }
        drillDownQuery.add(drillDownRequest.getFieldName(), path);
        drillSideways.search(drillDownQuery, facetsCollector);
        ValueSource valueSource = this.getCompiledScoreFunction(drillDownRequest.getScoreFunction(), indices);
        Facets facets = new TaxonomyFacetSumValueSource(taxonomyReader, config, facetsCollector, valueSource);
        return getCategoryDrillDownResponse(drillDownRequest, searchResults, path, facets);
    } catch (IndexNotFoundException ignore) {
        return new CategoryDrillDownResponse(new ArrayList<CategorySearchResultEntry>(0));
    } catch (IOException e) {
        throw new AnalyticsIndexException("Error while performing drilldownCategories: " + e.getMessage(), e);
    } catch (org.apache.lucene.queryparser.classic.ParseException e) {
        throw new AnalyticsIndexException("Error while parsing query " + e.getMessage(), e);
    } finally {
        this.closeTaxonomyIndexReaders(indexReader, taxonomyReader);
    }
}

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 {/*from w  w w.  ja v  a  2  s .c o  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);
    }
}