/** * Require at least one of the targeted fields to point to a location within the given box (~rectangle). * * @param topLeftLatitude The latitude of the top-left corner of the box. * @param topLeftLongitude The longitude of the top-left corner of the box. * @param bottomRightLatitude The latitude of the bottom-right corner of the box. * @param bottomRightLongitude The longitude of the bottom-right corner of the box. * @return A context allowing to get the resulting predicate. */ default SearchPredicateTerminalContext boundingBox(double topLeftLatitude, double topLeftLongitude, double bottomRightLatitude, double bottomRightLongitude) { return boundingBox( GeoBoundingBox.of( topLeftLatitude, topLeftLongitude, bottomRightLatitude, bottomRightLongitude ) ); } }
/** * Require at least one of the targeted fields to point to a location within the given box (~rectangle). * * @param topLeftLatitude The latitude of the top-left corner of the box. * @param topLeftLongitude The longitude of the top-left corner of the box. * @param bottomRightLatitude The latitude of the bottom-right corner of the box. * @param bottomRightLongitude The longitude of the bottom-right corner of the box. * @return A context allowing to get the resulting predicate. */ default SearchPredicateTerminalContext boundingBox(double topLeftLatitude, double topLeftLongitude, double bottomRightLatitude, double bottomRightLongitude) { return boundingBox( GeoBoundingBox.of( topLeftLatitude, topLeftLongitude, bottomRightLatitude, bottomRightLongitude ) ); } }
@Test public void boundingBox_error_null() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SubTest.expectException( "spatial().within().boundingBox() predicate with null bounding box", () -> searchTarget.predicate().spatial().within().onField( "geoPoint" ).boundingBox( null ) ) .assertThrown() .isInstanceOf( IllegalArgumentException.class ) .hasMessageContaining( "HSEARCH900000" ); }
@Test public void unknown_field() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SubTest.expectException( "spatial().within().boundingBox() predicate on unknown field", () -> searchTarget.predicate().spatial().within().onField( "unknown_field" ) .boundingBox( BOUNDING_BOX_1 ).toPredicate() ) .assertThrown() .isInstanceOf( SearchException.class ) .hasMessageContaining( "Unknown field" ) .hasMessageContaining( "'unknown_field'" ); }
@Test public void unsupported_field_types() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SubTest.expectException( "spatial().within().boundingBox() predicate on field with unsupported type", () -> searchTarget.predicate().spatial().within().onField( "string" ).boundingBox( BOUNDING_BOX_1 ) ) .assertThrown() .isInstanceOf( SearchException.class ) .hasMessageContaining( "Spatial predicates are not supported by" ) .satisfies( FailureReportUtils.hasContext( EventContexts.fromIndexFieldAbsolutePath( "string" ) ) ); }
@Test public void boundingBox_consistency() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.spatial().within().onField( "geoPoint" ) .boundingBox( GeoBoundingBox.of( GeoPoint.of( 25, 23 ), GeoPoint.of( 24, 26 ) ) ) .toPredicate() ) .build(); assertThat( query ) .hasDocRefHitsAnyOrder( INDEX_NAME, ADDITIONAL_POINT_1_ID, ADDITIONAL_POINT_2_ID ); }
.predicate( f -> f.spatial().within().onField( "geoPoint" ).orField( "geoPoint_1" ).boundingBox( BOUNDING_BOX_1 ).toPredicate() ) .build(); .predicate( f -> f.spatial().within().onField( "geoPoint" ).orField( "geoPoint_1" ).boundingBox( BOUNDING_BOX_2_1 ).toPredicate() ) .build(); .asReference() .predicate( f -> f.spatial().within().onField( "geoPoint" ).orFields( "geoPoint_1" ).orFields( "geoPoint_2" ) .boundingBox( BOUNDING_BOX_2 ) .toPredicate() .asReference() .predicate( f -> f.spatial().within().onField( "geoPoint" ).orFields( "geoPoint_1" ).orFields( "geoPoint_2" ) .boundingBox( BOUNDING_BOX_1_1 ) .toPredicate() .asReference() .predicate( f -> f.spatial().within().onField( "geoPoint" ).orFields( "geoPoint_1" ).orFields( "geoPoint_2" ) .boundingBox( BOUNDING_BOX_2_2 ) .toPredicate() .predicate( f -> f.spatial().within().onFields( "geoPoint", "geoPoint_2" ).boundingBox( BOUNDING_BOX_2 ).toPredicate() ) .build(); .predicate( f -> f.spatial().within().onFields( "geoPoint", "geoPoint_2" ).boundingBox( BOUNDING_BOX_1_2 ).toPredicate() ) .build();
.predicate( f -> f.spatial().within().onField( "geoPoint" ).boundingBox( BOUNDING_BOX_2 ).toPredicate() ) .build(); .predicate( f -> f.spatial().within().onField( "geoPoint" ).boundingBox( BOUNDING_BOX_1 ).toPredicate() ) .build(); .asReference() .predicate( f -> f.spatial().within().onField( "geoPoint" ) .boundingBox( BOUNDING_BOX_2.getTopLeft().getLatitude(), BOUNDING_BOX_2.getTopLeft().getLongitude(), BOUNDING_BOX_2.getBottomRight().getLatitude(), BOUNDING_BOX_2.getBottomRight().getLongitude() ) .toPredicate() .asReference() .predicate( f -> f.spatial().within().onField( "geoPoint" ) .boundingBox( BOUNDING_BOX_1.getTopLeft().getLatitude(), BOUNDING_BOX_1.getTopLeft().getLongitude(), BOUNDING_BOX_1.getBottomRight().getLatitude(), BOUNDING_BOX_1.getBottomRight().getLongitude() ) .toPredicate()
@Test public void boost() { StubMappingSearchTarget searchTarget = indexManager.createSearchTarget(); SearchQuery<DocumentReference> query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.spatial().within().onField( "geoPoint" ).boundingBox( CHEZ_MARGOTTE_BOUNDING_BOX ) ) .should( f.match().onField( "string" ).boostedTo( 42 ).matching( OURSON_QUI_BOIT_STRING ) ) .toPredicate() ) .sort( c -> c.byScore() ) .build(); assertThat( query ) .hasDocRefHitsExactOrder( INDEX_NAME, OURSON_QUI_BOIT_ID, CHEZ_MARGOTTE_ID ); query = searchTarget.query() .asReference() .predicate( f -> f.bool() .should( f.spatial().within().onField( "geoPoint" ).boostedTo( 42 ).boundingBox( CHEZ_MARGOTTE_BOUNDING_BOX ) ) .should( f.match().onField( "string" ).matching( OURSON_QUI_BOIT_STRING ) ) .toPredicate() ) .sort( c -> c.byScore() ) .build(); assertThat( query ) .hasDocRefHitsExactOrder( INDEX_NAME, CHEZ_MARGOTTE_ID, OURSON_QUI_BOIT_ID ); }