Search in sources :

Example 6 with SpatialPrefixTree

use of org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree in project lucene-solr by apache.

the class CompositeSpatialStrategy method makeQuery.

@Override
public Query makeQuery(SpatialArgs args) {
    final SpatialOperation pred = args.getOperation();
    if (pred == SpatialOperation.BBoxIntersects || pred == SpatialOperation.BBoxWithin) {
        throw new UnsupportedSpatialOperation(pred);
    }
    if (pred == SpatialOperation.IsDisjointTo) {
        // update class docs when it's added.
        throw new UnsupportedSpatialOperation(pred);
    }
    final ShapePredicateValueSource predicateValueSource = new ShapePredicateValueSource(geometryStrategy.makeShapeValueSource(), pred, args.getShape());
    //System.out.println("PredOpt: " + optimizePredicates);
    if (pred == SpatialOperation.Intersects && optimizePredicates) {
        // We have a smart Intersects impl
        final SpatialPrefixTree grid = indexStrategy.getGrid();
        //default to max precision
        final int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, 0.0));
        return new IntersectsRPTVerifyQuery(args.getShape(), indexStrategy.getFieldName(), grid, detailLevel, indexStrategy.getPrefixGridScanLevel(), predicateValueSource);
    } else {
        //The general path; all index matches get verified
        SpatialArgs indexArgs;
        if (pred == SpatialOperation.Contains) {
            // note: we could map IsWithin as well but it's pretty darned slow since it touches all world grids
            indexArgs = args;
        } else {
            //TODO add args.clone method with new predicate? Or simply make non-final?
            indexArgs = new SpatialArgs(SpatialOperation.Intersects, args.getShape());
            indexArgs.setDistErr(args.getDistErr());
            indexArgs.setDistErrPct(args.getDistErrPct());
        }
        if (indexArgs.getDistErr() == null && indexArgs.getDistErrPct() == null) {
            indexArgs.setDistErrPct(0.10);
        }
        final Query indexQuery = indexStrategy.makeQuery(indexArgs);
        return new CompositeVerifyQuery(indexQuery, predicateValueSource);
    }
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Query(org.apache.lucene.search.Query) ShapePredicateValueSource(org.apache.lucene.spatial.util.ShapePredicateValueSource) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) Point(org.locationtech.spatial4j.shape.Point)

Example 7 with SpatialPrefixTree

use of org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree in project lucene-solr by apache.

the class JtsPolygonTest method testBadPrefixTreePrune.

/**
   * A PrefixTree pruning optimization gone bad.
   * See <a href="https://issues.apache.org/jira/browse/LUCENE-4770">LUCENE-4770</a>.
   */
@Test
public void testBadPrefixTreePrune() throws Exception {
    Shape area = ctx.readShapeFromWkt("POLYGON((-122.83 48.57, -122.77 48.56, -122.79 48.53, -122.83 48.57))");
    SpatialPrefixTree trie = new QuadPrefixTree(ctx, 12);
    TermQueryPrefixTreeStrategy strategy = new TermQueryPrefixTreeStrategy(trie, "geo");
    Document doc = new Document();
    doc.add(new TextField("id", "1", Store.YES));
    Field[] fields = strategy.createIndexableFields(area, 0.025);
    for (Field field : fields) {
        doc.add(field);
    }
    addDocument(doc);
    Point upperleft = ctx.makePoint(-122.88, 48.54);
    Point lowerright = ctx.makePoint(-122.82, 48.62);
    Query query = strategy.makeQuery(new SpatialArgs(SpatialOperation.Intersects, ctx.makeRectangle(upperleft, lowerright)));
    commit();
    TopDocs search = indexSearcher.search(query, 10);
    ScoreDoc[] scoreDocs = search.scoreDocs;
    for (ScoreDoc scoreDoc : scoreDocs) {
        System.out.println(indexSearcher.doc(scoreDoc.doc));
    }
    assertEquals(1, search.totalHits);
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) Point(org.locationtech.spatial4j.shape.Point) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) QuadPrefixTree(org.apache.lucene.spatial.prefix.tree.QuadPrefixTree) TextField(org.apache.lucene.document.TextField) Test(org.junit.Test)

Example 8 with SpatialPrefixTree

use of org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree in project ddf by codice.

the class TestGeoNamesQueryLuceneIndex method setUp.

@Before
public void setUp() throws IOException {
    directoryIndex = spy(new GeoNamesQueryLuceneDirectoryIndex());
    directoryIndex.setIndexLocation(null);
    final SpatialPrefixTree grid = new GeohashPrefixTree(SPATIAL_CONTEXT, GeoNamesLuceneConstants.GEOHASH_LEVELS);
    strategy = new RecursivePrefixTreeStrategy(grid, GeoNamesLuceneConstants.GEO_FIELD);
    initializeIndex();
    doReturn(directory).when(directoryIndex).openDirectory();
}
Also used : RecursivePrefixTreeStrategy(org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy) SpatialPrefixTree(org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree) GeohashPrefixTree(org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree) Before(org.junit.Before)

Example 9 with SpatialPrefixTree

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

use of org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree 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)

Aggregations

SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)14 RecursivePrefixTreeStrategy (org.apache.lucene.spatial.prefix.RecursivePrefixTreeStrategy)10 GeohashPrefixTree (org.apache.lucene.spatial.prefix.tree.GeohashPrefixTree)9 Point (org.locationtech.spatial4j.shape.Point)5 SpatialStrategy (org.apache.lucene.spatial.SpatialStrategy)4 QuadPrefixTree (org.apache.lucene.spatial.prefix.tree.QuadPrefixTree)4 ArrayList (java.util.ArrayList)3 TermQueryPrefixTreeStrategy (org.apache.lucene.spatial.prefix.TermQueryPrefixTreeStrategy)3 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)3 SpatialContext (org.locationtech.spatial4j.context.SpatialContext)3 ParametersFactory (com.carrotsearch.randomizedtesting.annotations.ParametersFactory)2 IOException (java.io.IOException)2 Query (org.apache.lucene.search.Query)2 Cell (org.apache.lucene.spatial.prefix.tree.Cell)2 PackedQuadPrefixTree (org.apache.lucene.spatial.prefix.tree.PackedQuadPrefixTree)2 SerializedDVStrategy (org.apache.lucene.spatial.serialized.SerializedDVStrategy)2 GeoEntry (org.codice.ddf.spatial.geocoding.GeoEntry)2 Test (org.junit.Test)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1