Search in sources :

Example 1 with SpatialStrategy

use of org.apache.lucene.spatial.SpatialStrategy 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 SpatialStrategy

use of org.apache.lucene.spatial.SpatialStrategy in project lucene-solr by apache.

the class GeoFieldUpdater method create.

@Override
public DocTransformer create(String display, SolrParams params, SolrQueryRequest req) {
    String fname = params.get("f", display);
    if (fname.startsWith("[") && fname.endsWith("]")) {
        fname = display.substring(1, display.length() - 1);
    }
    SchemaField sf = req.getSchema().getFieldOrNull(fname);
    if (sf == null) {
        throw new SolrException(ErrorCode.BAD_REQUEST, this.getClass().getSimpleName() + " using unknown field: " + fname);
    }
    if (!(sf.getType() instanceof AbstractSpatialFieldType)) {
        throw new SolrException(ErrorCode.BAD_REQUEST, "GeoTransformer requested non-spatial field: " + fname + " (" + sf.getType().getClass().getSimpleName() + ")");
    }
    final GeoFieldUpdater updater = new GeoFieldUpdater();
    updater.field = fname;
    updater.display = display;
    updater.display_error = display + "_error";
    ValueSource shapes = null;
    AbstractSpatialFieldType<?> sdv = (AbstractSpatialFieldType<?>) sf.getType();
    SpatialStrategy strategy = sdv.getStrategy(fname);
    if (strategy instanceof CompositeSpatialStrategy) {
        shapes = ((CompositeSpatialStrategy) strategy).getGeometryStrategy().makeShapeValueSource();
    } else if (strategy instanceof SerializedDVStrategy) {
        shapes = ((SerializedDVStrategy) strategy).makeShapeValueSource();
    }
    String writerName = params.get("w", "GeoJSON");
    updater.formats = strategy.getSpatialContext().getFormats();
    updater.writer = updater.formats.getWriter(writerName);
    if (updater.writer == null) {
        StringBuilder str = new StringBuilder();
        str.append("Unknown Spatial Writer: ").append(writerName);
        str.append(" [");
        for (ShapeWriter w : updater.formats.getWriters()) {
            str.append(w.getFormatName()).append(' ');
        }
        str.append("]");
        throw new SolrException(ErrorCode.BAD_REQUEST, str.toString());
    }
    QueryResponseWriter qw = req.getCore().getQueryResponseWriter(req);
    updater.isJSON = (qw.getClass() == JSONResponseWriter.class) && (updater.writer instanceof GeoJSONWriter);
    // Using ValueSource
    if (shapes != null) {
        // we don't really need the qparser... just so we can reuse valueSource
        QParser parser = new QParser(null, null, params, req) {

            @Override
            public Query parse() throws SyntaxError {
                return new MatchAllDocsQuery();
            }
        };
        return new ValueSourceAugmenter(display, parser, shapes) {

            @Override
            protected void setValue(SolrDocument doc, Object val) {
                updater.setValue(doc, val);
            }
        };
    }
    // Using the raw stored values
    return new DocTransformer() {

        @Override
        public void transform(SolrDocument doc, int docid, float score) throws IOException {
            Object val = doc.remove(updater.field);
            if (val != null) {
                updater.setValue(doc, val);
            }
        }

        @Override
        public String getName() {
            return updater.display;
        }

        @Override
        public String[] getExtraRequestFields() {
            return new String[] { updater.field };
        }
    };
}
Also used : CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) JSONResponseWriter(org.apache.solr.response.JSONResponseWriter) ShapeWriter(org.locationtech.spatial4j.io.ShapeWriter) AbstractSpatialFieldType(org.apache.solr.schema.AbstractSpatialFieldType) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) SerializedDVStrategy(org.apache.lucene.spatial.serialized.SerializedDVStrategy) SchemaField(org.apache.solr.schema.SchemaField) SolrDocument(org.apache.solr.common.SolrDocument) ValueSource(org.apache.lucene.queries.function.ValueSource) QParser(org.apache.solr.search.QParser) QueryResponseWriter(org.apache.solr.response.QueryResponseWriter) GeoJSONWriter(org.locationtech.spatial4j.io.GeoJSONWriter) SolrException(org.apache.solr.common.SolrException)

Example 3 with SpatialStrategy

use of org.apache.lucene.spatial.SpatialStrategy in project lucene-solr by apache.

the class SpatialDocMaker method setConfig.

@Override
public void setConfig(Config config, ContentSource source) {
    super.setConfig(config, source);
    SpatialStrategy existing = spatialStrategyCache.get(config.getRoundNumber());
    if (existing == null) {
        //new round; we need to re-initialize
        strategy = makeSpatialStrategy(config);
        spatialStrategyCache.put(config.getRoundNumber(), strategy);
        //TODO remove previous round config?
        shapeConverter = makeShapeConverter(strategy, config, "doc.spatial.");
        System.out.println("Spatial Strategy: " + strategy);
    }
}
Also used : CompositeSpatialStrategy(org.apache.lucene.spatial.composite.CompositeSpatialStrategy) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy)

Example 4 with SpatialStrategy

use of org.apache.lucene.spatial.SpatialStrategy in project ddf by codice.

the class GeoNamesLuceneIndexer method indexGeoEntries.

private void indexGeoEntries(final IndexWriter indexWriter, final List<GeoEntry> geoEntryList, final ProgressCallback progressCallback) throws IOException {
    int progress = 0;
    int currentEntry = 0;
    final int numGeoEntries = geoEntryList.size();
    final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
    final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
    for (GeoEntry geoEntry : geoEntryList) {
        addDocument(indexWriter, geoEntry, strategy);
        if (currentEntry == (int) (numGeoEntries * (progress / 100.0f))) {
            if (progressCallback != null) {
                progressCallback.updateProgress(progress);
            }
            progress += 5;
        }
        ++currentEntry;
    }
    // the work is complete.
    if (progressCallback != null) {
        progressCallback.updateProgress(100);
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Example 5 with SpatialStrategy

use of org.apache.lucene.spatial.SpatialStrategy in project titan by thinkaurelius.

the class LuceneIndex method getSpatialStrategy.

private SpatialStrategy getSpatialStrategy(String key) {
    SpatialStrategy strategy = spatial.get(key);
    if (strategy == null) {
        synchronized (spatial) {
            if (!spatial.containsKey(key)) {
                // SpatialPrefixTree grid = new GeohashPrefixTree(ctx, GEO_MAX_LEVELS);
                // strategy = new RecursivePrefixTreeStrategy(grid, key);
                strategy = new PointVectorStrategy(ctx, key);
                spatial.put(key, strategy);
            } else
                return spatial.get(key);
        }
    }
    return strategy;
}
Also used : PointVectorStrategy(org.apache.lucene.spatial.vector.PointVectorStrategy) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy)

Aggregations

SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)12 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)6 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)6 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)5 DoublePoint (org.apache.lucene.document.DoublePoint)3 LongPoint (org.apache.lucene.document.LongPoint)3 Mapping (org.janusgraph.core.schema.Mapping)3 Instant (java.time.Instant)2 IntPoint (org.apache.lucene.document.IntPoint)2 ValueSource (org.apache.lucene.queries.function.ValueSource)2 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)2 PointVectorStrategy (org.apache.lucene.spatial.vector.PointVectorStrategy)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 Shape (org.locationtech.spatial4j.shape.Shape)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DoubleDocValuesField (org.apache.lucene.document.DoubleDocValuesField)1 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)1