public void testNormalization() throws IOException { assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); GeoBoundingBoxQueryBuilder qb = createTestQueryBuilder(); if (getCurrentTypes().length != 0 && "mapped_geo".equals(qb.fieldName())) { // only execute this test if we are running on a valid geo field qb.setCorners(200, 200, qb.bottomRight().getLat(), qb.bottomRight().getLon()); qb.setValidationMethod(GeoValidationMethod.COERCE); Query query = qb.toQuery(createShardContext()); if (query instanceof ConstantScoreQuery) { ConstantScoreQuery result = (ConstantScoreQuery) query; BooleanQuery bboxFilter = (BooleanQuery) result.getQuery(); for (BooleanClause clause : bboxFilter.clauses()) { LegacyNumericRangeQuery boundary = (LegacyNumericRangeQuery) clause.getQuery(); if (boundary.getMax() != null) { assertTrue( "If defined, non of the maximum range values should be larger than 180", boundary.getMax().intValue() <= 180); } } } else { assertTrue( "memory queries should result in LegacyInMemoryGeoBoundingBoxQuery", query instanceof LegacyInMemoryGeoBoundingBoxQuery); } } }
public void testLeftRightCanBeFlipped() { GeoBoundingBoxQueryBuilder builder = createTestQueryBuilder(); double top = builder.topLeft().getLat(); double left = builder.topLeft().getLon(); double bottom = builder.bottomRight().getLat(); double right = builder.bottomRight().getLon(); builder .setValidationMethod(GeoValidationMethod.IGNORE_MALFORMED) .setCorners(top, right, bottom, left); builder.setValidationMethod(GeoValidationMethod.STRICT).setCorners(top, right, bottom, left); }
public void testTopBottomCanBeFlippedOnIgnoreMalformed() { GeoBoundingBoxQueryBuilder builder = createTestQueryBuilder(); double top = builder.topLeft().getLat(); double left = builder.topLeft().getLon(); double bottom = builder.bottomRight().getLat(); double right = builder.bottomRight().getLon(); assumeTrue("top should not be equal to bottom for flip check", top != bottom); builder .setValidationMethod(GeoValidationMethod.IGNORE_MALFORMED) .setCorners(bottom, left, top, right); }
public void testTopBottomCannotBeFlipped() { GeoBoundingBoxQueryBuilder builder = createTestQueryBuilder(); double top = builder.topLeft().getLat(); double left = builder.topLeft().getLon(); double bottom = builder.bottomRight().getLat(); double right = builder.bottomRight().getLon(); assumeTrue("top should not be equal to bottom for flip check", top != bottom); logger.info("top: {} bottom: {}", top, bottom); builder.setValidationMethod(GeoValidationMethod.STRICT); IllegalArgumentException e = expectThrows( IllegalArgumentException.class, () -> builder.setCorners(bottom, left, top, right)); assertThat(e.getMessage(), containsString("top is below bottom corner:")); }
public void testFromJson() throws IOException { String json = "{\n" + " \"geo_bounding_box\" : {\n" + " \"pin.location\" : {\n" + " \"top_left\" : [ -74.1, 40.73 ],\n" + " \"bottom_right\" : [ -71.12, 40.01 ]\n" + " },\n" + " \"validation_method\" : \"STRICT\",\n" + " \"type\" : \"MEMORY\",\n" + " \"ignore_unmapped\" : false,\n" + " \"boost\" : 1.0\n" + " }\n" + "}"; GeoBoundingBoxQueryBuilder parsed = (GeoBoundingBoxQueryBuilder) parseQuery(json); checkGeneratedJson(json, parsed); assertEquals(json, "pin.location", parsed.fieldName()); assertEquals(json, -74.1, parsed.topLeft().getLon(), 0.0001); assertEquals(json, 40.73, parsed.topLeft().getLat(), 0.0001); assertEquals(json, -71.12, parsed.bottomRight().getLon(), 0.0001); assertEquals(json, 40.01, parsed.bottomRight().getLat(), 0.0001); assertEquals(json, 1.0, parsed.boost(), 0.0001); assertEquals(json, GeoExecType.MEMORY, parsed.type()); }
@Override public void fillIn(double coordinate, GeoBoundingBoxQueryBuilder qb) { qb.setCorners( qb.topLeft().getLat(), qb.topLeft().getLon(), qb.bottomRight().getLat(), coordinate); }