Search in sources :

Example 1 with DateMathFunction

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

the class StatsCollectorSupplierFactory method buildDateSource.

/**
   * Recursively parses and breaks down the expression string to build a date 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
   */
@SuppressWarnings("deprecation")
private static ValueSource buildDateSource(IndexSchema schema, String expressionString) {
    int paren = expressionString.indexOf('(');
    String[] arguments;
    if (paren < 0) {
        return buildFieldSource(schema, expressionString, DATE_TYPE);
    } else {
        arguments = ExpressionFactory.getArguments(expressionString.substring(paren + 1, expressionString.lastIndexOf(')')).trim());
    }
    String operands = arguments[0];
    String operation = expressionString.substring(0, paren).trim();
    if (operation.equals(AnalyticsParams.CONSTANT_DATE)) {
        if (arguments.length != 1) {
            throw new SolrException(ErrorCode.BAD_REQUEST, "The constant date declaration [" + expressionString + "] does not have exactly 1 argument.");
        }
        return new ConstDateSource(DateMathParser.parseMath(null, operands));
    } else if (operation.equals(AnalyticsParams.FILTER)) {
        return buildFilterSource(schema, operands, DATE_TYPE);
    }
    if (operation.equals(AnalyticsParams.DATE_MATH)) {
        List<ValueSource> subExpressions = new ArrayList<>();
        boolean first = true;
        for (String argument : arguments) {
            ValueSource argSource;
            if (first) {
                first = false;
                argSource = buildDateSource(schema, argument);
                if (argSource == null) {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "\"" + AnalyticsParams.DATE_MATH + "\" requires the first argument be a date operation or field. [" + argument + "] is not a date operation or field.");
                }
            } else {
                argSource = buildStringSource(schema, argument);
                if (argSource == null) {
                    throw new SolrException(ErrorCode.BAD_REQUEST, "\"" + AnalyticsParams.DATE_MATH + "\" requires that all arguments except the first be string operations. [" + argument + "] is not a string operation.");
                }
            }
            subExpressions.add(argSource);
        }
        return new DateMathFunction(subExpressions.toArray(new ValueSource[0]));
    }
    if (AnalyticsParams.NUMERIC_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 : ValueSource(org.apache.lucene.queries.function.ValueSource) ArrayList(java.util.ArrayList) ConstDateSource(org.apache.solr.analytics.util.valuesource.ConstDateSource) DateMathFunction(org.apache.solr.analytics.util.valuesource.DateMathFunction) SolrException(org.apache.solr.common.SolrException)

Aggregations

ArrayList (java.util.ArrayList)1 ValueSource (org.apache.lucene.queries.function.ValueSource)1 ConstDateSource (org.apache.solr.analytics.util.valuesource.ConstDateSource)1 DateMathFunction (org.apache.solr.analytics.util.valuesource.DateMathFunction)1 SolrException (org.apache.solr.common.SolrException)1