Example usage for org.apache.lucene.queries.function.valuesource DualFloatFunction DualFloatFunction

List of usage examples for org.apache.lucene.queries.function.valuesource DualFloatFunction DualFloatFunction

Introduction

In this page you can find the example usage for org.apache.lucene.queries.function.valuesource DualFloatFunction DualFloatFunction.

Prototype

public DualFloatFunction(ValueSource a, ValueSource b) 

Source Link

Usage

From source file:org.apache.solr.search.ValueSourceParser.java

License:Apache License

@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
    String first = fp.parseArg();
    String second = fp.parseArg();
    if (first == null)
        first = "NOW";

    Date d1 = getDate(fp, first);
    ValueSource v1 = d1 == null ? getValueSource(fp, first) : null;

    Date d2 = getDate(fp, second);
    ValueSource v2 = d2 == null ? getValueSource(fp, second) : null;

    // d     constant
    // v     field
    // dd    constant
    // dv    subtract field from constant
    // vd    subtract constant from field
    // vv    subtract fields

    final long ms1 = (d1 == null) ? 0 : d1.getTime();
    final long ms2 = (d2 == null) ? 0 : d2.getTime();

    // "d,dd" handle both constant cases

    if (d1 != null && v2 == null) {
        return new LongConstValueSource(ms1 - ms2);
    }// ww w  .  jav a  2s . c  o  m

    // "v" just the date field
    if (v1 != null && v2 == null && d2 == null) {
        return v1;
    }

    // "dv"
    if (d1 != null && v2 != null)
        return new DualFloatFunction(new LongConstValueSource(ms1), v2) {
            @Override
            protected String name() {
                return "ms";
            }

            @Override
            protected float func(int doc, FunctionValues aVals, FunctionValues bVals) {
                return ms1 - bVals.longVal(doc);
            }
        };

    // "vd"
    if (v1 != null && d2 != null)
        return new DualFloatFunction(v1, new LongConstValueSource(ms2)) {
            @Override
            protected String name() {
                return "ms";
            }

            @Override
            protected float func(int doc, FunctionValues aVals, FunctionValues bVals) {
                return aVals.longVal(doc) - ms2;
            }
        };

    // "vv"
    if (v1 != null && v2 != null)
        return new DualFloatFunction(v1, v2) {
            @Override
            protected String name() {
                return "ms";
            }

            @Override
            protected float func(int doc, FunctionValues aVals, FunctionValues bVals) {
                return aVals.longVal(doc) - bVals.longVal(doc);
            }
        };

    return null; // shouldn't happen
}