Search in sources :

Example 1 with PowDoubleFunction

use of org.apache.solr.analytics.util.valuesource.PowDoubleFunction in project lucene-solr by apache.

the class StatsCollectorSupplierFactory method buildNumericSource.

/**
   * Recursively parses and breaks down the expression string to build a numeric ValueSource.
   * 
   * @param schema The schema to pull fields from.
   * @param expressionString The expression string to build a ValueSource from.
   * @return The value source represented by the given expressionString
   */
private static ValueSource buildNumericSource(IndexSchema schema, String expressionString) {
    int paren = expressionString.indexOf('(');
    String[] arguments;
    String operands;
    if (paren < 0) {
        return buildFieldSource(schema, expressionString, NUMBER_TYPE);
    } else {
        try {
            operands = expressionString.substring(paren + 1, expressionString.lastIndexOf(')')).trim();
        } catch (Exception e) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "Missing closing parenthesis in [" + expressionString + "]");
        }
        arguments = ExpressionFactory.getArguments(operands);
    }
    String operation = expressionString.substring(0, paren).trim();
    if (operation.equals(AnalyticsParams.CONSTANT_NUMBER)) {
        if (arguments.length != 1) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The constant number declaration [" + expressionString + "] does not have exactly 1 argument.");
        }
        return new ConstDoubleSource(Double.parseDouble(arguments[0]));
    } else if (operation.equals(AnalyticsParams.NEGATE)) {
        if (arguments.length != 1) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The negate operation [" + expressionString + "] does not have exactly 1 argument.");
        }
        ValueSource argSource = buildNumericSource(schema, arguments[0]);
        if (argSource == null) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The operation \"" + AnalyticsParams.NEGATE + "\" requires a numeric field or operation as argument. \"" + arguments[0] + "\" is not a numeric field or operation.");
        }
        return new NegateDoubleFunction(argSource);
    } else if (operation.equals(AnalyticsParams.ABSOLUTE_VALUE)) {
        if (arguments.length != 1) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The absolute value operation [" + expressionString + "] does not have exactly 1 argument.");
        }
        ValueSource argSource = buildNumericSource(schema, arguments[0]);
        if (argSource == null) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The operation \"" + AnalyticsParams.NEGATE + "\" requires a numeric field or operation as argument. \"" + arguments[0] + "\" is not a numeric field or operation.");
        }
        return new AbsoluteValueDoubleFunction(argSource);
    } else if (operation.equals(AnalyticsParams.FILTER)) {
        return buildFilterSource(schema, operands, NUMBER_TYPE);
    }
    List<ValueSource> subExpressions = new ArrayList<>();
    for (String argument : arguments) {
        ValueSource argSource = buildNumericSource(schema, argument);
        if (argSource == null) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The operation \"" + operation + "\" requires numeric fields or operations as arguments. \"" + argument + "\" is not a numeric field or operation.");
        }
        subExpressions.add(argSource);
    }
    if (operation.equals(AnalyticsParams.ADD)) {
        return new AddDoubleFunction(subExpressions.toArray(new ValueSource[0]));
    } else if (operation.equals(AnalyticsParams.MULTIPLY)) {
        return new MultiplyDoubleFunction(subExpressions.toArray(new ValueSource[0]));
    } else if (operation.equals(AnalyticsParams.DIVIDE)) {
        if (subExpressions.size() != 2) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The divide operation [" + expressionString + "] does not have exactly 2 arguments.");
        }
        return new DivDoubleFunction(subExpressions.get(0), subExpressions.get(1));
    } else if (operation.equals(AnalyticsParams.POWER)) {
        if (subExpressions.size() != 2) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The power operation [" + expressionString + "] does not have exactly 2 arguments.");
        }
        return new PowDoubleFunction(subExpressions.get(0), subExpressions.get(1));
    } else if (operation.equals(AnalyticsParams.LOG)) {
        if (subExpressions.size() != 2) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The log operation [" + expressionString + "] does not have exactly 2 arguments.");
        }
        return new LogDoubleFunction(subExpressions.get(0), subExpressions.get(1));
    }
    if (AnalyticsParams.DATE_OPERATION_SET.contains(operation) || AnalyticsParams.STRING_OPERATION_SET.contains(operation)) {
        return null;
    }
    throw new SolrException(ErrorCode.BAD_REQUEST, "The operation [" + expressionString + "] is not supported.");
}
Also used : AddDoubleFunction(org.apache.solr.analytics.util.valuesource.AddDoubleFunction) ArrayList(java.util.ArrayList) DivDoubleFunction(org.apache.solr.analytics.util.valuesource.DivDoubleFunction) ConstDoubleSource(org.apache.solr.analytics.util.valuesource.ConstDoubleSource) SolrException(org.apache.solr.common.SolrException) PowDoubleFunction(org.apache.solr.analytics.util.valuesource.PowDoubleFunction) MultiplyDoubleFunction(org.apache.solr.analytics.util.valuesource.MultiplyDoubleFunction) ValueSource(org.apache.lucene.queries.function.ValueSource) NegateDoubleFunction(org.apache.solr.analytics.util.valuesource.NegateDoubleFunction) AbsoluteValueDoubleFunction(org.apache.solr.analytics.util.valuesource.AbsoluteValueDoubleFunction) LogDoubleFunction(org.apache.solr.analytics.util.valuesource.LogDoubleFunction) SolrException(org.apache.solr.common.SolrException)

Aggregations

ArrayList (java.util.ArrayList)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 AbsoluteValueDoubleFunction (org.apache.solr.analytics.util.valuesource.AbsoluteValueDoubleFunction)1 AddDoubleFunction (org.apache.solr.analytics.util.valuesource.AddDoubleFunction)1 ConstDoubleSource (org.apache.solr.analytics.util.valuesource.ConstDoubleSource)1 DivDoubleFunction (org.apache.solr.analytics.util.valuesource.DivDoubleFunction)1 LogDoubleFunction (org.apache.solr.analytics.util.valuesource.LogDoubleFunction)1 MultiplyDoubleFunction (org.apache.solr.analytics.util.valuesource.MultiplyDoubleFunction)1 NegateDoubleFunction (org.apache.solr.analytics.util.valuesource.NegateDoubleFunction)1 PowDoubleFunction (org.apache.solr.analytics.util.valuesource.PowDoubleFunction)1 SolrException (org.apache.solr.common.SolrException)1