response5 = client.search(s -> s.query(q -> q.bool(b -> b.must(ageQuery)
+ .must(fullNameQuery)
+ .must(simpleStringQuery))), Person.class);
+ response5.hits()
+ .hits()
+ .forEach(hit -> log.info("Response 5: {}", hit.source()));
}
}
diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java
index abe10f4607..51e6ebca70 100644
--- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java
+++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java
@@ -1,199 +1,198 @@
package com.baeldung.elasticsearch;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import co.elastic.clients.elasticsearch.ElasticsearchClient;
+import co.elastic.clients.elasticsearch._types.GeoShapeRelation;
+import co.elastic.clients.elasticsearch.core.IndexResponse;
+import co.elastic.clients.elasticsearch.core.SearchRequest;
+import co.elastic.clients.elasticsearch.core.SearchResponse;
+import co.elastic.clients.elasticsearch.core.search.Hit;
+import co.elastic.clients.json.JsonData;
+import co.elastic.clients.json.jackson.JacksonJsonpMapper;
+import co.elastic.clients.transport.ElasticsearchTransport;
+import co.elastic.clients.transport.rest_client.RestClientTransport;
import java.io.IOException;
-import java.util.ArrayList;
-import java.util.Arrays;
+import java.io.StringReader;
import java.util.List;
-import java.util.stream.Collectors;
-import com.baeldung.spring.data.es.config.Config;
+import lombok.extern.slf4j.Slf4j;
-import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
-import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
-import org.elasticsearch.action.index.IndexRequest;
-import org.elasticsearch.action.index.IndexResponse;
-import org.elasticsearch.action.search.SearchRequest;
-import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.client.RequestOptions;
-import org.elasticsearch.client.RestHighLevelClient;
-import org.elasticsearch.client.indices.CreateIndexRequest;
-import org.elasticsearch.common.geo.GeoPoint;
-import org.elasticsearch.common.geo.ShapeRelation;
-import org.elasticsearch.common.unit.DistanceUnit;
-import org.elasticsearch.index.query.GeoShapeQueryBuilder;
-import org.elasticsearch.index.query.QueryBuilder;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.search.SearchHit;
-import org.elasticsearch.search.builder.SearchSourceBuilder;
-import org.elasticsearch.xcontent.XContentType;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.locationtech.jts.geom.Coordinate;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.apache.http.HttpHost;
+import org.elasticsearch.client.RestClient;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
/**
* This Manual test requires: Elasticsearch instance running on localhost:9200.
- *
- * The following docker command can be used: docker run -d --name es762 -p
- * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2
+ *
+ * The following docker command can be used: docker run -d --name elastic-test -p 9200:9200 -e
+ * "discovery.type=single-node" -e "xpack.security.enabled=false"
+ * docker.elastic.co/elasticsearch/elasticsearch:8.9.0
*/
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = Config.class)
-public class GeoQueriesManualTest {
+
+@Slf4j
+class GeoQueriesManualTest {
private static final String WONDERS_OF_WORLD = "wonders-of-world";
- @Autowired
- private RestHighLevelClient client;
+ private ElasticsearchClient client;
- @Before
+ @BeforeEach
public void setUp() throws Exception {
- String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}";
-
- CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
- req.mapping(jsonObject, XContentType.JSON);
-
+ RestClient restClient = RestClient.builder(HttpHost.create("http://localhost:9200"))
+ .build();
+ ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
+ client = new ElasticsearchClient(transport);
+ log.info("Creating index: {}", WONDERS_OF_WORLD);
client.indices()
- .create(req, RequestOptions.DEFAULT);
- }
-
-// @Test
-// public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException {
-// String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}";
-// IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
-// indexRequest.source(jsonObject, XContentType.JSON);
-// IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
-//
-// String tajMahalId = response.getId();
-//
-// RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
-// client.indices()
-// .refresh(refreshRequest, RequestOptions.DEFAULT);
-//
-// Coordinate topLeft = new Coordinate(74, 31.2);
-// Coordinate bottomRight = new Coordinate(81.1, 24);
-//
-// GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry());
-// qb.relation(ShapeRelation.INTERSECTS);
-//
-// SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
-// SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
-// searchRequest.source(source);
-//
-// SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
-//
-// List ids = Arrays.stream(searchResponse.getHits()
-// .getHits())
-// .map(SearchHit::getId)
-// .collect(Collectors.toList());
-//
-// assertTrue(ids.contains(tajMahalId));
-// }
-
- @Test
- public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception {
- String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}";
-
- IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
- indexRequest.source(jsonObject, XContentType.JSON);
- IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
-
- String pyramidsOfGizaId = response.getId();
-
- RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
- client.indices()
- .refresh(refreshRequest, RequestOptions.DEFAULT);
-
- QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location")
- .setCorners(31, 30, 28, 32);
-
- SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
- SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
- searchRequest.source(source);
-
- SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
-
- List ids = Arrays.stream(searchResponse.getHits()
- .getHits())
- .map(SearchHit::getId)
- .collect(Collectors.toList());
- assertTrue(ids.contains(pyramidsOfGizaId));
+ .create(builder -> builder.index(WONDERS_OF_WORLD)
+ .mappings(typeMapping -> typeMapping.properties("region", region -> region.geoShape(gs -> gs))
+ .properties("location", location -> location.geoPoint(gp -> gp))));
}
@Test
- public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception {
- String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}";
+ void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException {
+ String jsonObject = """
+ {
+ "name":"Agra",
+ "region":{
+ "type":"envelope",
+ "coordinates":[[75,30.2],[80.1,25]]
+ }
+ }
+ """;
+ IndexResponse response = client.index(idx -> idx.index(WONDERS_OF_WORLD)
+ .withJson(new StringReader(jsonObject)));
- IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD);
- indexRequest.source(jsonObject, XContentType.JSON);
- IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT);
-
- String lighthouseOfAlexandriaId = response.getId();
-
- RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD);
+ String tajMahalId = response.id();
client.indices()
- .refresh(refreshRequest, RequestOptions.DEFAULT);
+ .refresh();
- QueryBuilder qb = QueryBuilders.geoDistanceQuery("location")
- .point(29.976, 31.131)
- .distance(10, DistanceUnit.MILES);
+ StringReader jsonData = new StringReader("""
+ {
+ "type":"envelope",
+ "coordinates": [[74.0, 31.2], [81.1, 24.0 ] ]
+ }
+ """);
- SearchSourceBuilder source = new SearchSourceBuilder().query(qb);
- SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD);
- searchRequest.source(source);
-
- SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
-
- List ids = Arrays.stream(searchResponse.getHits()
- .getHits())
- .map(SearchHit::getId)
- .collect(Collectors.toList());
- assertTrue(ids.contains(lighthouseOfAlexandriaId));
+ SearchRequest searchRequest = new SearchRequest.Builder().query(query -> query.bool(boolQuery -> boolQuery.filter(query1 -> query1.geoShape(geoShapeQuery -> geoShapeQuery.field("region")
+ .shape(geoShapeFieldQuery -> geoShapeFieldQuery.relation(GeoShapeRelation.Within)
+ .shape(JsonData.from(jsonData)))))))
+ .build();
+ log.info("Search request: {}", searchRequest);
+ SearchResponse