|
| 1 | +package com.baeldung.elasticsearch; |
| 2 | + |
| 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 | + |
| 9 | +import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; |
| 10 | +import org.elasticsearch.action.index.IndexResponse; |
| 11 | +import org.elasticsearch.action.search.SearchResponse; |
| 12 | +import org.elasticsearch.client.Client; |
| 13 | +import org.elasticsearch.common.geo.ShapeRelation; |
| 14 | +import org.elasticsearch.common.geo.builders.ShapeBuilder; |
| 15 | +import org.elasticsearch.common.unit.DistanceUnit; |
| 16 | +import org.elasticsearch.index.query.QueryBuilder; |
| 17 | +import org.elasticsearch.index.query.QueryBuilders; |
| 18 | +import org.junit.After; |
| 19 | +import org.junit.Before; |
| 20 | +import org.junit.Test; |
| 21 | +import org.junit.runner.RunWith; |
| 22 | +import org.springframework.beans.factory.annotation.Autowired; |
| 23 | +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; |
| 24 | +import org.springframework.test.context.ContextConfiguration; |
| 25 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 26 | + |
| 27 | +import com.baeldung.spring.data.es.config.Config; |
| 28 | + |
| 29 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 30 | +@ContextConfiguration(classes = Config.class) |
| 31 | +public class GeoQueriesTest { |
| 32 | + |
| 33 | + public static final String WONDERS_OF_WORLD = "wonders-of-world"; |
| 34 | + public static final String WONDERS = "Wonders"; |
| 35 | + @Autowired |
| 36 | + private ElasticsearchTemplate elasticsearchTemplate; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private Client client; |
| 40 | + |
| 41 | + @Before |
| 42 | + public void setUp() { |
| 43 | + String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; |
| 44 | + CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); |
| 45 | + req.mapping(WONDERS, jsonObject); |
| 46 | + client.admin() |
| 47 | + .indices() |
| 48 | + .create(req) |
| 49 | + .actionGet(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() { |
| 54 | + String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,25],[80.1,30.2]]}}"; |
| 55 | + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) |
| 56 | + .setSource(jsonObject) |
| 57 | + .get(); |
| 58 | + String tajMahalId = response.getId(); |
| 59 | + client.admin() |
| 60 | + .indices() |
| 61 | + .prepareRefresh(WONDERS_OF_WORLD) |
| 62 | + .get(); |
| 63 | + |
| 64 | + QueryBuilder qb = QueryBuilders.geoShapeQuery("region", ShapeBuilder.newEnvelope() |
| 65 | + .topLeft(74.00, 24.0) |
| 66 | + .bottomRight(81.1, 31.2)) |
| 67 | + .relation(ShapeRelation.WITHIN); |
| 68 | + |
| 69 | + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) |
| 70 | + .setTypes(WONDERS) |
| 71 | + .setQuery(qb) |
| 72 | + .execute() |
| 73 | + .actionGet(); |
| 74 | + List<String> ids = Arrays.stream(searchResponse.getHits() |
| 75 | + .getHits()) |
| 76 | + .map(hit -> { |
| 77 | + return hit.getId(); |
| 78 | + }) |
| 79 | + .collect(Collectors.toList()); |
| 80 | + assertTrue(ids.contains(tajMahalId)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { |
| 85 | + String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; |
| 86 | + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) |
| 87 | + .setSource(jsonObject) |
| 88 | + .get(); |
| 89 | + String pyramidsOfGizaId = response.getId(); |
| 90 | + client.admin() |
| 91 | + .indices() |
| 92 | + .prepareRefresh(WONDERS_OF_WORLD) |
| 93 | + .get(); |
| 94 | + |
| 95 | + QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location") |
| 96 | + .bottomLeft(28, 30) |
| 97 | + .topRight(31, 32); |
| 98 | + |
| 99 | + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) |
| 100 | + .setTypes(WONDERS) |
| 101 | + .setQuery(qb) |
| 102 | + .execute() |
| 103 | + .actionGet(); |
| 104 | + List<String> ids = Arrays.stream(searchResponse.getHits() |
| 105 | + .getHits()) |
| 106 | + .map(hit -> { |
| 107 | + return hit.getId(); |
| 108 | + }) |
| 109 | + .collect(Collectors.toList()); |
| 110 | + assertTrue(ids.contains(pyramidsOfGizaId)); |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { |
| 115 | + String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; |
| 116 | + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) |
| 117 | + .setSource(jsonObject) |
| 118 | + .get(); |
| 119 | + String lighthouseOfAlexandriaId = response.getId(); |
| 120 | + client.admin() |
| 121 | + .indices() |
| 122 | + .prepareRefresh(WONDERS_OF_WORLD) |
| 123 | + .get(); |
| 124 | + |
| 125 | + QueryBuilder qb = QueryBuilders.geoDistanceQuery("location") |
| 126 | + .point(29.976, 31.131) |
| 127 | + .distance(10, DistanceUnit.MILES); |
| 128 | + |
| 129 | + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) |
| 130 | + .setTypes(WONDERS) |
| 131 | + .setQuery(qb) |
| 132 | + .execute() |
| 133 | + .actionGet(); |
| 134 | + List<String> ids = Arrays.stream(searchResponse.getHits() |
| 135 | + .getHits()) |
| 136 | + .map(hit -> { |
| 137 | + return hit.getId(); |
| 138 | + }) |
| 139 | + .collect(Collectors.toList()); |
| 140 | + assertTrue(ids.contains(lighthouseOfAlexandriaId)); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { |
| 145 | + String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; |
| 146 | + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) |
| 147 | + .setSource(jsonObject) |
| 148 | + .get(); |
| 149 | + String greatRannOfKutchid = response.getId(); |
| 150 | + client.admin() |
| 151 | + .indices() |
| 152 | + .prepareRefresh(WONDERS_OF_WORLD) |
| 153 | + .get(); |
| 154 | + |
| 155 | + QueryBuilder qb = QueryBuilders.geoPolygonQuery("location") |
| 156 | + .addPoint(22.733, 68.859) |
| 157 | + .addPoint(24.733, 68.859) |
| 158 | + .addPoint(23, 70.859); |
| 159 | + |
| 160 | + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) |
| 161 | + .setTypes(WONDERS) |
| 162 | + .setQuery(qb) |
| 163 | + .execute() |
| 164 | + .actionGet(); |
| 165 | + List<String> ids = Arrays.stream(searchResponse.getHits() |
| 166 | + .getHits()) |
| 167 | + .map(hit -> { |
| 168 | + return hit.getId(); |
| 169 | + }) |
| 170 | + .collect(Collectors.toList()); |
| 171 | + assertTrue(ids.contains(greatRannOfKutchid)); |
| 172 | + } |
| 173 | + |
| 174 | + @After |
| 175 | + public void destroy() { |
| 176 | + elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments