26
26
import org .apache .lucene .search .IndexSearcher ;
27
27
import org .apache .lucene .search .MatchAllDocsQuery ;
28
28
import org .apache .lucene .store .Directory ;
29
+ import org .elasticsearch .ElasticsearchParseException ;
29
30
import org .elasticsearch .common .geo .GeoPoint ;
31
+ import org .elasticsearch .geo .GeometryTestUtils ;
32
+ import org .elasticsearch .geometry .Point ;
30
33
import org .elasticsearch .index .mapper .GeoPointFieldMapper ;
31
34
import org .elasticsearch .index .mapper .MappedFieldType ;
35
+ import org .elasticsearch .search .aggregations .AggregationBuilder ;
32
36
import org .elasticsearch .search .aggregations .AggregatorTestCase ;
33
37
import org .elasticsearch .search .aggregations .support .AggregationInspectionHelper ;
38
+ import org .elasticsearch .search .aggregations .support .CoreValuesSourceType ;
39
+ import org .elasticsearch .search .aggregations .support .ValuesSourceType ;
34
40
import org .elasticsearch .test .geo .RandomGeoGenerator ;
35
41
42
+ import java .util .List ;
43
+
36
44
import static org .elasticsearch .search .aggregations .metrics .InternalGeoBoundsTests .GEOHASH_TOLERANCE ;
37
45
import static org .hamcrest .Matchers .closeTo ;
46
+ import static org .hamcrest .Matchers .equalTo ;
47
+ import static org .hamcrest .Matchers .startsWith ;
38
48
39
49
public class GeoBoundsAggregatorTests extends AggregatorTestCase {
40
- public void testEmpty () throws Exception {
41
- try (Directory dir = newDirectory ();
42
- RandomIndexWriter w = new RandomIndexWriter (random (), dir )) {
50
+ public void testUnmappedWithNoDocs () throws Exception {
51
+ try (Directory dir = newDirectory (); RandomIndexWriter w = new RandomIndexWriter (random (), dir )) {
43
52
GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder ("my_agg" )
44
53
.field ("field" )
45
54
.wrapLongitude (false );
@@ -61,6 +70,83 @@ public void testEmpty() throws Exception {
61
70
}
62
71
}
63
72
73
+ public void testUnmappedFieldWithDocs () throws Exception {
74
+ try (Directory dir = newDirectory (); RandomIndexWriter w = new RandomIndexWriter (random (), dir )) {
75
+ if (randomBoolean ()) {
76
+ Document doc = new Document ();
77
+ doc .add (new LatLonDocValuesField ("field" , 0.0 , 0.0 ));
78
+ w .addDocument (doc );
79
+ }
80
+
81
+ GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder ("my_agg" )
82
+ .field ("non_existent" )
83
+ .wrapLongitude (false );
84
+
85
+ MappedFieldType fieldType = new GeoPointFieldMapper .GeoPointFieldType ();
86
+ fieldType .setHasDocValues (true );
87
+ fieldType .setName ("field" );
88
+ try (IndexReader reader = w .getReader ()) {
89
+ IndexSearcher searcher = new IndexSearcher (reader );
90
+ InternalGeoBounds bounds = search (searcher , new MatchAllDocsQuery (), aggBuilder , fieldType );
91
+ assertTrue (Double .isInfinite (bounds .top ));
92
+ assertTrue (Double .isInfinite (bounds .bottom ));
93
+ assertTrue (Double .isInfinite (bounds .posLeft ));
94
+ assertTrue (Double .isInfinite (bounds .posRight ));
95
+ assertTrue (Double .isInfinite (bounds .negLeft ));
96
+ assertTrue (Double .isInfinite (bounds .negRight ));
97
+ assertFalse (AggregationInspectionHelper .hasValue (bounds ));
98
+ }
99
+ }
100
+ }
101
+
102
+ public void testMissing () throws Exception {
103
+ try (Directory dir = newDirectory (); RandomIndexWriter w = new RandomIndexWriter (random (), dir )) {
104
+ Document doc = new Document ();
105
+ doc .add (new LatLonDocValuesField ("not_field" , 0.0 , 0.0 ));
106
+ w .addDocument (doc );
107
+
108
+ MappedFieldType fieldType = new GeoPointFieldMapper .GeoPointFieldType ();
109
+ fieldType .setHasDocValues (true );
110
+ fieldType .setName ("field" );
111
+
112
+ Point point = GeometryTestUtils .randomPoint (false );
113
+ double lon = point .getX ();
114
+ double lat = point .getY ();
115
+
116
+ // valid missing values
117
+ for (Object missingVal : List .of ("POINT(" + lon + " " + lat + ")" , lat + ", " + lon , new GeoPoint (lat , lon ))) {
118
+ GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder ("my_agg" )
119
+ .field ("field" )
120
+ .missing (missingVal )
121
+ .wrapLongitude (false );
122
+
123
+ try (IndexReader reader = w .getReader ()) {
124
+ IndexSearcher searcher = new IndexSearcher (reader );
125
+ InternalGeoBounds bounds = search (searcher , new MatchAllDocsQuery (), aggBuilder , fieldType );
126
+ assertThat (bounds .top , equalTo (lat ));
127
+ assertThat (bounds .bottom , equalTo (lat ));
128
+ assertThat (bounds .posLeft , equalTo (lon >= 0 ? lon : Double .POSITIVE_INFINITY ));
129
+ assertThat (bounds .posRight , equalTo (lon >= 0 ? lon : Double .NEGATIVE_INFINITY ));
130
+ assertThat (bounds .negLeft , equalTo (lon >= 0 ? Double .POSITIVE_INFINITY : lon ));
131
+ assertThat (bounds .negRight , equalTo (lon >= 0 ? Double .NEGATIVE_INFINITY : lon ));
132
+ }
133
+ }
134
+
135
+ // invalid missing values
136
+ GeoBoundsAggregationBuilder aggBuilder = new GeoBoundsAggregationBuilder ("my_agg" )
137
+ .field ("field" )
138
+ .missing ("invalid" )
139
+ .wrapLongitude (false );
140
+ try (IndexReader reader = w .getReader ()) {
141
+ IndexSearcher searcher = new IndexSearcher (reader );
142
+ ElasticsearchParseException exception = expectThrows (ElasticsearchParseException .class ,
143
+ () -> search (searcher , new MatchAllDocsQuery (), aggBuilder , fieldType ));
144
+ assertThat (exception .getMessage (), startsWith ("unsupported symbol" ));
145
+ }
146
+
147
+ }
148
+ }
149
+
64
150
public void testRandom () throws Exception {
65
151
double top = Double .NEGATIVE_INFINITY ;
66
152
double bottom = Double .POSITIVE_INFINITY ;
@@ -118,4 +204,14 @@ public void testRandom() throws Exception {
118
204
}
119
205
}
120
206
}
207
+
208
+ @ Override
209
+ protected AggregationBuilder createAggBuilderForTypeTest (MappedFieldType fieldType , String fieldName ) {
210
+ return new GeoBoundsAggregationBuilder ("foo" ).field (fieldName );
211
+ }
212
+
213
+ @ Override
214
+ protected List <ValuesSourceType > getSupportedValuesSourceTypes () {
215
+ return List .of (CoreValuesSourceType .GEOPOINT );
216
+ }
121
217
}
0 commit comments