Search in sources :

Example 1 with SpatialOperation

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

the class TermQueryPrefixTreeStrategy method makeQuery.

@Override
public Query makeQuery(SpatialArgs args) {
    final SpatialOperation op = args.getOperation();
    if (op != SpatialOperation.Intersects)
        throw new UnsupportedSpatialOperation(op);
    Shape shape = args.getShape();
    int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct));
    //--get a List of BytesRef for each term we want (no parents, no leaf bytes))
    final int GUESS_NUM_TERMS;
    if (shape instanceof Point)
        //perfect guess
        GUESS_NUM_TERMS = detailLevel;
    else
        //should this be a method on SpatialPrefixTree?
        GUESS_NUM_TERMS = 4096;
    //shared byte array for all terms
    BytesRefBuilder masterBytes = new BytesRefBuilder();
    List<BytesRef> terms = new ArrayList<>(GUESS_NUM_TERMS);
    CellIterator cells = grid.getTreeCellIterator(shape, detailLevel);
    while (cells.hasNext()) {
        Cell cell = cells.next();
        if (!cell.isLeaf())
            continue;
        //null because we want a new BytesRef
        BytesRef term = cell.getTokenBytesNoLeaf(null);
        //We copy out the bytes because it may be re-used across the iteration. This also gives us the opportunity
        // to use one contiguous block of memory for the bytes of all terms we need.
        masterBytes.grow(masterBytes.length() + term.length);
        masterBytes.append(term);
        //don't need; will reset later
        term.bytes = null;
        term.offset = masterBytes.length() - term.length;
        terms.add(term);
    }
    //doing this now because if we did earlier, it's possible the bytes needed to grow()
    for (BytesRef byteRef : terms) {
        byteRef.bytes = masterBytes.bytes();
    }
    //TODO an automatonQuery might be faster?
    return new TermInSetQuery(getFieldName(), terms);
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) Shape(org.locationtech.spatial4j.shape.Shape) TermInSetQuery(org.apache.lucene.search.TermInSetQuery) ArrayList(java.util.ArrayList) CellIterator(org.apache.lucene.spatial.prefix.tree.CellIterator) Point(org.locationtech.spatial4j.shape.Point) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) Cell(org.apache.lucene.spatial.prefix.tree.Cell) Point(org.locationtech.spatial4j.shape.Point) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with SpatialOperation

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

the class CompositeStrategyTest method testOperations.

@Test
@Repeat(iterations = 20)
public void testOperations() throws IOException {
    //setup
    if (randomBoolean()) {
        setupQuadGrid(-1);
    } else {
        setupGeohashGrid(-1);
    }
    SerializedDVStrategy serializedDVStrategy = new SerializedDVStrategy(ctx, getClass().getSimpleName() + "_sdv");
    this.strategy = new CompositeSpatialStrategy("composite_" + getClass().getSimpleName(), rptStrategy, serializedDVStrategy);
    for (SpatialOperation pred : SpatialOperation.values()) {
        if (pred == SpatialOperation.BBoxIntersects || pred == SpatialOperation.BBoxWithin) {
            continue;
        }
        if (pred == SpatialOperation.IsDisjointTo) {
            //TODO
            continue;
        }
        testOperationRandomShapes(pred);
        deleteAll();
        commit();
    }
}
Also used : SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) SerializedDVStrategy(org.apache.lucene.spatial.serialized.SerializedDVStrategy) Test(org.junit.Test) Repeat(com.carrotsearch.randomizedtesting.annotations.Repeat)

Example 3 with SpatialOperation

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

the class PortedSolr3Test method _checkHits.

private void _checkHits(boolean bbox, Point pt, double distKM, int assertNumFound, int... assertIds) {
    SpatialOperation op = SpatialOperation.Intersects;
    double distDEG = DistanceUtils.dist2Degrees(distKM, DistanceUtils.EARTH_MEAN_RADIUS_KM);
    Shape shape = ctx.makeCircle(pt, distDEG);
    if (bbox)
        shape = shape.getBoundingBox();
    SpatialArgs args = new SpatialArgs(op, shape);
    //args.setDistPrecision(0.025);
    Query query = strategy.makeQuery(args);
    SearchResults results = executeQuery(query, 100);
    assertEquals("" + shape, assertNumFound, results.numFound);
    if (assertIds != null) {
        Set<Integer> resultIds = new HashSet<>();
        for (SearchResult result : results.results) {
            resultIds.add(Integer.valueOf(result.document.get("id")));
        }
        for (int assertId : assertIds) {
            assertTrue("has " + assertId, resultIds.contains(assertId));
        }
    }
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation) Point(org.locationtech.spatial4j.shape.Point) HashSet(java.util.HashSet)

