Search in sources :

Example 1 with MultiValueSource

use of org.apache.lucene.queries.function.valuesource.MultiValueSource in project lucene-solr by apache.

the class GeoDistValueSourceParser method parse.

@Override
public ValueSource parse(FunctionQParser fp) throws SyntaxError {
    // TODO: dispatch through SpatialQueryable in the future?
    //note: parseValueSourceList can't handle a field reference to an AbstractSpatialFieldType,
    // so those fields are expressly handled via sfield=
    List<ValueSource> sources = fp.parseValueSourceList();
    // "m" is a multi-value source, "x" is a single-value source
    // allow (m,m) (m,x,x) (x,x,m) (x,x,x,x)
    // if not enough points are present, "pt" will be checked first, followed by "sfield".
    MultiValueSource mv1 = null;
    MultiValueSource mv2 = null;
    if (sources.size() == 0) {
    // nothing to do now
    } else if (sources.size() == 1) {
        ValueSource vs = sources.get(0);
        if (!(vs instanceof MultiValueSource)) {
            throw new SyntaxError("geodist - invalid parameters:" + sources);
        }
        mv1 = (MultiValueSource) vs;
    } else if (sources.size() == 2) {
        ValueSource vs1 = sources.get(0);
        ValueSource vs2 = sources.get(1);
        if (vs1 instanceof MultiValueSource && vs2 instanceof MultiValueSource) {
            mv1 = (MultiValueSource) vs1;
            mv2 = (MultiValueSource) vs2;
        } else {
            mv1 = makeMV(sources, sources);
        }
    } else if (sources.size() == 3) {
        ValueSource vs1 = sources.get(0);
        ValueSource vs2 = sources.get(1);
        if (vs1 instanceof MultiValueSource) {
            // (m,x,x)
            mv1 = (MultiValueSource) vs1;
            mv2 = makeMV(sources.subList(1, 3), sources);
        } else {
            // (x,x,m)
            mv1 = makeMV(sources.subList(0, 2), sources);
            vs1 = sources.get(2);
            if (!(vs1 instanceof MultiValueSource)) {
                throw new SyntaxError("geodist - invalid parameters:" + sources);
            }
            mv2 = (MultiValueSource) vs1;
        }
    } else if (sources.size() == 4) {
        mv1 = makeMV(sources.subList(0, 2), sources);
        mv2 = makeMV(sources.subList(2, 4), sources);
    } else if (sources.size() > 4) {
        throw new SyntaxError("geodist - invalid parameters:" + sources);
    }
    if (mv1 == null) {
        mv1 = parsePoint(fp);
        mv2 = parseSfield(fp);
    } else if (mv2 == null) {
        mv2 = parsePoint(fp);
        if (mv2 == null)
            mv2 = parseSfield(fp);
    }
    if (mv1 == null || mv2 == null) {
        throw new SyntaxError("geodist - not enough parameters:" + sources);
    }
    // We have all the parameters at this point, now check if one of the points is constant
    //latLon
    double[] constants;
    constants = getConstants(mv1);
    MultiValueSource other = mv2;
    if (constants == null) {
        constants = getConstants(mv2);
        other = mv1;
    }
    // sfield can only be in mv2, according to the logic above
    if (mv2 instanceof SpatialStrategyMultiValueSource) {
        if (constants == null)
            throw new SyntaxError("When using AbstractSpatialFieldType (e.g. RPT not LatLonType)," + " the point must be supplied as constants");
        // note: uses Haversine by default but can be changed via distCalc=...
        SpatialStrategy strategy = ((SpatialStrategyMultiValueSource) mv2).strategy;
        DistanceUnits distanceUnits = ((SpatialStrategyMultiValueSource) mv2).distanceUnits;
        Point queryPoint = strategy.getSpatialContext().makePoint(constants[1], constants[0]);
        return strategy.makeDistanceValueSource(queryPoint, distanceUnits.multiplierFromDegreesToThisUnit());
    }
    if (constants != null && other instanceof VectorValueSource) {
        return new HaversineConstFunction(constants[0], constants[1], (VectorValueSource) other);
    }
    return new HaversineFunction(mv1, mv2, DistanceUtils.EARTH_MEAN_RADIUS_KM, true);
}
Also used : SyntaxError(org.apache.solr.search.SyntaxError) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) DistanceUnits(org.apache.solr.util.DistanceUnits) Point(org.locationtech.spatial4j.shape.Point) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy)

Example 2 with MultiValueSource

use of org.apache.lucene.queries.function.valuesource.MultiValueSource in project lucene-solr by apache.

the class GeoDistValueSourceParser method makeMV.

/** make a MultiValueSource from two non MultiValueSources */
private VectorValueSource makeMV(List<ValueSource> sources, List<ValueSource> orig) throws SyntaxError {
    ValueSource vs1 = sources.get(0);
    ValueSource vs2 = sources.get(1);
    if (vs1 instanceof MultiValueSource || vs2 instanceof MultiValueSource) {
        throw new SyntaxError("geodist - invalid parameters:" + orig);
    }
    return new VectorValueSource(sources);
}
Also used : SyntaxError(org.apache.solr.search.SyntaxError) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) ValueSource(org.apache.lucene.queries.function.ValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource)

Example 3 with MultiValueSource

use of org.apache.lucene.queries.function.valuesource.MultiValueSource in project lucene-solr by apache.

the class GeoDistValueSourceParser method parseSfield.

private MultiValueSource parseSfield(FunctionQParser fp) throws SyntaxError {
    String sfield = fp.getParam(SpatialParams.FIELD);
    if (sfield == null)
        return null;
    SchemaField sf = fp.getReq().getSchema().getField(sfield);
    FieldType type = sf.getType();
    if (type instanceof AbstractSpatialFieldType) {
        AbstractSpatialFieldType asft = (AbstractSpatialFieldType) type;
        return new SpatialStrategyMultiValueSource(asft.getStrategy(sfield), asft.getDistanceUnits());
    }
    ValueSource vs = type.getValueSource(sf, fp);
    if (vs instanceof MultiValueSource) {
        return (MultiValueSource) vs;
    }
    throw new SyntaxError("Spatial field must implement MultiValueSource or extend AbstractSpatialFieldType:" + sf);
}
Also used : SchemaField(org.apache.solr.schema.SchemaField) SyntaxError(org.apache.solr.search.SyntaxError) ValueSource(org.apache.lucene.queries.function.ValueSource) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) VectorValueSource(org.apache.lucene.queries.function.valuesource.VectorValueSource) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MultiValueSource(org.apache.lucene.queries.function.valuesource.MultiValueSource) FieldType(org.apache.solr.schema.FieldType) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType)

Aggregations

ValueSource (org.apache.lucene.queries.function.ValueSource)3 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)3 MultiValueSource (org.apache.lucene.queries.function.valuesource.MultiValueSource)3 VectorValueSource (org.apache.lucene.queries.function.valuesource.VectorValueSource)3 SyntaxError (org.apache.solr.search.SyntaxError)3 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)1 AbstractSpatialFieldType (org.apache.solr.schema.AbstractSpatialFieldType)1 FieldType (org.apache.solr.schema.FieldType)1 SchemaField (org.apache.solr.schema.SchemaField)1 DistanceUnits (org.apache.solr.util.DistanceUnits)1 Point (org.locationtech.spatial4j.shape.Point)1