|
8 | 8 |
|
9 | 9 | package org.opensearch.geo.search;
|
10 | 10 |
|
| 11 | +import org.hamcrest.MatcherAssert; |
| 12 | +import org.junit.Before; |
11 | 13 | import org.opensearch.action.search.SearchResponse;
|
| 14 | +import org.opensearch.common.geo.GeoPoint; |
12 | 15 | import org.opensearch.geo.GeoModulePluginIntegTestCase;
|
| 16 | +import org.opensearch.geo.search.aggregations.common.GeoBoundsHelper; |
13 | 17 | import org.opensearch.geo.search.aggregations.metrics.GeoBounds;
|
14 | 18 | import org.opensearch.geo.tests.common.AggregationBuilders;
|
| 19 | +import org.opensearch.geo.tests.common.RandomGeoGenerator; |
| 20 | +import org.opensearch.geo.tests.common.RandomGeoGeometryGenerator; |
| 21 | +import org.opensearch.geometry.Geometry; |
| 22 | +import org.opensearch.geometry.utils.WellKnownText; |
15 | 23 | import org.opensearch.test.OpenSearchIntegTestCase;
|
16 | 24 |
|
17 | 25 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked;
|
18 | 26 | import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse;
|
19 | 27 | import static org.hamcrest.Matchers.closeTo;
|
20 | 28 |
|
| 29 | +/** |
| 30 | + * Tests to validate if user specified a missingValue in the input while doing the aggregation |
| 31 | + */ |
21 | 32 | @OpenSearchIntegTestCase.SuiteScopeTestCase
|
22 | 33 | public class MissingValueIT extends GeoModulePluginIntegTestCase {
|
23 | 34 |
|
| 35 | + private static final String INDEX_NAME = "idx"; |
| 36 | + private static final String GEO_SHAPE_FIELD_NAME = "myshape"; |
| 37 | + private static final String GEO_SHAPE_FIELD_TYPE = "type=geo_shape"; |
| 38 | + private static final String AGGREGATION_NAME = "bounds"; |
| 39 | + private static final String NON_EXISTENT_FIELD = "non_existing_field"; |
| 40 | + private static final WellKnownText WKT = WellKnownText.INSTANCE; |
| 41 | + private static Geometry indexedGeometry; |
| 42 | + private static GeoPoint indexedGeoPoint; |
| 43 | + private GeoPoint bottomRight; |
| 44 | + private GeoPoint topLeft; |
| 45 | + |
24 | 46 | @Override
|
25 | 47 | protected void setupSuiteScopeCluster() throws Exception {
|
26 |
| - assertAcked(prepareCreate("idx").setMapping("date", "type=date", "location", "type=geo_point", "str", "type=keyword").get()); |
| 48 | + assertAcked( |
| 49 | + prepareCreate(INDEX_NAME).setMapping( |
| 50 | + "date", |
| 51 | + "type=date", |
| 52 | + "location", |
| 53 | + "type=geo_point", |
| 54 | + "str", |
| 55 | + "type=keyword", |
| 56 | + GEO_SHAPE_FIELD_NAME, |
| 57 | + GEO_SHAPE_FIELD_TYPE |
| 58 | + ).get() |
| 59 | + ); |
| 60 | + indexedGeometry = RandomGeoGeometryGenerator.randomGeometry(random()); |
| 61 | + indexedGeoPoint = RandomGeoGenerator.randomPoint(random()); |
| 62 | + assert indexedGeometry != null; |
27 | 63 | indexRandom(
|
28 | 64 | true,
|
29 |
| - client().prepareIndex("idx").setId("1").setSource(), |
30 |
| - client().prepareIndex("idx") |
| 65 | + client().prepareIndex(INDEX_NAME).setId("1").setSource(), |
| 66 | + client().prepareIndex(INDEX_NAME) |
31 | 67 | .setId("2")
|
32 |
| - .setSource("str", "foo", "long", 3L, "double", 5.5, "date", "2015-05-07", "location", "1,2") |
| 68 | + .setSource( |
| 69 | + "str", |
| 70 | + "foo", |
| 71 | + "long", |
| 72 | + 3L, |
| 73 | + "double", |
| 74 | + 5.5, |
| 75 | + "date", |
| 76 | + "2015-05-07", |
| 77 | + "location", |
| 78 | + indexedGeoPoint.toString(), |
| 79 | + GEO_SHAPE_FIELD_NAME, |
| 80 | + WKT.toWKT(indexedGeometry) |
| 81 | + ) |
33 | 82 | );
|
34 | 83 | }
|
35 | 84 |
|
| 85 | + @Before |
| 86 | + public void runBeforeEachTest() { |
| 87 | + bottomRight = new GeoPoint(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY); |
| 88 | + topLeft = new GeoPoint(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); |
| 89 | + } |
| 90 | + |
36 | 91 | public void testUnmappedGeoBounds() {
|
37 |
| - SearchResponse response = client().prepareSearch("idx") |
38 |
| - .addAggregation(AggregationBuilders.geoBounds("bounds").field("non_existing_field").missing("2,1")) |
| 92 | + final GeoPoint missingGeoPoint = RandomGeoGenerator.randomPoint(random()); |
| 93 | + GeoBoundsHelper.updateBoundsBottomRight(missingGeoPoint, bottomRight); |
| 94 | + GeoBoundsHelper.updateBoundsTopLeft(missingGeoPoint, topLeft); |
| 95 | + SearchResponse response = client().prepareSearch(INDEX_NAME) |
| 96 | + .addAggregation( |
| 97 | + AggregationBuilders.geoBounds(AGGREGATION_NAME) |
| 98 | + .field(NON_EXISTENT_FIELD) |
| 99 | + .wrapLongitude(false) |
| 100 | + .missing(missingGeoPoint.toString()) |
| 101 | + ) |
39 | 102 | .get();
|
40 | 103 | assertSearchResponse(response);
|
41 |
| - GeoBounds bounds = response.getAggregations().get("bounds"); |
42 |
| - assertThat(bounds.bottomRight().lat(), closeTo(2.0, 1E-5)); |
43 |
| - assertThat(bounds.bottomRight().lon(), closeTo(1.0, 1E-5)); |
44 |
| - assertThat(bounds.topLeft().lat(), closeTo(2.0, 1E-5)); |
45 |
| - assertThat(bounds.topLeft().lon(), closeTo(1.0, 1E-5)); |
| 104 | + validateResult(response.getAggregations().get(AGGREGATION_NAME)); |
46 | 105 | }
|
47 | 106 |
|
48 | 107 | public void testGeoBounds() {
|
49 |
| - SearchResponse response = client().prepareSearch("idx") |
50 |
| - .addAggregation(AggregationBuilders.geoBounds("bounds").field("location").missing("2,1")) |
| 108 | + GeoBoundsHelper.updateBoundsForGeoPoint(indexedGeoPoint, topLeft, bottomRight); |
| 109 | + final GeoPoint missingGeoPoint = RandomGeoGenerator.randomPoint(random()); |
| 110 | + GeoBoundsHelper.updateBoundsForGeoPoint(missingGeoPoint, topLeft, bottomRight); |
| 111 | + SearchResponse response = client().prepareSearch(INDEX_NAME) |
| 112 | + .addAggregation( |
| 113 | + AggregationBuilders.geoBounds(AGGREGATION_NAME).field("location").wrapLongitude(false).missing(missingGeoPoint.toString()) |
| 114 | + ) |
51 | 115 | .get();
|
52 | 116 | assertSearchResponse(response);
|
53 |
| - GeoBounds bounds = response.getAggregations().get("bounds"); |
54 |
| - assertThat(bounds.bottomRight().lat(), closeTo(1.0, 1E-5)); |
55 |
| - assertThat(bounds.bottomRight().lon(), closeTo(2.0, 1E-5)); |
56 |
| - assertThat(bounds.topLeft().lat(), closeTo(2.0, 1E-5)); |
57 |
| - assertThat(bounds.topLeft().lon(), closeTo(1.0, 1E-5)); |
| 117 | + validateResult(response.getAggregations().get(AGGREGATION_NAME)); |
| 118 | + } |
| 119 | + |
| 120 | + public void testGeoBoundsWithMissingShape() { |
| 121 | + // create GeoBounds for the indexed Field |
| 122 | + GeoBoundsHelper.updateBoundsForGeometry(indexedGeometry, topLeft, bottomRight); |
| 123 | + final Geometry missingGeometry = RandomGeoGeometryGenerator.randomGeometry(random()); |
| 124 | + assert missingGeometry != null; |
| 125 | + GeoBoundsHelper.updateBoundsForGeometry(missingGeometry, topLeft, bottomRight); |
| 126 | + final SearchResponse response = client().prepareSearch(INDEX_NAME) |
| 127 | + .addAggregation( |
| 128 | + AggregationBuilders.geoBounds(AGGREGATION_NAME) |
| 129 | + .wrapLongitude(false) |
| 130 | + .field(GEO_SHAPE_FIELD_NAME) |
| 131 | + .missing(WKT.toWKT(missingGeometry)) |
| 132 | + ) |
| 133 | + .get(); |
| 134 | + assertSearchResponse(response); |
| 135 | + validateResult(response.getAggregations().get(AGGREGATION_NAME)); |
| 136 | + } |
| 137 | + |
| 138 | + public void testUnmappedGeoBoundsOnGeoShape() { |
| 139 | + // We cannot useGeometry other than Point as for GeoBoundsAggregation as the Default Value for the |
| 140 | + // CoreValueSourceType is GeoPoint hence we need to use Point here. |
| 141 | + final Geometry missingGeometry = RandomGeoGeometryGenerator.randomPoint(random()); |
| 142 | + final SearchResponse response = client().prepareSearch(INDEX_NAME) |
| 143 | + .addAggregation(AggregationBuilders.geoBounds(AGGREGATION_NAME).field(NON_EXISTENT_FIELD).missing(WKT.toWKT(missingGeometry))) |
| 144 | + .get(); |
| 145 | + GeoBoundsHelper.updateBoundsForGeometry(missingGeometry, topLeft, bottomRight); |
| 146 | + assertSearchResponse(response); |
| 147 | + validateResult(response.getAggregations().get(AGGREGATION_NAME)); |
| 148 | + } |
| 149 | + |
| 150 | + private void validateResult(final GeoBounds bounds) { |
| 151 | + MatcherAssert.assertThat(bounds.bottomRight().lat(), closeTo(bottomRight.lat(), GEOHASH_TOLERANCE)); |
| 152 | + MatcherAssert.assertThat(bounds.bottomRight().lon(), closeTo(bottomRight.lon(), GEOHASH_TOLERANCE)); |
| 153 | + MatcherAssert.assertThat(bounds.topLeft().lat(), closeTo(topLeft.lat(), GEOHASH_TOLERANCE)); |
| 154 | + MatcherAssert.assertThat(bounds.topLeft().lon(), closeTo(topLeft.lon(), GEOHASH_TOLERANCE)); |
58 | 155 | }
|
59 | 156 | }
|
0 commit comments