Example 4 with SpatialOperation

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

the class BBoxStrategy method makeQuery.

//---------------------------------
// Query Building
//---------------------------------
//  Utility on SpatialStrategy?
//  public Query makeQueryWithValueSource(SpatialArgs args, ValueSource valueSource) {
//    return new CustomScoreQuery(makeQuery(args), new FunctionQuery(valueSource));
//or...
//  return new BooleanQuery.Builder()
//      .add(new FunctionQuery(valueSource), BooleanClause.Occur.MUST)//matches everything and provides score
//      .add(filterQuery, BooleanClause.Occur.FILTER)//filters (score isn't used)
//  .build();
//  }
@Override
public Query makeQuery(SpatialArgs args) {
    Shape shape = args.getShape();
    if (!(shape instanceof Rectangle))
        throw new UnsupportedOperationException("Can only query by Rectangle, not " + shape);
    Rectangle bbox = (Rectangle) shape;
    Query spatial;
    // Useful for understanding Relations:
    // http://edndoc.esri.com/arcsde/9.1/general_topics/understand_spatial_relations.htm
    SpatialOperation op = args.getOperation();
    if (op == SpatialOperation.BBoxIntersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.BBoxWithin)
        spatial = makeWithin(bbox);
    else if (op == SpatialOperation.Contains)
        spatial = makeContains(bbox);
    else if (op == SpatialOperation.Intersects)
        spatial = makeIntersects(bbox);
    else if (op == SpatialOperation.IsEqualTo)
        spatial = makeEquals(bbox);
    else if (op == SpatialOperation.IsDisjointTo)
        spatial = makeDisjoint(bbox);
    else if (op == SpatialOperation.IsWithin)
        spatial = makeWithin(bbox);
    else {
        //no Overlaps support yet
        throw new UnsupportedSpatialOperation(op);
    }
    return new ConstantScoreQuery(spatial);
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) Query(org.apache.lucene.search.Query) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) Rectangle(org.locationtech.spatial4j.shape.Rectangle) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Example 5 with SpatialOperation

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

the class RecursivePrefixTreeStrategy method makeQuery.

@Override
public Query makeQuery(SpatialArgs args) {
    final SpatialOperation op = args.getOperation();
    Shape shape = args.getShape();
    int detailLevel = grid.getLevelForDistance(args.resolveDistErr(ctx, distErrPct));
    if (op == SpatialOperation.Intersects) {
        if (isGridAlignedShape(args.getShape())) {
            return makeGridShapeIntersectsQuery(args.getShape());
        }
        return new IntersectsPrefixTreeQuery(shape, getFieldName(), grid, detailLevel, prefixGridScanLevel);
    } else if (op == SpatialOperation.IsWithin) {
        return new WithinPrefixTreeQuery(shape, getFieldName(), grid, detailLevel, prefixGridScanLevel, //-1 flag is slower but ensures correct results
        -1);
    } else if (op == SpatialOperation.Contains) {
        return new ContainsPrefixTreeQuery(shape, getFieldName(), grid, detailLevel, multiOverlappingIndexedShapes);
    }
    throw new UnsupportedSpatialOperation(op);
}
Also used : UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) Shape(org.locationtech.spatial4j.shape.Shape) UnsupportedSpatialOperation(org.apache.lucene.spatial.query.UnsupportedSpatialOperation) SpatialOperation(org.apache.lucene.spatial.query.SpatialOperation)

Aggregations

SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)12 Shape (org.locationtech.spatial4j.shape.Shape)8 SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)6 UnsupportedSpatialOperation (org.apache.lucene.spatial.query.UnsupportedSpatialOperation)6 Query (org.apache.lucene.search.Query)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 TermQuery (org.apache.lucene.search.TermQuery)3 Point (org.locationtech.spatial4j.shape.Point)3 Rectangle (org.locationtech.spatial4j.shape.Rectangle)3 Repeat (com.carrotsearch.randomizedtesting.annotations.Repeat)2 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)2 Test (org.junit.Test)2 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashSet (java.util.HashSet)1 UUID (java.util.UUID)1 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)1 Supplier (org.apache.logging.log4j.util.Supplier)1 DoublePoint (org.apache.lucene.document.DoublePoint)1