24
24
25
25
import org .elasticsearch .common .geo .GeoHashUtils ;
26
26
import org .elasticsearch .index .query .FilterBuilders ;
27
+ import org .elasticsearch .search .aggregations .AggregationBuilders ;
27
28
import org .junit .Test ;
28
29
import org .junit .runner .RunWith ;
29
30
import org .springframework .beans .factory .annotation .Autowired ;
@@ -63,18 +64,36 @@ private void loadClassBaseEntities() {
63
64
}
64
65
65
66
private void loadAnnotationBaseEntities () {
66
- elasticsearchTemplate .deleteIndex (AuthorMarkerAnnotatedEntity .class );
67
- elasticsearchTemplate .createIndex (AuthorMarkerAnnotatedEntity .class );
68
- elasticsearchTemplate .refresh (AuthorMarkerAnnotatedEntity .class , true );
69
- elasticsearchTemplate .putMapping (AuthorMarkerAnnotatedEntity .class );
67
+ elasticsearchTemplate .deleteIndex (LocationMarkerEntity .class );
68
+ elasticsearchTemplate .createIndex (LocationMarkerEntity .class );
69
+ elasticsearchTemplate .refresh (LocationMarkerEntity .class , true );
70
+ elasticsearchTemplate .putMapping (LocationMarkerEntity .class );
70
71
71
72
List <IndexQuery > indexQueries = new ArrayList <IndexQuery >();
72
73
double [] latLonArray = {0.100000 , 51.000000 };
73
74
String lonLatString = "51.000000, 0.100000" ;
74
75
String geohash = "u1044k2bd6u" ;
75
- indexQueries .add (new AuthorMarkerAnnotatedEntityBuilder ("2" ).name ("Mohsin Husen" ).location (geohash .substring (0 , 5 )).additionalLocation (latLonArray ).buildIndex ());
76
- indexQueries .add (new AuthorMarkerAnnotatedEntityBuilder ("1" ).name ("Artur Konczak" ).location (lonLatString ).additionalLocation (latLonArray ).buildIndex ());
77
- indexQueries .add (new AuthorMarkerAnnotatedEntityBuilder ("3" ).name ("Rizwan Idrees" ).location (geohash ).additionalLocation (latLonArray ).buildIndex ());
76
+ LocationMarkerEntity location1 = LocationMarkerEntity .builder ()
77
+ .id ("1" ).name ("Artur Konczak" )
78
+ .locationAsString (lonLatString )
79
+ .locationAsArray (latLonArray )
80
+ .locationWithPrefixAsDistance (geohash )
81
+ .locationWithPrefixAsLengthOfGeoHash (geohash )
82
+ .build ();
83
+ LocationMarkerEntity location2 = LocationMarkerEntity .builder ()
84
+ .id ("2" ).name ("Mohsin Husen" )
85
+ .locationAsString (geohash .substring (0 , 5 ))
86
+ .locationAsArray (latLonArray )
87
+ .build ();
88
+ LocationMarkerEntity location3 = LocationMarkerEntity .builder ()
89
+ .id ("3" ).name ("Rizwan Idrees" )
90
+ .locationAsString (geohash )
91
+ .locationAsArray (latLonArray )
92
+ .locationWithPrefixAsLengthOfGeoHash (geohash )
93
+ .build ();
94
+ indexQueries .add (buildIndex (location1 ));
95
+ indexQueries .add (buildIndex (location2 ));
96
+ indexQueries .add (buildIndex (location3 ));
78
97
79
98
elasticsearchTemplate .bulkIndex (indexQueries );
80
99
elasticsearchTemplate .refresh (AuthorMarkerEntity .class , true );
@@ -122,9 +141,9 @@ public void shouldFindStringAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
122
141
//given
123
142
loadAnnotationBaseEntities ();
124
143
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery (
125
- new Criteria ("location " ).within (new GeoPoint (51.000000 , 0.100000 ), "1km" ));
144
+ new Criteria ("locationAsString " ).within (new GeoPoint (51.000000 , 0.100000 ), "1km" ));
126
145
//when
127
- List <AuthorMarkerAnnotatedEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , AuthorMarkerAnnotatedEntity .class );
146
+ List <LocationMarkerEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , LocationMarkerEntity .class );
128
147
129
148
//then
130
149
assertThat (geoAuthorsForGeoCriteria .size (), is (3 ));
@@ -135,9 +154,9 @@ public void shouldFindDoubleAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
135
154
//given
136
155
loadAnnotationBaseEntities ();
137
156
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery (
138
- new Criteria ("additionalLocation " ).within (new GeoPoint (51.001000 , 0.10100 ), "1km" ));
157
+ new Criteria ("locationAsArray " ).within (new GeoPoint (51.001000 , 0.10100 ), "1km" ));
139
158
//when
140
- List <AuthorMarkerAnnotatedEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , AuthorMarkerAnnotatedEntity .class );
159
+ List <LocationMarkerEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , LocationMarkerEntity .class );
141
160
142
161
//then
143
162
assertThat (geoAuthorsForGeoCriteria .size (), is (3 ));
@@ -148,9 +167,9 @@ public void shouldFindAnnotatedGeoMarkersInRangeForGivenCriteriaQuery() {
148
167
//given
149
168
loadAnnotationBaseEntities ();
150
169
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery (
151
- new Criteria ("additionalLocation " ).within ("51.001000, 0.10100" , "1km" ));
170
+ new Criteria ("locationAsArray " ).within ("51.001000, 0.10100" , "1km" ));
152
171
//when
153
- List <AuthorMarkerAnnotatedEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , AuthorMarkerAnnotatedEntity .class );
172
+ List <LocationMarkerEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , LocationMarkerEntity .class );
154
173
155
174
//then
156
175
assertThat (geoAuthorsForGeoCriteria .size (), is (3 ));
@@ -161,9 +180,9 @@ public void shouldFindAnnotatedGeoMarkersInRangeForGivenCriteriaQueryUsingGeohas
161
180
//given
162
181
loadAnnotationBaseEntities ();
163
182
CriteriaQuery geoLocationCriteriaQuery = new CriteriaQuery (
164
- new Criteria ("additionalLocation " ).within ("u1044" , "1km" ));
183
+ new Criteria ("locationAsArray " ).within ("u1044" , "1km" ));
165
184
//when
166
- List <AuthorMarkerAnnotatedEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , AuthorMarkerAnnotatedEntity .class );
185
+ List <LocationMarkerEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (geoLocationCriteriaQuery , LocationMarkerEntity .class );
167
186
168
187
//then
169
188
assertThat (geoAuthorsForGeoCriteria .size (), is (3 ));
@@ -173,9 +192,9 @@ public void shouldFindAnnotatedGeoMarkersInRangeForGivenCriteriaQueryUsingGeohas
173
192
public void shouldFindAllMarkersForNativeSearchQuery () {
174
193
//Given
175
194
loadAnnotationBaseEntities ();
176
- NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoBoundingBoxFilter ("additionalLocation " ).topLeft (52 , -1 ).bottomRight (50 , 1 ));
195
+ NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoBoundingBoxFilter ("locationAsArray " ).topLeft (52 , -1 ).bottomRight (50 , 1 ));
177
196
//When
178
- List <AuthorMarkerAnnotatedEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (queryBuilder .build (), AuthorMarkerAnnotatedEntity .class );
197
+ List <LocationMarkerEntity > geoAuthorsForGeoCriteria = elasticsearchTemplate .queryForList (queryBuilder .build (), LocationMarkerEntity .class );
179
198
//Then
180
199
assertThat (geoAuthorsForGeoCriteria .size (), is (3 ));
181
200
}
@@ -228,4 +247,45 @@ public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingGeoPoints() {
228
247
assertThat (geoAuthorsForGeoCriteria3 .size (), is (2 ));
229
248
assertThat (geoAuthorsForGeoCriteria3 , containsInAnyOrder (hasProperty ("name" , equalTo ("Mohsin Husen" )), hasProperty ("name" , equalTo ("Rizwan Idrees" ))));
230
249
}
250
+
251
+ @ Test
252
+ public void shouldFindLocationWithGeoHashPrefix () {
253
+
254
+ //given
255
+ //u1044k2bd6u - with precision = 4 -> u, u1, u10, u104
256
+ //u1044k2bd6u - with precision = 5 -> u, u1, u10, u104, u1044
257
+
258
+ loadAnnotationBaseEntities ();
259
+ NativeSearchQueryBuilder locationWithPrefixAsDistancePrecision3 = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoHashCellFilter ("box-one" ).field ("locationWithPrefixAsDistance" ).geohash ("u1044k2bd6u" ).precision (3 ));
260
+ NativeSearchQueryBuilder locationWithPrefixAsDistancePrecision4 = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoHashCellFilter ("box-one" ).field ("locationWithPrefixAsDistance" ).geohash ("u1044k2bd6u" ).precision (4 ));
261
+ NativeSearchQueryBuilder locationWithPrefixAsDistancePrecision5 = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoHashCellFilter ("box-one" ).field ("locationWithPrefixAsDistance" ).geohash ("u1044k2bd6u" ).precision (5 ));
262
+
263
+ NativeSearchQueryBuilder locationWithPrefixAsLengthOfGeoHashPrecision4 = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoHashCellFilter ("box-one" ).field ("locationWithPrefixAsLengthOfGeoHash" ).geohash ("u1044k2bd6u" ).precision (4 ));
264
+ NativeSearchQueryBuilder locationWithPrefixAsLengthOfGeoHashPrecision5 = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoHashCellFilter ("box-one" ).field ("locationWithPrefixAsLengthOfGeoHash" ).geohash ("u1044k2bd6u" ).precision (5 ));
265
+ NativeSearchQueryBuilder locationWithPrefixAsLengthOfGeoHashPrecision6 = new NativeSearchQueryBuilder ().withFilter (FilterBuilders .geoHashCellFilter ("box-one" ).field ("locationWithPrefixAsLengthOfGeoHash" ).geohash ("u1044k2bd6u" ).precision (6 ));
266
+ //when
267
+ List <LocationMarkerEntity > resultDistancePrecision3 = elasticsearchTemplate .queryForList (locationWithPrefixAsDistancePrecision3 .build (), LocationMarkerEntity .class );
268
+ List <LocationMarkerEntity > resultDistancePrecision4 = elasticsearchTemplate .queryForList (locationWithPrefixAsDistancePrecision4 .build (), LocationMarkerEntity .class );
269
+ List <LocationMarkerEntity > resultDistancePrecision5 = elasticsearchTemplate .queryForList (locationWithPrefixAsDistancePrecision5 .build (), LocationMarkerEntity .class );
270
+
271
+ List <LocationMarkerEntity > resultGeoHashLengthPrecision4 = elasticsearchTemplate .queryForList (locationWithPrefixAsLengthOfGeoHashPrecision4 .build (), LocationMarkerEntity .class );
272
+ List <LocationMarkerEntity > resultGeoHashLengthPrecision5 = elasticsearchTemplate .queryForList (locationWithPrefixAsLengthOfGeoHashPrecision5 .build (), LocationMarkerEntity .class );
273
+ List <LocationMarkerEntity > resultGeoHashLengthPrecision6 = elasticsearchTemplate .queryForList (locationWithPrefixAsLengthOfGeoHashPrecision6 .build (), LocationMarkerEntity .class );
274
+
275
+ //then
276
+ assertThat (resultDistancePrecision3 .size (), is (1 ));
277
+ assertThat (resultDistancePrecision4 .size (), is (1 ));
278
+ assertThat (resultDistancePrecision5 .size (), is (0 ));
279
+
280
+ assertThat (resultGeoHashLengthPrecision4 .size (), is (2 ));
281
+ assertThat (resultGeoHashLengthPrecision5 .size (), is (2 ));
282
+ assertThat (resultGeoHashLengthPrecision6 .size (), is (0 ));
283
+ }
284
+
285
+ private IndexQuery buildIndex (LocationMarkerEntity result ) {
286
+ IndexQuery indexQuery = new IndexQuery ();
287
+ indexQuery .setId (result .getId ());
288
+ indexQuery .setObject (result );
289
+ return indexQuery ;
290
+ }
231
291
}
0 commit comments