Search in sources :

Example 11 with SpatialArgs

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

the class TestPointVectorStrategy method testFieldOptions.

@Test
public void testFieldOptions() throws IOException, ParseException {
    // It's not stored; test it isn't.
    this.strategy = PointVectorStrategy.newInstance(ctx, getClass().getSimpleName());
    adoc("99", "POINT(-5.0 8.2)");
    commit();
    SearchResults results = executeQuery(new MatchAllDocsQuery(), 1);
    Document document = results.results.get(0).document;
    assertNull("not stored", document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_X));
    assertNull("not stored", document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_Y));
    deleteAll();
    // Now we mark it stored.  We also disable pointvalues...
    FieldType fieldType = new FieldType(PointVectorStrategy.DEFAULT_FIELDTYPE);
    fieldType.setStored(true);
    //disable point values
    fieldType.setDimensions(0, 0);
    this.strategy = new PointVectorStrategy(ctx, getClass().getSimpleName(), fieldType);
    adoc("99", "POINT(-5.0 8.2)");
    commit();
    results = executeQuery(new MatchAllDocsQuery(), 1);
    document = results.results.get(0).document;
    assertEquals("stored", -5.0, document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_X).numericValue());
    assertEquals("stored", 8.2, document.getField(strategy.getFieldName() + PointVectorStrategy.SUFFIX_Y).numericValue());
    // Test a query fails without point values
    expectThrows(UnsupportedOperationException.class, () -> {
        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeRectangle(-10.0, 10.0, -5.0, 5.0));
        this.strategy.makeQuery(args);
    });
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Document(org.apache.lucene.document.Document) FieldType(org.apache.lucene.document.FieldType) Test(org.junit.Test)

Example 12 with SpatialArgs

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

the class StrategyTestCase method testOperation.

protected void testOperation(Shape indexedShape, SpatialOperation operation, Shape queryShape, boolean match) throws IOException {
    assertTrue("Faulty test", operation.evaluate(indexedShape, queryShape) == match || indexedShape.equals(queryShape) && (operation == SpatialOperation.Contains || operation == SpatialOperation.IsWithin));
    adoc("0", indexedShape);
    commit();
    Query query = strategy.makeQuery(new SpatialArgs(operation, queryShape));
    SearchResults got = executeQuery(query, 1);
    assert got.numFound <= 1 : "unclean test env";
    if ((got.numFound == 1) != match)
        fail(operation + " I:" + indexedShape + " Q:" + queryShape);
    //clean up after ourselves
    deleteAll();
}
Also used : SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) TermQuery(org.apache.lucene.search.TermQuery)

Example 13 with SpatialArgs

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

the class SpatialExample method search.

private void search() throws Exception {
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    Sort idSort = new Sort(new SortField("id", SortField.Type.INT));
    //--Filter by circle (<= distance from a point)
    {
        //Search with circle
        //note: SpatialArgs can be parsed from a string
        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeCircle(-80.0, 33.0, DistanceUtils.dist2Degrees(200, DistanceUtils.EARTH_MEAN_RADIUS_KM)));
        Query query = strategy.makeQuery(args);
        TopDocs docs = indexSearcher.search(query, 10, idSort);
        assertDocMatchedIds(indexSearcher, docs, 2);
        //Now, lets get the distance for the 1st doc via computing from stored point value:
        // (this computation is usually not redundant)
        Document doc1 = indexSearcher.doc(docs.scoreDocs[0].doc);
        String doc1Str = doc1.getField(strategy.getFieldName()).stringValue();
        //assume doc1Str is "x y" as written in newSampleDocument()
        int spaceIdx = doc1Str.indexOf(' ');
        double x = Double.parseDouble(doc1Str.substring(0, spaceIdx));
        double y = Double.parseDouble(doc1Str.substring(spaceIdx + 1));
        double doc1DistDEG = ctx.calcDistance(args.getShape().getCenter(), x, y);
        assertEquals(121.6d, DistanceUtils.degrees2Dist(doc1DistDEG, DistanceUtils.EARTH_MEAN_RADIUS_KM), 0.1);
        //or more simply:
        assertEquals(121.6d, doc1DistDEG * DistanceUtils.DEG_TO_KM, 0.1);
    }
    //--Match all, order by distance ascending
    {
        Point pt = ctx.makePoint(60, -50);
        //the distance (in km)
        ValueSource valueSource = strategy.makeDistanceValueSource(pt, DistanceUtils.DEG_TO_KM);
        //false=asc dist
        Sort distSort = new Sort(valueSource.getSortField(false)).rewrite(indexSearcher);
        TopDocs docs = indexSearcher.search(new MatchAllDocsQuery(), 10, distSort);
        assertDocMatchedIds(indexSearcher, docs, 4, 20, 2);
    //To get the distance, we could compute from stored values like earlier.
    // However in this example we sorted on it, and the distance will get
    // computed redundantly.  If the distance is only needed for the top-X
    // search results then that's not a big deal. Alternatively, try wrapping
    // the ValueSource with CachingDoubleValueSource then retrieve the value
    // from the ValueSource now. See LUCENE-4541 for an example.
    }
    //demo arg parsing
    {
        SpatialArgs args = new SpatialArgs(SpatialOperation.Intersects, ctx.makeCircle(-80.0, 33.0, 1));
        SpatialArgs args2 = new SpatialArgsParser().parse("Intersects(BUFFER(POINT(-80 33),1))", ctx);
        assertEquals(args.toString(), args2.toString());
    }
    indexReader.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SpatialArgsParser(org.apache.lucene.spatial.query.SpatialArgsParser) SortField(org.apache.lucene.search.SortField) Point(org.locationtech.spatial4j.shape.Point) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TopDocs(org.apache.lucene.search.TopDocs) ValueSource(org.apache.lucene.queries.function.ValueSource) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort)

Example 14 with SpatialArgs

use of org.apache.lucene.spatial.query.SpatialArgs 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 15 with SpatialArgs

use of org.apache.lucene.spatial.query.SpatialArgs 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)

Aggregations

SpatialArgs (org.apache.lucene.spatial.query.SpatialArgs)34 Query (org.apache.lucene.search.Query)16 Shape (org.locationtech.spatial4j.shape.Shape)14 Test (org.junit.Test)12 Point (org.locationtech.spatial4j.shape.Point)10 SpatialOperation (org.apache.lucene.spatial.query.SpatialOperation)6 Document (org.apache.lucene.document.Document)5 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 TopDocs (org.apache.lucene.search.TopDocs)4 SpatialPrefixTree (org.apache.lucene.spatial.prefix.tree.SpatialPrefixTree)3 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Analyzer (org.apache.lucene.analysis.Analyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 Field (org.apache.lucene.document.Field)2 TextField (org.apache.lucene.document.TextField)2 IndexReader (org.apache.lucene.index.IndexReader)2 Term (org.apache.lucene.index.Term)2 BooleanFilter (org.apache.lucene.queries.BooleanFilter)2