Search in sources :

Example 6 with RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy 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 7 with RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy in project janusgraph by JanusGraph.

the class LuceneIndex method getSpatialStrategy.

private SpatialStrategy getSpatialStrategy(String key, KeyInformation ki) {
    SpatialStrategy strategy = spatial.get(key);
    final Mapping mapping = Mapping.getMapping(ki);
    final int maxLevels = ParameterType.INDEX_GEO_MAX_LEVELS.findParameter(ki.getParameters(), DEFAULT_GEO_MAX_LEVELS);
    final double distErrorPct = ParameterType.INDEX_GEO_DIST_ERROR_PCT.findParameter(ki.getParameters(), DEFAULT_GEO_DIST_ERROR_PCT);
    if (strategy == null) {
        synchronized (spatial) {
            if (!spatial.containsKey(key)) {
                // strategy = new RecursivePrefixTreeStrategy(grid, key);
                if (mapping == Mapping.DEFAULT) {
                    strategy = PointVectorStrategy.newInstance(ctx, key);
                } else {
                    final SpatialPrefixTree grid = new QuadPrefixTree(ctx, maxLevels);
                    strategy = new RecursivePrefixTreeStrategy(grid, key);
                    ((PrefixTreeStrategy) strategy).setDistErrPct(distErrorPct);
                }
                spatial.put(key, strategy);
            } else
                return spatial.get(key);
        }
    }
    return strategy;
}
Also used : QuadPrefixTree(org.apache.lucene.spatial.prefix.tree.QuadPrefixTree) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) Mapping(org.janusgraph.core.schema.Mapping) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)

Example 8 with RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy in project janusgraph by JanusGraph.

the class LuceneExample method getSpatialStrategy.

private SpatialStrategy getSpatialStrategy(String key) {
    SpatialStrategy strategy = spatial.get(key);
    if (strategy == null) {
        final int maxLevels = 11;
        SpatialPrefixTree grid = new GeohashPrefixTree(ctx, maxLevels);
        strategy = new RecursivePrefixTreeStrategy(grid, key);
        spatial.put(key, strategy);
    }
    return strategy;
}
Also used : 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 9 with RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy in project elasticsearch by elastic.

the class GeoShapeQueryBuilder method doToQuery.

@Override
protected Query doToQuery(QueryShardContext context) {
    if (shape == null) {
        throw new UnsupportedOperationException("query must be rewritten first");
    }
    final ShapeBuilder shapeToQuery = shape;
    final MappedFieldType fieldType = context.fieldMapper(fieldName);
    if (fieldType == null) {
        if (ignoreUnmapped) {
            return new MatchNoDocsQuery();
        } else {
            throw new QueryShardException(context, "failed to find geo_shape field [" + fieldName + "]");
        }
    }
    // TODO: This isn't the nicest way to check this
    if (!(fieldType instanceof GeoShapeFieldMapper.GeoShapeFieldType)) {
        throw new QueryShardException(context, "Field [" + fieldName + "] is not a geo_shape");
    }
    final GeoShapeFieldMapper.GeoShapeFieldType shapeFieldType = (GeoShapeFieldMapper.GeoShapeFieldType) fieldType;
    PrefixTreeStrategy strategy = shapeFieldType.defaultStrategy();
    if (this.strategy != null) {
        strategy = shapeFieldType.resolveStrategy(this.strategy);
    }
    Query query;
    if (strategy instanceof RecursivePrefixTreeStrategy && relation == ShapeRelation.DISJOINT) {
        // this strategy doesn't support disjoint anymore: but it did
        // before, including creating lucene fieldcache (!)
        // in this case, execute disjoint as exists && !intersects
        BooleanQuery.Builder bool = new BooleanQuery.Builder();
        Query exists = ExistsQueryBuilder.newFilter(context, fieldName);
        Query intersects = strategy.makeQuery(getArgs(shapeToQuery, ShapeRelation.INTERSECTS));
        bool.add(exists, BooleanClause.Occur.MUST);
        bool.add(intersects, BooleanClause.Occur.MUST_NOT);
        query = new ConstantScoreQuery(bool.build());
    } else {
        query = new ConstantScoreQuery(strategy.makeQuery(getArgs(shapeToQuery, relation)));
    }
    return query;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) ShapeBuilder(org.elasticsearch.common.geo.builders.ShapeBuilder) GeoShapeFieldMapper(org.elasticsearch.index.mapper.GeoShapeFieldMapper) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) PrefixTreeStrategy(org.apache.lucene.spatial.prefix.PrefixTreeStrategy) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)

Example 10 with RecursivePrefixTreeStrategy

use of org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy in project elasticsearch by elastic.

the class GeoFilterIT method testRelationSupport.

protected static boolean testRelationSupport(SpatialOperation relation) {
    if (relation == SpatialOperation.IsDisjointTo) {
        // disjoint works in terms of intersection
        relation = SpatialOperation.Intersects;
    }
    try {
        GeohashPrefixTree tree = new GeohashPrefixTree(SpatialContext.GEO, 3);
        RecursivePrefixTreeStrategy strategy = new RecursivePrefixTreeStrategy(tree, "area");
        Shape shape = SpatialContext.GEO.makePoint(0, 0);
        SpatialArgs args = new SpatialArgs(relation, shape);
        strategy.makeQuery(args);
        return true;
    } catch (UnsupportedSpatialOperation e) {
        final SpatialOperation finalRelation = relation;
        ESLoggerFactory.getLogger(GeoFilterIT.class.getName()).info((Supplier<?>) () -> new ParameterizedMessage("Unsupported spatial operation {}", finalRelation), e);
        return false;
    }
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) Supplier(org.apache.logging.log4j.util.Supplier) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Aggregations

RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)18 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)12 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)11 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)6 QuadPrefixTree (org.apache.lucene.spatial.prefix.tree.QuadPrefixTree)4 ArrayList (java.util.ArrayList)3 TermQueryPrefixTreeStrategy (org.apache.lucene.spatial.prefix.TermQueryPrefixTreeStrategy)3 PackedQuadPrefixTree (org.apache.lucene.spatial.prefix.tree.PackedQuadPrefixTree)3 SerializedDVStrategy (org.apache.lucene.spatial.serialized.SerializedDVStrategy)3 ParametersFactory (com.carrotsearch.randomizedtesting.annotations.ParametersFactory)2 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)2 PrefixTreeStrategy (org.apache.lucene.spatial.prefix.PrefixTreeStrategy)2 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)2 Point (org.locationtech.spatial4j.shape.Point)2 IOException (java.io.IOException)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 IndexWriter (org.apache.lucene.index.IndexWriter)1