43
43
import org .opensearch .test .OpenSearchIntegTestCase ;
44
44
import org .opensearch .test .VersionUtils ;
45
45
46
+ import java .io .IOException ;
47
+
46
48
import static org .opensearch .action .support .WriteRequest .RefreshPolicy .IMMEDIATE ;
47
49
import static org .opensearch .common .xcontent .XContentFactory .jsonBuilder ;
48
50
import static org .opensearch .index .query .QueryBuilders .boolQuery ;
52
54
import static org .hamcrest .Matchers .anyOf ;
53
55
import static org .hamcrest .Matchers .equalTo ;
54
56
55
- public class GeoBoundingBoxQueryIT extends OpenSearchIntegTestCase {
57
+ abstract class AbstractGeoBoundingBoxQueryIT extends OpenSearchIntegTestCase {
58
+
59
+ public abstract XContentBuilder addGeoMapping (XContentBuilder parentMapping ) throws IOException ;
60
+
61
+ public XContentBuilder getMapping () throws IOException {
62
+ XContentBuilder mapping = XContentFactory .jsonBuilder ().startObject ().startObject ("properties" );
63
+ mapping = addGeoMapping (mapping );
64
+ return mapping .endObject ().endObject ();
65
+ }
56
66
57
67
@ Override
58
68
protected boolean forbidPrivateIndexSettings () {
@@ -62,110 +72,55 @@ protected boolean forbidPrivateIndexSettings() {
62
72
public void testSimpleBoundingBoxTest () throws Exception {
63
73
Version version = VersionUtils .randomIndexCompatibleVersion (random ());
64
74
Settings settings = Settings .builder ().put (IndexMetadata .SETTING_VERSION_CREATED , version ).build ();
65
- XContentBuilder xContentBuilder = XContentFactory .jsonBuilder ()
66
- .startObject ()
67
- .startObject ("properties" )
68
- .startObject ("location" )
69
- .field ("type" , "geo_point" );
70
- xContentBuilder .endObject ().endObject ().endObject ();
75
+ XContentBuilder xContentBuilder = getMapping ();
71
76
assertAcked (prepareCreate ("test" ).setSettings (settings ).setMapping (xContentBuilder ));
72
77
ensureGreen ();
73
78
74
79
client ().prepareIndex ("test" )
75
80
.setId ("1" )
76
- .setSource (
77
- jsonBuilder ().startObject ()
78
- .field ("name" , "New York" )
79
- .startObject ("location" )
80
- .field ("lat" , 40.7143528 )
81
- .field ("lon" , -74.0059731 )
82
- .endObject ()
83
- .endObject ()
84
- )
81
+ .setSource (jsonBuilder ().startObject ().field ("name" , "New York" ).field ("location" , "POINT(-74.0059731 40.7143528)" ).endObject ())
85
82
.get ();
86
83
87
84
// to NY: 5.286 km
88
85
client ().prepareIndex ("test" )
89
86
.setId ("2" )
90
87
.setSource (
91
- jsonBuilder ().startObject ()
92
- .field ("name" , "Times Square" )
93
- .startObject ("location" )
94
- .field ("lat" , 40.759011 )
95
- .field ("lon" , -73.9844722 )
96
- .endObject ()
97
- .endObject ()
88
+ jsonBuilder ().startObject ().field ("name" , "Times Square" ).field ("location" , "POINT(-73.9844722 40.759011)" ).endObject ()
98
89
)
99
90
.get ();
100
91
101
92
// to NY: 0.4621 km
102
93
client ().prepareIndex ("test" )
103
94
.setId ("3" )
104
- .setSource (
105
- jsonBuilder ().startObject ()
106
- .field ("name" , "Tribeca" )
107
- .startObject ("location" )
108
- .field ("lat" , 40.718266 )
109
- .field ("lon" , -74.007819 )
110
- .endObject ()
111
- .endObject ()
112
- )
95
+ .setSource (jsonBuilder ().startObject ().field ("name" , "Tribeca" ).field ("location" , "POINT(-74.007819 40.718266)" ).endObject ())
113
96
.get ();
114
97
115
98
// to NY: 1.055 km
116
99
client ().prepareIndex ("test" )
117
100
.setId ("4" )
118
101
.setSource (
119
- jsonBuilder ().startObject ()
120
- .field ("name" , "Wall Street" )
121
- .startObject ("location" )
122
- .field ("lat" , 40.7051157 )
123
- .field ("lon" , -74.0088305 )
124
- .endObject ()
125
- .endObject ()
102
+ jsonBuilder ().startObject ().field ("name" , "Wall Street" ).field ("location" , "POINT(-74.0088305 40.7051157)" ).endObject ()
126
103
)
127
104
.get ();
128
105
129
106
// to NY: 1.258 km
130
107
client ().prepareIndex ("test" )
131
108
.setId ("5" )
132
- .setSource (
133
- jsonBuilder ().startObject ()
134
- .field ("name" , "Soho" )
135
- .startObject ("location" )
136
- .field ("lat" , 40.7247222 )
137
- .field ("lon" , -74 )
138
- .endObject ()
139
- .endObject ()
140
- )
109
+ .setSource (jsonBuilder ().startObject ().field ("name" , "Soho" ).field ("location" , "POINT(-74 40.7247222)" ).endObject ())
141
110
.get ();
142
111
143
112
// to NY: 2.029 km
144
113
client ().prepareIndex ("test" )
145
114
.setId ("6" )
146
115
.setSource (
147
- jsonBuilder ().startObject ()
148
- .field ("name" , "Greenwich Village" )
149
- .startObject ("location" )
150
- .field ("lat" , 40.731033 )
151
- .field ("lon" , -73.9962255 )
152
- .endObject ()
153
- .endObject ()
116
+ jsonBuilder ().startObject ().field ("name" , "Greenwich Village" ).field ("location" , "POINT(-73.9962255 40.731033)" ).endObject ()
154
117
)
155
118
.get ();
156
119
157
120
// to NY: 8.572 km
158
121
client ().prepareIndex ("test" )
159
122
.setId ("7" )
160
- .setSource (
161
- jsonBuilder ().startObject ()
162
- .field ("name" , "Brooklyn" )
163
- .startObject ("location" )
164
- .field ("lat" , 40.65 )
165
- .field ("lon" , -73.95 )
166
- .endObject ()
167
- .endObject ()
168
- )
123
+ .setSource (jsonBuilder ().startObject ().field ("name" , "Brooklyn" ).field ("location" , "POINT(-73.95 40.65)" ).endObject ())
169
124
.get ();
170
125
171
126
client ().admin ().indices ().prepareRefresh ().get ();
@@ -192,12 +147,7 @@ public void testSimpleBoundingBoxTest() throws Exception {
192
147
public void testLimit2BoundingBox () throws Exception {
193
148
Version version = VersionUtils .randomIndexCompatibleVersion (random ());
194
149
Settings settings = Settings .builder ().put (IndexMetadata .SETTING_VERSION_CREATED , version ).build ();
195
- XContentBuilder xContentBuilder = XContentFactory .jsonBuilder ()
196
- .startObject ()
197
- .startObject ("properties" )
198
- .startObject ("location" )
199
- .field ("type" , "geo_point" );
200
- xContentBuilder .endObject ().endObject ().endObject ();
150
+ XContentBuilder xContentBuilder = getMapping ();
201
151
assertAcked (prepareCreate ("test" ).setSettings (settings ).setMapping (xContentBuilder ));
202
152
ensureGreen ();
203
153
@@ -207,10 +157,7 @@ public void testLimit2BoundingBox() throws Exception {
207
157
jsonBuilder ().startObject ()
208
158
.field ("userid" , 880 )
209
159
.field ("title" , "Place in Stockholm" )
210
- .startObject ("location" )
211
- .field ("lat" , 59.328355000000002 )
212
- .field ("lon" , 18.036842 )
213
- .endObject ()
160
+ .field ("location" , "POINT(59.328355000000002 18.036842)" )
214
161
.endObject ()
215
162
)
216
163
.setRefreshPolicy (IMMEDIATE )
@@ -222,10 +169,7 @@ public void testLimit2BoundingBox() throws Exception {
222
169
jsonBuilder ().startObject ()
223
170
.field ("userid" , 534 )
224
171
.field ("title" , "Place in Montreal" )
225
- .startObject ("location" )
226
- .field ("lat" , 45.509526999999999 )
227
- .field ("lon" , -73.570986000000005 )
228
- .endObject ()
172
+ .field ("location" , "POINT(-73.570986000000005 45.509526999999999)" )
229
173
.endObject ()
230
174
)
231
175
.setRefreshPolicy (IMMEDIATE )
@@ -271,12 +215,7 @@ public void testLimit2BoundingBox() throws Exception {
271
215
public void testCompleteLonRange () throws Exception {
272
216
Version version = VersionUtils .randomIndexCompatibleVersion (random ());
273
217
Settings settings = Settings .builder ().put (IndexMetadata .SETTING_VERSION_CREATED , version ).build ();
274
- XContentBuilder xContentBuilder = XContentFactory .jsonBuilder ()
275
- .startObject ()
276
- .startObject ("properties" )
277
- .startObject ("location" )
278
- .field ("type" , "geo_point" );
279
- xContentBuilder .endObject ().endObject ().endObject ();
218
+ XContentBuilder xContentBuilder = getMapping ();
280
219
assertAcked (prepareCreate ("test" ).setSettings (settings ).setMapping (xContentBuilder ));
281
220
ensureGreen ();
282
221
@@ -286,10 +225,7 @@ public void testCompleteLonRange() throws Exception {
286
225
jsonBuilder ().startObject ()
287
226
.field ("userid" , 880 )
288
227
.field ("title" , "Place in Stockholm" )
289
- .startObject ("location" )
290
- .field ("lat" , 59.328355000000002 )
291
- .field ("lon" , 18.036842 )
292
- .endObject ()
228
+ .field ("location" , "POINT(18.036842 59.328355000000002)" )
293
229
.endObject ()
294
230
)
295
231
.setRefreshPolicy (IMMEDIATE )
@@ -301,10 +237,7 @@ public void testCompleteLonRange() throws Exception {
301
237
jsonBuilder ().startObject ()
302
238
.field ("userid" , 534 )
303
239
.field ("title" , "Place in Montreal" )
304
- .startObject ("location" )
305
- .field ("lat" , 45.509526999999999 )
306
- .field ("lon" , -73.570986000000005 )
307
- .endObject ()
240
+ .field ("location" , "POINT(-73.570986000000005 45.509526999999999)" )
308
241
.endObject ()
309
242
)
310
243
.setRefreshPolicy (IMMEDIATE )
0 commit comments