Search in sources :

Example 1 with StatsCollector

use of org.apache.solr.analytics.statistics.StatsCollector in project lucene-solr by apache.

the class ExpressionFactory method create.

/**
   * Creates a single expression that contains delegate expressions and/or 
   * a StatsCollector.
   * StatsCollectors are given as input and not created within the method so that
   * expressions can share the same StatsCollectors, minimizing computation.
   * 
   * @param expression String representation of the desired expression
   * @param statsCollectors List of StatsCollectors to build the expression with. 
   * @return the expression
   */
@SuppressWarnings("deprecation")
public static Expression create(String expression, StatsCollector[] statsCollectors) {
    int paren = expression.indexOf('(');
    if (paren <= 0) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "The expression [" + expression + "] has no arguments and is not supported.");
    }
    String topOperation = expression.substring(0, paren).trim();
    String operands;
    try {
        operands = expression.substring(paren + 1, expression.lastIndexOf(')')).trim();
    } catch (Exception e) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "Missing closing parenthesis in [" + expression + "]", e);
    }
    // Statistic 
    if (AnalyticsParams.ALL_STAT_SET.contains(topOperation)) {
        if (topOperation.equals(AnalyticsParams.STAT_PERCENTILE)) {
            operands = expression.substring(expression.indexOf(',') + 1, expression.lastIndexOf(')')).trim();
            topOperation = topOperation + "_" + expression.substring(expression.indexOf('(') + 1, expression.indexOf(',')).trim();
        }
        StatsCollector collector = null;
        // Finds the desired counter and builds an expression around it and the desired statistic.
        for (StatsCollector c : statsCollectors) {
            if (c.valueSourceString().equals(operands)) {
                collector = c;
                break;
            }
        }
        if (collector == null) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "ValueSource [" + operands + "] in Expression [" + expression + "] not found.");
        }
        return new BaseExpression(collector, topOperation);
    }
    // Constant
    if (topOperation.equals(AnalyticsParams.CONSTANT_NUMBER)) {
        try {
            return new ConstantNumberExpression(Double.parseDouble(operands));
        } catch (NumberFormatException e) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The constant " + operands + " cannot be converted into a number.", e);
        }
    } else if (topOperation.equals(AnalyticsParams.CONSTANT_DATE)) {
        return new ConstantDateExpression(DateMathParser.parseMath(null, operands));
    } else if (topOperation.equals(AnalyticsParams.CONSTANT_STRING)) {
        operands = expression.substring(paren + 1, expression.lastIndexOf(')'));
        return new ConstantStringExpression(operands);
    }
    // Complex Delegating Expressions
    String[] arguments = getArguments(operands);
    Expression[] expArgs = new Expression[arguments.length];
    for (int count = 0; count < arguments.length; count++) {
        // Recursively builds delegate expressions
        expArgs[count] = create(arguments[count], statsCollectors);
    }
    // Single Delegate Expressions
    if (expArgs.length == 1) {
        // Numeric Expression
        if (topOperation.equals(AnalyticsParams.NEGATE)) {
            return new NegateExpression(expArgs[0]);
        }
        if (topOperation.equals(AnalyticsParams.ABSOLUTE_VALUE)) {
            return new AbsoluteValueExpression(expArgs[0]);
        } else // String Expression
        if (topOperation.equals(AnalyticsParams.REVERSE)) {
            return new ReverseExpression(expArgs[0]);
        }
        throw new SolrException(ErrorCode.BAD_REQUEST, topOperation + " does not have the correct number of arguments.");
    } else {
        // Numeric Expression
        if (topOperation.equals(AnalyticsParams.ADD)) {
            return new AddExpression(expArgs);
        } else if (topOperation.equals(AnalyticsParams.MULTIPLY)) {
            return new MultiplyExpression(expArgs);
        } else // Date Expression
        if (topOperation.equals(AnalyticsParams.DATE_MATH)) {
            return new DateMathExpression(expArgs);
        } else // String Expression
        if (topOperation.equals(AnalyticsParams.CONCATENATE)) {
            return new ConcatenateExpression(expArgs);
        } else // Dual Delegate Expressions
        if (expArgs.length == 2 && (topOperation.equals(AnalyticsParams.DIVIDE) || topOperation.equals(AnalyticsParams.POWER) || topOperation.equals(AnalyticsParams.LOG))) {
            // Numeric Expression
            if (topOperation.equals(AnalyticsParams.DIVIDE)) {
                return new DivideExpression(expArgs[0], expArgs[1]);
            } else if (topOperation.equals(AnalyticsParams.POWER)) {
                return new PowerExpression(expArgs[0], expArgs[1]);
            } else if (topOperation.equals(AnalyticsParams.LOG)) {
                return new LogExpression(expArgs[0], expArgs[1]);
            }
            return null;
        }
        throw new SolrException(ErrorCode.BAD_REQUEST, topOperation + " does not have the correct number of arguments or is unsupported.");
    }
}
Also used : StatsCollector(org.apache.solr.analytics.statistics.StatsCollector) SolrException(org.apache.solr.common.SolrException) SolrException(org.apache.solr.common.SolrException)

