11package com .baeldung .elasticsearch ;
22
3- import static org .junit .Assert .assertTrue ;
4-
5- import java .util .Arrays ;
6- import java .util .List ;
7- import java .util .stream .Collectors ;
8-
3+ import com .baeldung .spring .data .es .config .Config ;
94import org .elasticsearch .action .admin .indices .create .CreateIndexRequest ;
105import org .elasticsearch .action .index .IndexResponse ;
116import org .elasticsearch .action .search .SearchResponse ;
1510import org .elasticsearch .common .unit .DistanceUnit ;
1611import org .elasticsearch .index .query .QueryBuilder ;
1712import org .elasticsearch .index .query .QueryBuilders ;
13+ import org .elasticsearch .search .SearchHit ;
1814import org .junit .After ;
1915import org .junit .Before ;
2016import org .junit .Test ;
2420import org .springframework .test .context .ContextConfiguration ;
2521import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
2622
27- import com .baeldung .spring .data .es .config .Config ;
23+ import java .util .Arrays ;
24+ import java .util .List ;
25+ import java .util .stream .Collectors ;
26+
27+ import static org .junit .Assert .assertTrue ;
2828
2929@ RunWith (SpringJUnit4ClassRunner .class )
3030@ ContextConfiguration (classes = Config .class )
3131public class GeoQueriesTest {
3232
33- public static final String WONDERS_OF_WORLD = "wonders-of-world" ;
34- public static final String WONDERS = "Wonders" ;
33+ private static final String WONDERS_OF_WORLD = "wonders-of-world" ;
34+ private static final String WONDERS = "Wonders" ;
35+
3536 @ Autowired
3637 private ElasticsearchTemplate elasticsearchTemplate ;
3738
@@ -44,136 +45,127 @@ public void setUp() {
4445 CreateIndexRequest req = new CreateIndexRequest (WONDERS_OF_WORLD );
4546 req .mapping (WONDERS , jsonObject );
4647 client .admin ()
47- .indices ()
48- .create (req )
49- .actionGet ();
48+ .indices ()
49+ .create (req )
50+ .actionGet ();
5051 }
5152
5253 @ Test
5354 public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty () {
5455 String jsonObject = "{\" name\" :\" Agra\" ,\" region\" :{\" type\" :\" envelope\" ,\" coordinates\" :[[75,25],[80.1,30.2]]}}" ;
5556 IndexResponse response = client .prepareIndex (WONDERS_OF_WORLD , WONDERS )
56- .setSource (jsonObject )
57- .get ();
57+ .setSource (jsonObject )
58+ .get ();
5859 String tajMahalId = response .getId ();
5960 client .admin ()
60- .indices ()
61- .prepareRefresh (WONDERS_OF_WORLD )
62- .get ();
61+ .indices ()
62+ .prepareRefresh (WONDERS_OF_WORLD )
63+ .get ();
6364
6465 QueryBuilder qb = QueryBuilders .geoShapeQuery ("region" , ShapeBuilder .newEnvelope ()
65- .topLeft (74.00 , 24.0 )
66- .bottomRight (81.1 , 31.2 ))
67- .relation (ShapeRelation .WITHIN );
66+ .topLeft (74.00 , 24.0 )
67+ .bottomRight (81.1 , 31.2 ))
68+ .relation (ShapeRelation .WITHIN );
6869
6970 SearchResponse searchResponse = client .prepareSearch (WONDERS_OF_WORLD )
70- .setTypes (WONDERS )
71- .setQuery (qb )
72- .execute ()
73- .actionGet ();
71+ .setTypes (WONDERS )
72+ .setQuery (qb )
73+ .execute ()
74+ .actionGet ();
7475 List <String > ids = Arrays .stream (searchResponse .getHits ()
75- .getHits ())
76- .map (hit -> {
77- return hit .getId ();
78- })
79- .collect (Collectors .toList ());
76+ .getHits ())
77+ .map (SearchHit ::getId )
78+ .collect (Collectors .toList ());
8079 assertTrue (ids .contains (tajMahalId ));
8180 }
8281
8382 @ Test
8483 public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty () {
8584 String jsonObject = "{\" name\" :\" Pyramids of Giza\" ,\" location\" :[31.131302,29.976480]}" ;
8685 IndexResponse response = client .prepareIndex (WONDERS_OF_WORLD , WONDERS )
87- .setSource (jsonObject )
88- .get ();
86+ .setSource (jsonObject )
87+ .get ();
8988 String pyramidsOfGizaId = response .getId ();
9089 client .admin ()
91- .indices ()
92- .prepareRefresh (WONDERS_OF_WORLD )
93- .get ();
90+ .indices ()
91+ .prepareRefresh (WONDERS_OF_WORLD )
92+ .get ();
9493
9594 QueryBuilder qb = QueryBuilders .geoBoundingBoxQuery ("location" )
96- .bottomLeft (28 , 30 )
97- .topRight (31 , 32 );
95+ .bottomLeft (28 , 30 )
96+ .topRight (31 , 32 );
9897
9998 SearchResponse searchResponse = client .prepareSearch (WONDERS_OF_WORLD )
100- .setTypes (WONDERS )
101- .setQuery (qb )
102- .execute ()
103- .actionGet ();
99+ .setTypes (WONDERS )
100+ .setQuery (qb )
101+ .execute ()
102+ .actionGet ();
104103 List <String > ids = Arrays .stream (searchResponse .getHits ()
105- .getHits ())
106- .map (hit -> {
107- return hit .getId ();
108- })
109- .collect (Collectors .toList ());
104+ .getHits ())
105+ .map (SearchHit ::getId )
106+ .collect (Collectors .toList ());
110107 assertTrue (ids .contains (pyramidsOfGizaId ));
111108 }
112109
113110 @ Test
114111 public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty () {
115112 String jsonObject = "{\" name\" :\" Lighthouse of alexandria\" ,\" location\" :[31.131302,29.976480]}" ;
116113 IndexResponse response = client .prepareIndex (WONDERS_OF_WORLD , WONDERS )
117- .setSource (jsonObject )
118- .get ();
114+ .setSource (jsonObject )
115+ .get ();
119116 String lighthouseOfAlexandriaId = response .getId ();
120117 client .admin ()
121- .indices ()
122- .prepareRefresh (WONDERS_OF_WORLD )
123- .get ();
118+ .indices ()
119+ .prepareRefresh (WONDERS_OF_WORLD )
120+ .get ();
124121
125122 QueryBuilder qb = QueryBuilders .geoDistanceQuery ("location" )
126- .point (29.976 , 31.131 )
127- .distance (10 , DistanceUnit .MILES );
123+ .point (29.976 , 31.131 )
124+ .distance (10 , DistanceUnit .MILES );
128125
129126 SearchResponse searchResponse = client .prepareSearch (WONDERS_OF_WORLD )
130- .setTypes (WONDERS )
131- .setQuery (qb )
132- .execute ()
133- .actionGet ();
127+ .setTypes (WONDERS )
128+ .setQuery (qb )
129+ .execute ()
130+ .actionGet ();
134131 List <String > ids = Arrays .stream (searchResponse .getHits ()
135- .getHits ())
136- .map (hit -> {
137- return hit .getId ();
138- })
139- .collect (Collectors .toList ());
132+ .getHits ())
133+ .map (SearchHit ::getId )
134+ .collect (Collectors .toList ());
140135 assertTrue (ids .contains (lighthouseOfAlexandriaId ));
141136 }
142137
143138 @ Test
144139 public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty () {
145140 String jsonObject = "{\" name\" :\" The Great Rann of Kutch\" ,\" location\" :[69.859741,23.733732]}" ;
146141 IndexResponse response = client .prepareIndex (WONDERS_OF_WORLD , WONDERS )
147- .setSource (jsonObject )
148- .get ();
142+ .setSource (jsonObject )
143+ .get ();
149144 String greatRannOfKutchid = response .getId ();
150145 client .admin ()
151- .indices ()
152- .prepareRefresh (WONDERS_OF_WORLD )
153- .get ();
146+ .indices ()
147+ .prepareRefresh (WONDERS_OF_WORLD )
148+ .get ();
154149
155150 QueryBuilder qb = QueryBuilders .geoPolygonQuery ("location" )
156- .addPoint (22.733 , 68.859 )
157- .addPoint (24.733 , 68.859 )
158- .addPoint (23 , 70.859 );
151+ .addPoint (22.733 , 68.859 )
152+ .addPoint (24.733 , 68.859 )
153+ .addPoint (23 , 70.859 );
159154
160155 SearchResponse searchResponse = client .prepareSearch (WONDERS_OF_WORLD )
161- .setTypes (WONDERS )
162- .setQuery (qb )
163- .execute ()
164- .actionGet ();
156+ .setTypes (WONDERS )
157+ .setQuery (qb )
158+ .execute ()
159+ .actionGet ();
165160 List <String > ids = Arrays .stream (searchResponse .getHits ()
166- .getHits ())
167- .map (hit -> {
168- return hit .getId ();
169- })
170- .collect (Collectors .toList ());
161+ .getHits ())
162+ .map (SearchHit ::getId )
163+ .collect (Collectors .toList ());
171164 assertTrue (ids .contains (greatRannOfKutchid ));
172165 }
173166
174167 @ After
175168 public void destroy () {
176169 elasticsearchTemplate .deleteIndex (WONDERS_OF_WORLD );
177170 }
178-
179171}
0 commit comments