Search in sources :

Example 6 with SpatialStrategy

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

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 7 with SpatialStrategy

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

the class GeoNamesQueryLuceneIndex method createSpatialQuery.

private Query createSpatialQuery(Point shapeCenter, int radiusInKm) {
    final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
    final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
    // Create a spatial filter that will select the documents that are in the specified
    // search radius around the metacard's center.
    final double searchRadiusDegrees = radiusInKm * DistanceUtils.KM_TO_DEG;
    final SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, SPATIAL_CONTEXT.getShapeFactory().circle(shapeCenter, searchRadiusDegrees));
    return strategy.makeQuery(args);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) 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 8 with SpatialStrategy

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

the class GeoNamesLuceneIndexer method updateIndex.

@Override
public void updateIndex(final String resource, final GeoEntryExtractor geoEntryExtractor, final boolean create, final ProgressCallback progressCallback) throws GeoEntryIndexingException, GeoEntryExtractionException, GeoNamesRemoteDownloadException {
    Directory directory;
    try {
        directory = FSDirectory.open(Paths.get(indexLocation));
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Couldn't open the directory for the index, " + indexLocation, e);
    }
    // Try-with-resources to ensure the IndexWriter always gets closed.
    try (final IndexWriter indexWriter = createIndexWriter(create, directory)) {
        final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
        final SpatialStrategy strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
        final ExtractionCallback extractionCallback = new ExtractionCallback() {

            @Override
            public void extracted(final GeoEntry newEntry) throws GeoEntryIndexingException {
                try {
                    addDocument(indexWriter, newEntry, strategy);
                } catch (IOException e) {
                    throw new GeoEntryIndexingException("Error writing to the index.", e);
                }
            }

            @Override
            public void updateProgress(final int progress) {
                if (progressCallback != null) {
                    progressCallback.updateProgress(progress);
                }
            }
        };
        try {
            geoEntryExtractor.pushGeoEntriesToExtractionCallback(resource, extractionCallback);
        } catch (GeoEntryExtractionException e) {
            // Need to roll back here before the IndexWriter is closed at the end of the try
            // block.
            indexWriter.rollback();
            throw e;
        }
    } catch (IOException e) {
        throw new GeoEntryIndexingException("Error writing to the index.", e);
    }
}
Also used : GeoEntry(org.codice.ddf.spatial.geocoding.GeoEntry) GeoEntryExtractionException(org.codice.ddf.spatial.geocoding.GeoEntryExtractionException) IndexWriter(org.apache.lucene.index.IndexWriter) RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) IOException(java.io.IOException) ExtractionCallback(org.codice.ddf.spatial.geocoding.GeoEntryExtractor.ExtractionCallback) SpatialStrategy(org.apache.lucene.spatial.SpatialStrategy) GeoEntryIndexingException(org.codice.ddf.spatial.geocoding.GeoEntryIndexingException) Directory(org.apache.lucene.store.Directory) FSDirectory(org.apache.lucene.store.FSDirectory) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)

Aggregations

SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)8 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)4 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)4 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)4 ValueSource (org.apache.lucene.queries.function.ValueSource)2 CompositeSpatialStrategy (org.apache.lucene.spatial.composite.CompositeSpatialStrategy)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 IOException (java.io.IOException)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 DoubleConstValueSource (org.apache.lucene.queries.function.valuesource.DoubleConstValueSource)1 MultiValueSource (org.apache.lucene.queries.function.valuesource.MultiValueSource)1 VectorValueSource (org.apache.lucene.queries.function.valuesource.VectorValueSource)1 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)1 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)1 SerializedDVStrategy (org.apache.lucene.spatial.serialized.SerializedDVStrategy)1 PointVectorStrategy (org.apache.lucene.spatial.vector.PointVectorStrategy)1 Directory (org.apache.lucene.store.Directory)1 FSDirectory (org.apache.lucene.store.FSDirectory)1 SolrDocument (org.apache.solr.common.SolrDocument)1 SolrException (org.apache.solr.common.SolrException)1