Example 2 with StatsCollector

use of org.apache.solr.analytics.statistics.StatsCollector in project lucene-solr by apache.

the class FacetingAccumulator method collectRange.

/**
   * Given a document, rangeFacet field and range, adds the document to the
   * {@link StatsCollector}s held in the bucket corresponding to the rangeFacet field and range.
   * Called during post processing.
   */
@Override
public void collectRange(int doc, String facetField, String range) throws IOException {
    Map<String, StatsCollector[]> map = rangeFacetCollectors.get(facetField);
    StatsCollector[] statsCollectors = map.get(range);
    // created and associated with that bucket.
    if (statsCollectors == null) {
        statsCollectors = statsCollectorArraySupplier.get();
        map.put(range, statsCollectors);
        rangeFacetExpressions.get(facetField).put(range, makeExpressions(statsCollectors));
        for (StatsCollector statsCollector : statsCollectors) {
            statsCollector.setNextReader(context);
        }
    }
    for (StatsCollector statsCollector : statsCollectors) {
        statsCollector.collect(doc);
    }
}
Also used : StatsCollector(org.apache.solr.analytics.statistics.StatsCollector)

Example 3 with StatsCollector

use of org.apache.solr.analytics.statistics.StatsCollector in project lucene-solr by apache.

the class FacetingAccumulator method collectQuery.

/**
   * Given a document, queryFacet name and query, adds the document to the
   * {@link StatsCollector}s held in the bucket corresponding to the queryFacet name and query.
   * Called during post processing.
   */
@Override
public void collectQuery(int doc, String facetName, String query) throws IOException {
    Map<String, StatsCollector[]> map = queryFacetCollectors.get(facetName);
    StatsCollector[] statsCollectors = map.get(query);
    // created and associated with that bucket.
    if (statsCollectors == null) {
        statsCollectors = statsCollectorArraySupplier.get();
        map.put(query, statsCollectors);
        queryFacetExpressions.get(facetName).put(query, makeExpressions(statsCollectors));
        for (StatsCollector statsCollector : statsCollectors) {
            statsCollector.setNextReader(context);
        }
    }
    for (StatsCollector statsCollector : statsCollectors) {
        statsCollector.collect(doc);
    }
}
Also used : StatsCollector(org.apache.solr.analytics.statistics.StatsCollector)

Example 4 with StatsCollector

use of org.apache.solr.analytics.statistics.StatsCollector in project lucene-solr by apache.

the class FacetingAccumulator method compute.

/**
   * Finalizes the statistics within the each facet bucket before exporting;
   */
@Override
public void compute() {
    if (!basicsAndFieldFacetsComputed) {
        super.compute();
        for (Map<String, StatsCollector[]> f : fieldFacetCollectors.values()) {
            for (StatsCollector[] arr : f.values()) {
                for (StatsCollector b : arr) {
                    b.compute();
                }
            }
        }
        basicsAndFieldFacetsComputed = true;
    }
}
Also used : StatsCollector(org.apache.solr.analytics.statistics.StatsCollector)

Example 5 with StatsCollector

use of org.apache.solr.analytics.statistics.StatsCollector in project lucene-solr by apache.

the class FacetingAccumulator method collectField.

/**
   * Given a document, fieldFacet field and facetValue, adds the document to the
   * {@link StatsCollector}s held in the bucket corresponding to the fieldFacet field and facetValue.
   * Called during initial document collection.
   */
@Override
public void collectField(int doc, String facetField, String facetValue) throws IOException {
    Map<String, StatsCollector[]> map = fieldFacetCollectors.get(facetField);
    StatsCollector[] statsCollectors = map.get(facetValue);
    // created and associated with that bucket.
    if (statsCollectors == null) {
        statsCollectors = statsCollectorArraySupplier.get();
        map.put(facetValue, statsCollectors);
        fieldFacetExpressions.get(facetField).put(facetValue, makeExpressions(statsCollectors));
        for (StatsCollector statsCollector : statsCollectors) {
            statsCollector.setNextReader(context);
        }
    }
    for (StatsCollector statsCollector : statsCollectors) {
        statsCollector.collect(doc);
    }
}
Also used : StatsCollector(org.apache.solr.analytics.statistics.StatsCollector)

Aggregations

StatsCollector (org.apache.solr.analytics.statistics.StatsCollector)6 SolrException (org.apache.solr.common.SolrException)1 Filter (org.apache.solr.search.Filter)1