From 745d5edda1c1018fad25e4e076f54f1fda89b464 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Sun, 12 Apr 2020 16:20:59 -0400 Subject: [PATCH 01/12] upgraded libs, moved to RestHighLevelClient --- .../spring-data-elasticsearch/pom.xml | 32 ++-- .../spring/data/es/config/Config.java | 47 ++---- .../ElasticSearchManualTest.java | 141 +++++++++--------- .../elasticsearch/GeoQueriesManualTest.java | 17 +-- .../data/es/ElasticSearchQueryManualTest.java | 3 +- 5 files changed, 107 insertions(+), 133 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 3446528323..f322e46c0e 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -25,6 +25,12 @@ ${spring.version} + + org.springframework + spring-web + ${spring.version} + + org.springframework.data spring-data-elasticsearch @@ -32,8 +38,8 @@ - org.elasticsearch - elasticsearch + org.elasticsearch.client + elasticsearch-rest-high-level-client ${elasticsearch.version} @@ -49,15 +55,9 @@ - com.vividsolutions - jts - ${jts.version} - - - xerces - xercesImpl - - + org.locationtech.jts + jts-core + 1.16.1 @@ -66,12 +66,6 @@ ${log4j.version} - - org.elasticsearch.client - transport - ${elasticsearch.version} - - org.springframework spring-test @@ -88,9 +82,9 @@ - 3.0.8.RELEASE + 3.2.6.RELEASE 4.5.2 - 5.6.0 + 7.6.2 1.2.47 0.6 1.13 diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java index e6ce795b45..f98342486f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java +++ b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java @@ -1,19 +1,13 @@ package com.baeldung.spring.data.es.config; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import org.elasticsearch.client.Client; -import org.elasticsearch.client.transport.TransportClient; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.transport.InetSocketTransportAddress; -import org.elasticsearch.transport.client.PreBuiltTransportClient; -import org.springframework.beans.factory.annotation.Value; +import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.client.ClientConfiguration; +import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; @Configuration @@ -21,30 +15,15 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea @ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" }) public class Config { - @Value("${elasticsearch.home:/usr/local/Cellar/elasticsearch/5.6.0}") - private String elasticsearchHome; + @Bean + RestHighLevelClient client() { + ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); - @Value("${elasticsearch.cluster.name:elasticsearch}") - private String clusterName; + return RestClients.create(clientConfiguration).rest(); + } - @Bean - public Client client() { - TransportClient client = null; - try { - final Settings elasticsearchSettings = Settings.builder() - .put("client.transport.sniff", true) - .put("path.home", elasticsearchHome) - .put("cluster.name", clusterName).build(); - client = new PreBuiltTransportClient(elasticsearchSettings); - client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); - } catch (UnknownHostException e) { - e.printStackTrace(); - } - return client; - } - - @Bean - public ElasticsearchOperations elasticsearchTemplate() { - return new ElasticsearchTemplate(client()); - } + @Bean + public ElasticsearchOperations elasticsearchTemplate() { + return new ElasticsearchRestTemplate(client()); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index e43dcdf43e..6dc1c42dc1 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -3,7 +3,6 @@ package com.baeldung.elasticsearch; import static org.junit.Assert.assertEquals; import java.io.IOException; -import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; @@ -11,24 +10,28 @@ import java.util.Date; import java.util.List; import java.util.stream.Collectors; +import com.alibaba.fastjson.JSON; + import org.elasticsearch.action.DocWriteResponse.Result; +import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; +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.action.search.SearchType; -import org.elasticsearch.client.Client; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.transport.InetSocketTransportAddress; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.transport.client.PreBuiltTransportClient; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; import org.junit.Test; - -import com.alibaba.fastjson.JSON; +import org.springframework.data.elasticsearch.client.ClientConfiguration; +import org.springframework.data.elasticsearch.client.RestClients; /** * @@ -39,7 +42,7 @@ import com.alibaba.fastjson.JSON; */ public class ElasticSearchManualTest { private List listOfPersons = new ArrayList<>(); - private Client client = null; + private RestHighLevelClient client = null; @Before public void setUp() throws UnknownHostException { @@ -48,47 +51,44 @@ public class ElasticSearchManualTest { listOfPersons.add(person1); listOfPersons.add(person2); - client = new PreBuiltTransportClient(Settings.builder().put("client.transport.sniff", true) - .put("cluster.name","elasticsearch").build()) - .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); + ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); + client = RestClients.create(clientConfiguration).rest(); } @Test - public void givenJsonString_whenJavaObject_thenIndexDocument() { + public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexResponse response = client - .prepareIndex("people", "Doe") - .setSource(jsonObject, XContentType.JSON) - .get(); - String index = response.getIndex(); - String type = response.getType(); + IndexRequest request = new IndexRequest("people"); + request.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(request, RequestOptions.DEFAULT); + String index = response.getIndex(); + assertEquals(Result.CREATED, response.getResult()); assertEquals(index, "people"); - assertEquals(type, "Doe"); } @Test - public void givenDocumentId_whenJavaObject_thenDeleteDocument() { + public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexResponse response = client - .prepareIndex("people", "Doe") - .setSource(jsonObject, XContentType.JSON) - .get(); + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(jsonObject, XContentType.JSON); + + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); String id = response.getId(); - DeleteResponse deleteResponse = client - .prepareDelete("people", "Doe", id) - .get(); + + DeleteRequest deleteRequest = new DeleteRequest("people"); + deleteRequest.id(id); + + DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); assertEquals(Result.DELETED,deleteResponse.getResult()); } @Test - public void givenSearchRequest_whenMatchAll_thenReturnAllResults() { - SearchResponse response = client - .prepareSearch() - .execute() - .actionGet(); + public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { + SearchRequest searchRequest = new SearchRequest(); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); SearchHit[] searchHits = response .getHits() .getHits(); @@ -98,42 +98,42 @@ public class ElasticSearchManualTest { } @Test - public void givenSearchParameters_thenReturnResults() { - SearchResponse response = client - .prepareSearch() - .setTypes() - .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setPostFilter(QueryBuilders - .rangeQuery("age") - .from(5) - .to(15)) - .setFrom(0) - .setSize(60) - .setExplain(true) - .execute() - .actionGet(); + public void givenSearchParameters_thenReturnResults() throws Exception { + SearchSourceBuilder builder = new SearchSourceBuilder() + .postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) + .from(0) + .size(60) + .explain(true); + + SearchRequest searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse response2 = client - .prepareSearch() - .setTypes() - .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setPostFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) - .setFrom(0) - .setSize(60) - .setExplain(true) - .execute() - .actionGet(); + builder = new SearchSourceBuilder() + .postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) + .from(0) + .size(60) + .explain(true); + + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + + builder = new SearchSourceBuilder() + .postFilter(QueryBuilders.matchQuery("John", "Name*")) + .from(0) + .size(60) + .explain(true); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse response3 = client - .prepareSearch() - .setTypes() - .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setPostFilter(QueryBuilders.matchQuery("John", "Name*")) - .setFrom(0) - .setSize(60) - .setExplain(true) - .execute() - .actionGet(); response2.getHits(); response3.getHits(); @@ -151,10 +151,11 @@ public class ElasticSearchManualTest { .field("salary", "11500") .field("age", "10") .endObject(); - IndexResponse response = client - .prepareIndex("people", "Doe") - .setSource(builder) - .get(); + + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(builder); + + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); assertEquals(Result.CREATED, response.getResult()); } 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 f9a42050b6..698f605eeb 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,4 +1,5 @@ package com.baeldung.elasticsearch; + import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -12,8 +13,7 @@ import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.common.geo.ShapeRelation; -import org.elasticsearch.common.geo.builders.ShapeBuilders; +import org.elasticsearch.common.geo.builders.EnvelopeBuilder; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; @@ -23,13 +23,13 @@ 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.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; -import com.vividsolutions.jts.geom.Coordinate; /** * @@ -75,13 +75,12 @@ public class GeoQueriesManualTest { .indices() .prepareRefresh(WONDERS_OF_WORLD) .get(); - - Coordinate topLeft =new Coordinate(74, 31.2); - Coordinate bottomRight =new Coordinate(81.1, 24); + + Coordinate topLeft = new Coordinate(74, 31.2); + Coordinate bottomRight = new Coordinate(81.1, 24); QueryBuilder qb = QueryBuilders - .geoShapeQuery("region", ShapeBuilders.newEnvelope(topLeft, bottomRight)) - .relation(ShapeRelation.WITHIN); - + .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); + //.relation(ShapeRelation.WITHIN)); SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) .setTypes(WONDERS) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 5e24d8398c..0125a4b624 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -22,6 +22,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; @@ -159,7 +160,7 @@ public class ElasticSearchQueryManualTest { @Test public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") - .order(Terms.Order.count(false)); + .order(BucketOrder.count(false)); final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) .execute().actionGet(); From c1d547c5f0a43e79497d3e80f349bc7d1f5467e5 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 13 Apr 2020 13:02:12 -0400 Subject: [PATCH 02/12] fixed tests --- .../spring-data-elasticsearch/pom.xml | 2 +- .../elasticsearch/ElasticSearchManualTest.java | 10 +++++++--- .../elasticsearch/GeoQueriesManualTest.java | 3 +-- .../spring/data/es/ElasticSearchManualTest.java | 14 +++++++------- .../data/es/ElasticSearchQueryManualTest.java | 15 +++++++-------- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index f322e46c0e..36cd2d4171 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -84,7 +84,7 @@ 3.2.6.RELEASE 4.5.2 - 7.6.2 + 6.8.8 1.2.47 0.6 1.13 diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index 6dc1c42dc1..5928b0ef8e 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -58,7 +58,7 @@ public class ElasticSearchManualTest { @Test public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people"); + IndexRequest request = new IndexRequest("people", "Doe"); request.source(jsonObject, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); @@ -71,7 +71,7 @@ public class ElasticSearchManualTest { @Test public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people"); + IndexRequest indexRequest = new IndexRequest("people", "Doe"); indexRequest.source(jsonObject, XContentType.JSON); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); @@ -79,6 +79,7 @@ public class ElasticSearchManualTest { DeleteRequest deleteRequest = new DeleteRequest("people"); deleteRequest.id(id); + deleteRequest.type("Doe"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); @@ -95,6 +96,8 @@ public class ElasticSearchManualTest { List results = Arrays.stream(searchHits) .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) .collect(Collectors.toList()); + + results.forEach(System.out::println); } @Test @@ -140,6 +143,7 @@ public class ElasticSearchManualTest { final List results = Arrays.stream(response.getHits().getHits()) .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) .collect(Collectors.toList()); + results.forEach(System.out::println); } @Test @@ -152,7 +156,7 @@ public class ElasticSearchManualTest { .field("age", "10") .endObject(); - IndexRequest indexRequest = new IndexRequest("people"); + IndexRequest indexRequest = new IndexRequest("people", "Doe"); indexRequest.source(builder); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); 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 698f605eeb..45c643b636 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 @@ -79,8 +79,7 @@ public class GeoQueriesManualTest { Coordinate topLeft = new Coordinate(74, 31.2); Coordinate bottomRight = new Coordinate(81.1, 24); QueryBuilder qb = QueryBuilders - .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); - //.relation(ShapeRelation.WITHIN)); + .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) .setTypes(WONDERS) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index bed2e2ff25..7c60c98ab7 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -10,23 +10,23 @@ import static org.junit.Assert.assertNotNull; import java.util.List; +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; - /** * * This Manual test requires: @@ -39,7 +39,7 @@ import com.baeldung.spring.data.es.service.ArticleService; public class ElasticSearchManualTest { @Autowired - private ElasticsearchTemplate elasticsearchTemplate; + private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired private ArticleService articleService; diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 0125a4b624..b6146ac415 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -14,6 +14,11 @@ import static org.junit.Assert.assertEquals; import java.util.List; import java.util.Map; +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; + import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; @@ -25,23 +30,17 @@ import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; -import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; - /** * * This Manual test requires: @@ -54,7 +53,7 @@ import com.baeldung.spring.data.es.service.ArticleService; public class ElasticSearchQueryManualTest { @Autowired - private ElasticsearchTemplate elasticsearchTemplate; + private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired private ArticleService articleService; From 2c5c556fedd3610ca3c146bbd58b67a4421e87d4 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 13 Apr 2020 13:35:47 -0400 Subject: [PATCH 03/12] fixed tests --- .../spring-data-elasticsearch/pom.xml | 5 - .../com/baeldung/SpringContextManualTest.java | 3 +- .../ElasticSearchManualTest.java | 195 ++++++++-------- .../elasticsearch/GeoQueriesManualTest.java | 213 +++++++----------- .../data/es/ElasticSearchManualTest.java | 24 +- .../data/es/ElasticSearchQueryManualTest.java | 36 ++- 6 files changed, 211 insertions(+), 265 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 36cd2d4171..7d7ffb525d 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -37,11 +37,6 @@ ${spring-data-elasticsearch.version} - - org.elasticsearch.client - elasticsearch-rest-high-level-client - ${elasticsearch.version} - com.alibaba fastjson diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index c69deeb77c..2e9352b081 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -9,8 +9,7 @@ import com.baeldung.spring.data.es.config.Config; /** * - * This Manual test requires: - * * Elasticsearch instance running on host + * This Manual test requires: * Elasticsearch instance running on host * */ @RunWith(SpringJUnit4ClassRunner.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index 5928b0ef8e..f8f70c58a4 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -35,132 +35,113 @@ import org.springframework.data.elasticsearch.client.RestClients; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * */ public class ElasticSearchManualTest { - private List listOfPersons = new ArrayList<>(); - private RestHighLevelClient client = null; + private List listOfPersons = new ArrayList<>(); + private RestHighLevelClient client = null; - @Before - public void setUp() throws UnknownHostException { - Person person1 = new Person(10, "John Doe", new Date()); - Person person2 = new Person(25, "Janette Doe", new Date()); - listOfPersons.add(person1); - listOfPersons.add(person2); - - ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); - client = RestClients.create(clientConfiguration).rest(); - } + @Before + public void setUp() throws UnknownHostException { + Person person1 = new Person(10, "John Doe", new Date()); + Person person2 = new Person(25, "Janette Doe", new Date()); + listOfPersons.add(person1); + listOfPersons.add(person2); - @Test - public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { - String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people", "Doe"); - request.source(jsonObject, XContentType.JSON); + ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); + client = RestClients.create(clientConfiguration).rest(); + } - IndexResponse response = client.index(request, RequestOptions.DEFAULT); - String index = response.getIndex(); - - assertEquals(Result.CREATED, response.getResult()); - assertEquals(index, "people"); - } + @Test + public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { + String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; + IndexRequest request = new IndexRequest("people", "Doe"); + request.source(jsonObject, XContentType.JSON); - @Test - public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { - String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people", "Doe"); - indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(request, RequestOptions.DEFAULT); + String index = response.getIndex(); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - String id = response.getId(); + assertEquals(Result.CREATED, response.getResult()); + assertEquals(index, "people"); + } - DeleteRequest deleteRequest = new DeleteRequest("people"); - deleteRequest.id(id); - deleteRequest.type("Doe"); + @Test + public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { + String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; + IndexRequest indexRequest = new IndexRequest("people", "Doe"); + indexRequest.source(jsonObject, XContentType.JSON); - DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String id = response.getId(); - assertEquals(Result.DELETED,deleteResponse.getResult()); - } + DeleteRequest deleteRequest = new DeleteRequest("people"); + deleteRequest.id(id); + deleteRequest.type("Doe"); - @Test - public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { - SearchRequest searchRequest = new SearchRequest(); - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchHit[] searchHits = response - .getHits() - .getHits(); - List results = Arrays.stream(searchHits) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); - - results.forEach(System.out::println); - } + DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); - @Test - public void givenSearchParameters_thenReturnResults() throws Exception { - SearchSourceBuilder builder = new SearchSourceBuilder() - .postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) - .from(0) - .size(60) + assertEquals(Result.DELETED, deleteResponse.getResult()); + } + + @Test + public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { + SearchRequest searchRequest = new SearchRequest(); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + SearchHit[] searchHits = response.getHits().getHits(); + List results = Arrays.stream(searchHits).map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); + + results.forEach(System.out::println); + } + + @Test + public void givenSearchParameters_thenReturnResults() throws Exception { + SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) + .from(0).size(60).explain(true); + + SearchRequest searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + + builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) + .from(0).size(60).explain(true); + + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + + builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")).from(0).size(60) .explain(true); - - SearchRequest searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); - - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - builder = new SearchSourceBuilder() - .postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) - .from(0) - .size(60) - .explain(true); + SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + response2.getHits(); + response3.getHits(); - SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + final List results = Arrays.stream(response.getHits().getHits()) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)).collect(Collectors.toList()); + results.forEach(System.out::println); + } - builder = new SearchSourceBuilder() - .postFilter(QueryBuilders.matchQuery("John", "Name*")) - .from(0) - .size(60) - .explain(true); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + @Test + public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test") + .field("salary", "11500").field("age", "10").endObject(); - SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); + IndexRequest indexRequest = new IndexRequest("people", "Doe"); + indexRequest.source(builder); - response2.getHits(); - response3.getHits(); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - final List results = Arrays.stream(response.getHits().getHits()) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); - results.forEach(System.out::println); - } - - @Test - public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { - XContentBuilder builder = XContentFactory - .jsonBuilder() - .startObject() - .field("fullName", "Test") - .field("salary", "11500") - .field("age", "10") - .endObject(); - - IndexRequest indexRequest = new IndexRequest("people", "Doe"); - indexRequest.source(builder); - - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - - assertEquals(Result.CREATED, response.getResult()); - } + assertEquals(Result.CREATED, response.getResult()); + } } 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 45c643b636..e373fef7a6 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 @@ -33,155 +33,110 @@ import com.baeldung.spring.data.es.config.Config; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch - * * and further configurations + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * and further configurations * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class GeoQueriesManualTest { - private static final String WONDERS_OF_WORLD = "wonders-of-world"; - private static final String WONDERS = "Wonders"; + private static final String WONDERS_OF_WORLD = "wonders-of-world"; + private static final String WONDERS = "Wonders"; - @Autowired - private ElasticsearchTemplate elasticsearchTemplate; + @Autowired + private ElasticsearchTemplate elasticsearchTemplate; - @Autowired - private Client client; + @Autowired + private Client client; - @Before - public void setUp() { - String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; - CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); - req.mapping(WONDERS, jsonObject, XContentType.JSON); - client.admin() - .indices() - .create(req) - .actionGet(); - } + @Before + public void setUp() { + String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; + CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); + req.mapping(WONDERS, jsonObject, XContentType.JSON); + client.admin().indices().create(req).actionGet(); + } - @Test - public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException{ - String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - - String tajMahalId = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); - - Coordinate topLeft = new Coordinate(74, 31.2); - Coordinate bottomRight = new Coordinate(81.1, 24); - QueryBuilder qb = QueryBuilders - .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); + @Test + public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException { + String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); + String tajMahalId = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); + Coordinate topLeft = new Coordinate(74, 31.2); + Coordinate bottomRight = new Coordinate(81.1, 24); + QueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); - assertTrue(ids.contains(tajMahalId)); - } + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); - @Test - public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { - String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - String pyramidsOfGizaId = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); - QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location") - .setCorners(31,30,28,32); - - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(pyramidsOfGizaId)); - } + assertTrue(ids.contains(tajMahalId)); + } - @Test - public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { - String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - String lighthouseOfAlexandriaId = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); + @Test + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); + String pyramidsOfGizaId = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - QueryBuilder qb = QueryBuilders.geoDistanceQuery("location") - .point(29.976, 31.131) - .distance(10, DistanceUnit.MILES); + QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(lighthouseOfAlexandriaId)); - } + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(pyramidsOfGizaId)); + } - @Test - public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { - String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - String greatRannOfKutchid = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); + @Test + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); + String lighthouseOfAlexandriaId = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - List allPoints = new ArrayList(); - allPoints.add(new GeoPoint(22.733, 68.859)); - allPoints.add(new GeoPoint(24.733, 68.859)); - allPoints.add(new GeoPoint(23, 70.859)); - QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + QueryBuilder qb = QueryBuilders.geoDistanceQuery("location").point(29.976, 31.131).distance(10, DistanceUnit.MILES); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(greatRannOfKutchid)); - } + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(lighthouseOfAlexandriaId)); + } - @After - public void destroy() { - elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); - } + @Test + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); + String greatRannOfKutchid = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + + List allPoints = new ArrayList(); + allPoints.add(new GeoPoint(22.733, 68.859)); + allPoints.add(new GeoPoint(24.733, 68.859)); + allPoints.add(new GeoPoint(23, 70.859)); + QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(greatRannOfKutchid)); + } + + @After + public void destroy() { + elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 7c60c98ab7..751755689f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -29,9 +29,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * */ @RunWith(SpringJUnit4ClassRunner.class) @@ -88,26 +87,29 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleService - .findByAuthorName(johnSmith.getName(), PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), + PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", + PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", + PageRequest.of(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", + "elasticsearch", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @@ -115,7 +117,7 @@ public class ElasticSearchManualTest { public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) - .build(); + .build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); @@ -142,7 +144,7 @@ public class ElasticSearchManualTest { final String articleTitle = "Spring Data Elasticsearch"; final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); + .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); final long count = articleService.count(); @@ -155,7 +157,7 @@ public class ElasticSearchManualTest { @Test public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index b6146ac415..629f89f3f1 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -20,8 +20,10 @@ import com.baeldung.spring.data.es.model.Author; import com.baeldung.spring.data.es.service.ArticleService; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.client.Client; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; @@ -29,8 +31,9 @@ import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; -import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; +import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -59,7 +62,7 @@ public class ElasticSearchQueryManualTest { private ArticleService articleService; @Autowired - private Client client; + private RestHighLevelClient client; private final Author johnSmith = new Author("John Smith"); private final Author johnDoe = new Author("John Doe"); @@ -141,13 +144,18 @@ public class ElasticSearchQueryManualTest { } @Test - public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() { + public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); - final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) - .execute().actionGet(); + + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest("blog") + .types("article") + .source(builder); + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + final Map results = response.getAggregations().asMap(); - final StringTerms topTags = (StringTerms) results.get("top_tags"); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); final List keys = topTags.getBuckets().stream() .map(MultiBucketsAggregation.Bucket::getKeyAsString) @@ -157,14 +165,20 @@ public class ElasticSearchQueryManualTest { } @Test - public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { + public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") .order(BucketOrder.count(false)); - final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) - .execute().actionGet(); + + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest() + .indices("blog") + .types("article") + .source(builder); + + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); final Map results = response.getAggregations().asMap(); - final StringTerms topTags = (StringTerms) results.get("top_tags"); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); final List keys = topTags.getBuckets().stream() .map(MultiBucketsAggregation.Bucket::getKeyAsString) From 58e44705b28db75112fe6a65b065e365d6a37b82 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 13 Apr 2020 13:47:17 -0400 Subject: [PATCH 04/12] fixed format --- .../spring-data-elasticsearch/pom.xml | 1 - .../data/es/ElasticSearchQueryManualTest.java | 277 +++++++++--------- 2 files changed, 136 insertions(+), 142 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 7d7ffb525d..e2089eb8b1 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -79,7 +79,6 @@ 3.2.6.RELEASE 4.5.2 - 6.8.8 1.2.47 0.6 1.13 diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 629f89f3f1..438d3adf02 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -46,183 +46,178 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class ElasticSearchQueryManualTest { - @Autowired - private ElasticsearchRestTemplate elasticsearchTemplate; + @Autowired + private ElasticsearchRestTemplate elasticsearchTemplate; - @Autowired - private ArticleService articleService; + @Autowired + private ArticleService articleService; - @Autowired - private RestHighLevelClient client; + @Autowired + private RestHighLevelClient client; - private final Author johnSmith = new Author("John Smith"); - private final Author johnDoe = new Author("John Doe"); + private final Author johnSmith = new Author("John Smith"); + private final Author johnDoe = new Author("John Doe"); - @Before - public void before() { - elasticsearchTemplate.deleteIndex(Article.class); - elasticsearchTemplate.createIndex(Article.class); - elasticsearchTemplate.putMapping(Article.class); - elasticsearchTemplate.refresh(Article.class); + @Before + public void before() { + elasticsearchTemplate.deleteIndex(Article.class); + elasticsearchTemplate.createIndex(Article.class); + elasticsearchTemplate.putMapping(Article.class); + elasticsearchTemplate.refresh(Article.class); - Article article = new Article("Spring Data Elasticsearch"); - article.setAuthors(asList(johnSmith, johnDoe)); - article.setTags("elasticsearch", "spring data"); - articleService.save(article); + Article article = new Article("Spring Data Elasticsearch"); + article.setAuthors(asList(johnSmith, johnDoe)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); - article = new Article("Search engines"); - article.setAuthors(asList(johnDoe)); - article.setTags("search engines", "tutorial"); - articleService.save(article); + article = new Article("Search engines"); + article.setAuthors(asList(johnDoe)); + article.setTags("search engines", "tutorial"); + articleService.save(article); - article = new Article("Second Article About Elasticsearch"); - article.setAuthors(asList(johnSmith)); - article.setTags("elasticsearch", "spring data"); - articleService.save(article); + article = new Article("Second Article About Elasticsearch"); + article.setAuthors(asList(johnSmith)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); - article = new Article("Elasticsearch Tutorial"); - article.setAuthors(asList(johnDoe)); - article.setTags("elasticsearch"); - articleService.save(article); - } + article = new Article("Elasticsearch Tutorial"); + article.setAuthors(asList(johnDoe)); + article.setTags("elasticsearch"); + articleService.save(article); + } - @Test - public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - } + @Test + public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } - @Test - public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Engines Solutions")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - assertEquals("Search engines", articles.get(0).getTitle()); - } + @Test + public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) + .build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + assertEquals("Search engines", articles.get(0).getTitle()); + } - @Test - public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "elasticsearch data")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(3, articles.size()); - } + @Test + public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) + .build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(3, articles.size()); + } - @Test - public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { - SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); - List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + @Test + public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { + SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); + List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); - searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) - .build(); - articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(0, articles.size()); - } + searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) + .build(); + articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(0, articles.size()); + } - @Test - public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { - final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); + @Test + public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { + final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), + ScoreMode.None); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(2, articles.size()); - } + assertEquals(2, articles.size()); + } - @Test - public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); - - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest("blog") - .types("article") - .source(builder); + @Test + public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest("blog").types("article").source(builder); - final List keys = topTags.getBuckets().stream() - .map(MultiBucketsAggregation.Bucket::getKeyAsString) - .sorted() - .collect(toList()); - assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); - } + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - @Test - public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") - .order(BucketOrder.count(false)); + final Map results = response.getAggregations().asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest() - .indices("blog") - .types("article") - .source(builder); + final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).sorted() + .collect(toList()); + assertEquals( + asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); + } - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") + .order(BucketOrder.count(false)); - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest().indices("blog").types("article").source(builder); - final List keys = topTags.getBuckets().stream() - .map(MultiBucketsAggregation.Bucket::getKeyAsString) - .collect(toList()); - assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); - } + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - @Test - public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - } + final Map results = response.getAggregations().asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - @Test - public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE) - .prefixLength(3)).build(); + final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString) + .collect(toList()); + assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); + } - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - } + @Test + public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } - @Test - public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(multiMatchQuery("tutorial").field("title").field("tags") - .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); + @Test + public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery( + matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)) + .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(2, articles.size()); - } + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } - @Test - public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { - final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) - .filter(termQuery("tags", "elasticsearch")); + @Test + public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery( + multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) + .build(); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) - .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(2, articles.size()); + } - assertEquals(2, articles.size()); - } + @Test + public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { + final QueryBuilder builder = boolQuery() + .must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) + .filter(termQuery("tags", "elasticsearch")); + + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + + assertEquals(2, articles.size()); + } } From 95e4e7326d8cd504f0c749e460fd887b6734a53c Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 20 Apr 2020 20:29:21 -0400 Subject: [PATCH 05/12] removed ArticleService --- .../data/es/service/ArticleService.java | 28 -------- .../data/es/service/ArticleServiceImpl.java | 67 ------------------- .../data/es/ElasticSearchManualTest.java | 32 ++++----- .../data/es/ElasticSearchQueryManualTest.java | 12 ++-- 4 files changed, 22 insertions(+), 117 deletions(-) delete mode 100644 persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java delete mode 100644 persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java deleted file mode 100644 index a0f72aa5f7..0000000000 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.spring.data.es.service; - -import java.util.Optional; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; - -import com.baeldung.spring.data.es.model.Article; - -public interface ArticleService { - Article save(Article article); - - Optional
findOne(String id); - - Iterable
findAll(); - - Page
findByAuthorName(String name, Pageable pageable); - - Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable); - - Page
findByFilteredTagQuery(String tag, Pageable pageable); - - Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable); - - long count(); - - void delete(Article article); -} diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java deleted file mode 100644 index 5064f16508..0000000000 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.baeldung.spring.data.es.service; - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.repository.ArticleRepository; - -@Service -public class ArticleServiceImpl implements ArticleService { - - private final ArticleRepository articleRepository; - - @Autowired - public ArticleServiceImpl(ArticleRepository articleRepository) { - this.articleRepository = articleRepository; - } - - @Override - public Article save(Article article) { - return articleRepository.save(article); - } - - @Override - public Optional
findOne(String id) { - return articleRepository.findById(id); - } - - @Override - public Iterable
findAll() { - return articleRepository.findAll(); - } - - @Override - public Page
findByAuthorName(String name, Pageable pageable) { - return articleRepository.findByAuthorsName(name, pageable); - } - - @Override - public Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable) { - return articleRepository.findByAuthorsNameUsingCustomQuery(name, pageable); - } - - @Override - public Page
findByFilteredTagQuery(String tag, Pageable pageable) { - return articleRepository.findByFilteredTagQuery(tag, pageable); - } - - @Override - public Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable) { - return articleRepository.findByAuthorsNameAndFilteredTagQuery(name, tag, pageable); - } - - @Override - public long count() { - return articleRepository.count(); - } - - @Override - public void delete(Article article) { - articleRepository.delete(article); - } -} diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 751755689f..6e1d7d1b7d 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -13,7 +13,7 @@ import java.util.List; import com.baeldung.spring.data.es.config.Config; import com.baeldung.spring.data.es.model.Article; import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; +import com.baeldung.spring.data.es.repository.ArticleRepository; import org.junit.Before; import org.junit.Test; @@ -41,7 +41,7 @@ public class ElasticSearchManualTest { private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired - private ArticleService articleService; + private ArticleRepository articleRepository; private final Author johnSmith = new Author("John Smith"); private final Author johnDoe = new Author("John Doe"); @@ -55,22 +55,22 @@ public class ElasticSearchManualTest { Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Search engines"); article.setAuthors(asList(johnDoe)); article.setTags("search engines", "tutorial"); - articleService.save(article); + articleRepository.save(article); article = new Article("Second Article About Elasticsearch"); article.setAuthors(asList(johnSmith)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Elasticsearch Tutorial"); article.setAuthors(asList(johnDoe)); article.setTags("elasticsearch"); - articleService.save(article); + articleRepository.save(article); } @Test @@ -80,35 +80,35 @@ public class ElasticSearchManualTest { Article article = new Article("Making Search Elastic"); article.setAuthors(authors); - article = articleService.save(article); + article = articleRepository.save(article); assertNotNull(article.getId()); } @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), + final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", + final Page
articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", + final Page
articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", + final Page
articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @@ -133,9 +133,9 @@ public class ElasticSearchManualTest { final Article article = articles.get(0); final String newTitle = "Getting started with Search Engines"; article.setTitle(newTitle); - articleService.save(article); + articleRepository.save(article); - assertEquals(newTitle, articleService.findOne(article.getId()).get().getTitle()); + assertEquals(newTitle, articleRepository.findById(article.getId()).get().getTitle()); } @Test @@ -147,11 +147,11 @@ public class ElasticSearchManualTest { .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); - final long count = articleService.count(); + final long count = articleRepository.count(); - articleService.delete(articles.get(0)); + articleRepository.delete(articles.get(0)); - assertEquals(count - 1, articleService.count()); + assertEquals(count - 1, articleRepository.count()); } @Test diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 438d3adf02..ff804daa20 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -17,7 +17,7 @@ import java.util.Map; import com.baeldung.spring.data.es.config.Config; import com.baeldung.spring.data.es.model.Article; import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; +import com.baeldung.spring.data.es.repository.ArticleRepository; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchRequest; @@ -58,7 +58,7 @@ public class ElasticSearchQueryManualTest { private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired - private ArticleService articleService; + private ArticleRepository articleRepository; @Autowired private RestHighLevelClient client; @@ -76,22 +76,22 @@ public class ElasticSearchQueryManualTest { Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Search engines"); article.setAuthors(asList(johnDoe)); article.setTags("search engines", "tutorial"); - articleService.save(article); + articleRepository.save(article); article = new Article("Second Article About Elasticsearch"); article.setAuthors(asList(johnSmith)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Elasticsearch Tutorial"); article.setAuthors(asList(johnDoe)); article.setTags("elasticsearch"); - articleService.save(article); + articleRepository.save(article); } @Test From 6f9e27dc5957a4a30549b8828d619ccda813e365 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Wed, 29 Apr 2020 21:23:48 -0400 Subject: [PATCH 06/12] fixed most tests --- .../spring-data-elasticsearch/pom.xml | 27 ++-- .../com/baeldung/SpringContextManualTest.java | 6 +- .../ElasticSearchManualTest.java | 53 ++++--- .../elasticsearch/GeoQueriesManualTest.java | 136 ++++++++++++------ .../data/es/ElasticSearchManualTest.java | 51 +++---- .../data/es/ElasticSearchQueryManualTest.java | 114 +++++++++------ 6 files changed, 237 insertions(+), 150 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index e2089eb8b1..72860cb136 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -52,13 +52,13 @@ org.locationtech.jts jts-core - 1.16.1 - - - - org.apache.logging.log4j - log4j-core - ${log4j.version} + 1.15.0 + + + xerces + xercesImpl + + @@ -77,12 +77,19 @@ - 3.2.6.RELEASE + 4.0.0.RC1 4.5.2 1.2.47 - 0.6 + 0.7 1.13 - 2.9.1 + + + repository.spring.milestone + Spring Milestone Repository + https://repo.spring.io/milestone + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index 2e9352b081..5087862061 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -8,9 +8,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; /** - * - * This Manual test requires: * Elasticsearch instance running on host - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index f8f70c58a4..3402a10b81 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -9,12 +9,15 @@ import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import com.alibaba.fastjson.JSON; import org.elasticsearch.action.DocWriteResponse.Result; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; @@ -34,10 +37,9 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ public class ElasticSearchManualTest { private List listOfPersons = new ArrayList<>(); @@ -57,28 +59,35 @@ public class ElasticSearchManualTest { @Test public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people", "Doe"); + IndexRequest request = new IndexRequest("people"); request.source(jsonObject, XContentType.JSON); - + IndexResponse response = client.index(request, RequestOptions.DEFAULT); String index = response.getIndex(); - + long version = response.getVersion(); + assertEquals(Result.CREATED, response.getResult()); - assertEquals(index, "people"); + assertEquals(1, version); + assertEquals("people", index); } @Test public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people", "Doe"); + IndexRequest indexRequest = new IndexRequest("people"); indexRequest.source(jsonObject, XContentType.JSON); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); String id = response.getId(); + GetRequest getRequest = new GetRequest("people"); + getRequest.id(id); + + GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); + System.out.println(getResponse.getSourceAsString()); + DeleteRequest deleteRequest = new DeleteRequest("people"); deleteRequest.id(id); - deleteRequest.type("Doe"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); @@ -98,8 +107,8 @@ public class ElasticSearchManualTest { @Test public void givenSearchParameters_thenReturnResults() throws Exception { - SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) - .from(0).size(60).explain(true); + SearchSourceBuilder builder = + new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)); SearchRequest searchRequest = new SearchRequest(); searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); @@ -107,8 +116,7 @@ public class ElasticSearchManualTest { SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) - .from(0).size(60).explain(true); + builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")); searchRequest = new SearchRequest(); searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); @@ -116,8 +124,7 @@ public class ElasticSearchManualTest { SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")).from(0).size(60) - .explain(true); + builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")); searchRequest = new SearchRequest(); searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); searchRequest.source(builder); @@ -126,9 +133,15 @@ public class ElasticSearchManualTest { response2.getHits(); response3.getHits(); - - final List results = Arrays.stream(response.getHits().getHits()) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)).collect(Collectors.toList()); + + final List results = + Stream.of(response.getHits().getHits(), + response2.getHits().getHits(), + response3.getHits().getHits()) + .flatMap(Arrays::stream) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); + results.forEach(System.out::println); } @@ -137,7 +150,7 @@ public class ElasticSearchManualTest { XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test") .field("salary", "11500").field("age", "10").endObject(); - IndexRequest indexRequest = new IndexRequest("people", "Doe"); + IndexRequest indexRequest = new IndexRequest("people"); indexRequest.source(builder); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); 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 e373fef7a6..bd54183263 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 @@ -8,71 +8,84 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; +import com.baeldung.spring.data.es.config.Config; + +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.Client; +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.geo.builders.EnvelopeBuilder; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentType; +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.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.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; - /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch * and further configurations - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class GeoQueriesManualTest { private static final String WONDERS_OF_WORLD = "wonders-of-world"; - private static final String WONDERS = "Wonders"; @Autowired - private ElasticsearchTemplate elasticsearchTemplate; - - @Autowired - private Client client; + private RestHighLevelClient client; @Before - public void setUp() { - String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; + 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(WONDERS, jsonObject, XContentType.JSON); - client.admin().indices().create(req).actionGet(); + req.mapping(jsonObject, XContentType.JSON); + + 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]]}}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); String tajMahalId = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + + 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); - QueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); + + GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", + new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); + qb.relation(ShapeRelation.INTERSECTS); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); + 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()); @@ -81,62 +94,93 @@ public class GeoQueriesManualTest { } @Test - public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception { String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); + + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String pyramidsOfGizaId = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); + 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)); } @Test - public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception { String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); + + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String lighthouseOfAlexandriaId = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - QueryBuilder qb = QueryBuilders.geoDistanceQuery("location").point(29.976, 31.131).distance(10, DistanceUnit.MILES); + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + + QueryBuilder qb = + QueryBuilders.geoDistanceQuery("location") + .point(29.976, 31.131) + .distance(10, DistanceUnit.MILES); + + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) .collect(Collectors.toList()); assertTrue(ids.contains(lighthouseOfAlexandriaId)); } @Test - public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception { String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); - String greatRannOfKutchid = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + + String greatRannOfKutchid = response.getId(); + + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + List allPoints = new ArrayList(); allPoints.add(new GeoPoint(22.733, 68.859)); allPoints.add(new GeoPoint(24.733, 68.859)); allPoints.add(new GeoPoint(23, 70.859)); QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); + 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(greatRannOfKutchid)); } @After - public void destroy() { - elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); + public void destroy() throws Exception { + DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD); + client.indices().delete(deleteIndex, RequestOptions.DEFAULT); } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 6e1d7d1b7d..e1c0994d3e 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -22,16 +22,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; +import org.springframework.data.elasticsearch.core.SearchHits; +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; -import org.springframework.data.elasticsearch.core.query.SearchQuery; +import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -48,8 +49,8 @@ public class ElasticSearchManualTest { @Before public void before() { - elasticsearchTemplate.deleteIndex(Article.class); - elasticsearchTemplate.createIndex(Article.class); + elasticsearchTemplate.indexOps(Article.class).delete(); + elasticsearchTemplate.indexOps(Article.class).create(); // don't call putMapping() to test the default mappings Article article = new Article("Spring Data Elasticsearch"); @@ -86,7 +87,6 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); @@ -115,22 +115,23 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { - - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) + final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); } @Test public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(1, articles.size()); + assertEquals(1, articles.getTotalHits()); - final Article article = articles.get(0); + final Article article = articles.getSearchHit(0).getContent(); final String newTitle = "Getting started with Search Engines"; article.setTitle(newTitle); articleRepository.save(article); @@ -140,25 +141,27 @@ public class ElasticSearchManualTest { @Test public void givenSavedDoc_whenDelete_thenRemovedFromIndex() { - final String articleTitle = "Spring Data Elasticsearch"; - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final Query searchQuery = new NativeSearchQueryBuilder() .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); final long count = articleRepository.count(); - articleRepository.delete(articles.get(0)); + articleRepository.delete(articles.getSearchHit(0).getContent()); assertEquals(count - 1, articleRepository.count()); } @Test public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final Query searchQuery = new NativeSearchQueryBuilder() .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index ff804daa20..2300ff50db 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -2,7 +2,6 @@ package com.baeldung.spring.data.es; import static java.util.Arrays.asList; import static java.util.stream.Collectors.toList; -import static org.elasticsearch.index.query.Operator.AND; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; @@ -26,6 +25,7 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.query.MultiMatchQueryBuilder; +import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -39,16 +39,17 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; +import org.springframework.data.elasticsearch.core.SearchHits; +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; +import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; -import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -68,10 +69,10 @@ public class ElasticSearchQueryManualTest { @Before public void before() { - elasticsearchTemplate.deleteIndex(Article.class); - elasticsearchTemplate.createIndex(Article.class); - elasticsearchTemplate.putMapping(Article.class); - elasticsearchTemplate.refresh(Article.class); + elasticsearchTemplate.indexOps(Article.class).delete(); + elasticsearchTemplate.indexOps(Article.class).create(); + elasticsearchTemplate.indexOps(Article.class).createMapping(); + elasticsearchTemplate.indexOps(Article.class).refresh();; Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); @@ -96,40 +97,55 @@ public class ElasticSearchQueryManualTest { @Test public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "Search engines").operator(Operator.AND)).build(); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); } @Test public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) + final NativeSearchQuery searchQuery = + new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - assertEquals("Search engines", articles.get(0).getTitle()); + + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); + assertEquals("Search engines", articles.getSearchHit(0).getContent().getTitle()); } @Test public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) + final NativeSearchQuery searchQuery = + new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(3, articles.size()); + + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(3, articles.getTotalHits()); } @Test public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { - SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); - List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")) + .build(); - searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) - .build(); - articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(0, articles.size()); + SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); + + searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title.verbatim", "Second Article About")) + .build(); + + articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(0, articles.getTotalHits()); } @Test @@ -137,10 +153,10 @@ public class ElasticSearchQueryManualTest { final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(2, articles.size()); + assertEquals(2, articles.getTotalHits()); } @Test @@ -148,7 +164,7 @@ public class ElasticSearchQueryManualTest { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest("blog").types("article").source(builder); + final SearchRequest searchRequest = new SearchRequest("blog").source(builder); final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); @@ -167,7 +183,7 @@ public class ElasticSearchQueryManualTest { .order(BucketOrder.count(false)); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest().indices("blog").types("article").source(builder); + final SearchRequest searchRequest = new SearchRequest().indices("blog").source(builder); final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); @@ -181,32 +197,36 @@ public class ElasticSearchQueryManualTest { @Test public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); } @Test public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery( - matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)) + matchQuery("title", "spring date elasticserch").operator(Operator.AND).fuzziness(Fuzziness.ONE).prefixLength(3)) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); } @Test public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery( multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(2, articles.size()); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(2, articles.getTotalHits()); } @Test @@ -215,9 +235,9 @@ public class ElasticSearchQueryManualTest { .must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) .filter(termQuery("tags", "elasticsearch")); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(2, articles.size()); + assertEquals(2, articles.getTotalHits()); } } From 1ed9c7db328be10c999c321f3dbca82f29b011dc Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 12 May 2020 21:41:51 -0400 Subject: [PATCH 07/12] fixed mapping and tests --- .../com/baeldung/spring/data/es/model/Author.java | 5 +++++ .../com/baeldung/SpringContextManualTest.java | 4 +++- .../elasticsearch/ElasticSearchManualTest.java | 4 +++- .../elasticsearch/GeoQueriesManualTest.java | 4 +++- .../spring/data/es/ElasticSearchManualTest.java | 14 +++++++++----- .../data/es/ElasticSearchQueryManualTest.java | 15 +++++++++------ 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java index 38f50e1614..1d596cd92b 100644 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java +++ b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java @@ -1,7 +1,12 @@ package com.baeldung.spring.data.es.model; +import static org.springframework.data.elasticsearch.annotations.FieldType.Text; + +import org.springframework.data.elasticsearch.annotations.Field; + public class Author { + @Field(type = Text) private String name; public Author() { diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index 5087862061..27b41649ee 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -8,7 +8,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index 3402a10b81..d19a7a379f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -37,7 +37,9 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ 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 bd54183263..ed67818d6c 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 @@ -39,7 +39,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index e1c0994d3e..87eab00da0 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -15,6 +15,7 @@ import com.baeldung.spring.data.es.model.Article; import com.baeldung.spring.data.es.model.Author; import com.baeldung.spring.data.es.repository.ArticleRepository; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -30,7 +31,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @@ -49,10 +52,6 @@ public class ElasticSearchManualTest { @Before public void before() { - elasticsearchTemplate.indexOps(Article.class).delete(); - elasticsearchTemplate.indexOps(Article.class).create(); - // don't call putMapping() to test the default mappings - Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); @@ -74,6 +73,11 @@ public class ElasticSearchManualTest { articleRepository.save(article); } + @After + public void after() { + articleRepository.deleteAll(); + } + @Test public void givenArticleService_whenSaveArticle_thenIdIsAssigned() { final List authors = asList(new Author("John Smith"), johnDoe); diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 2300ff50db..b9de9d51af 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -34,6 +34,7 @@ import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,7 +48,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @@ -69,11 +72,6 @@ public class ElasticSearchQueryManualTest { @Before public void before() { - elasticsearchTemplate.indexOps(Article.class).delete(); - elasticsearchTemplate.indexOps(Article.class).create(); - elasticsearchTemplate.indexOps(Article.class).createMapping(); - elasticsearchTemplate.indexOps(Article.class).refresh();; - Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); @@ -95,6 +93,11 @@ public class ElasticSearchQueryManualTest { articleRepository.save(article); } + @After + public void after() { + articleRepository.deleteAll(); + } + @Test public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() From 40aa75eec10e32fd16237a5f0fbb3f583ca012c2 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:21:51 -0400 Subject: [PATCH 08/12] fixed formatting according to eclipse formatter --- .../spring/data/es/config/Config.java | 21 +- .../com/baeldung/SpringContextManualTest.java | 7 +- .../ElasticSearchManualTest.java | 199 ++++++------ .../elasticsearch/GeoQueriesManualTest.java | 228 ++++++------- .../data/es/ElasticSearchManualTest.java | 55 ++-- .../data/es/ElasticSearchQueryManualTest.java | 304 +++++++++--------- 6 files changed, 419 insertions(+), 395 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java index f98342486f..51bbe73e9e 100644 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java +++ b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java @@ -15,15 +15,18 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea @ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" }) public class Config { - @Bean - RestHighLevelClient client() { - ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); + @Bean + RestHighLevelClient client() { + ClientConfiguration clientConfiguration = ClientConfiguration.builder() + .connectedTo("localhost:9200") + .build(); - return RestClients.create(clientConfiguration).rest(); - } + return RestClients.create(clientConfiguration) + .rest(); + } - @Bean - public ElasticsearchOperations elasticsearchTemplate() { - return new ElasticsearchRestTemplate(client()); - } + @Bean + public ElasticsearchOperations elasticsearchTemplate() { + return new ElasticsearchRestTemplate(client()); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index 27b41649ee..bff910cf93 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -8,11 +8,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index d19a7a379f..e4b3f6d801 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -37,126 +37,137 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ public class ElasticSearchManualTest { - private List listOfPersons = new ArrayList<>(); - private RestHighLevelClient client = null; + private List listOfPersons = new ArrayList<>(); + private RestHighLevelClient client = null; - @Before - public void setUp() throws UnknownHostException { - Person person1 = new Person(10, "John Doe", new Date()); - Person person2 = new Person(25, "Janette Doe", new Date()); - listOfPersons.add(person1); - listOfPersons.add(person2); + @Before + public void setUp() throws UnknownHostException { + Person person1 = new Person(10, "John Doe", new Date()); + Person person2 = new Person(25, "Janette Doe", new Date()); + listOfPersons.add(person1); + listOfPersons.add(person2); - ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); - client = RestClients.create(clientConfiguration).rest(); - } + ClientConfiguration clientConfiguration = ClientConfiguration.builder() + .connectedTo("localhost:9200") + .build(); + client = RestClients.create(clientConfiguration) + .rest(); + } - @Test - public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { - String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people"); - request.source(jsonObject, XContentType.JSON); - - IndexResponse response = client.index(request, RequestOptions.DEFAULT); - String index = response.getIndex(); - long version = response.getVersion(); - - assertEquals(Result.CREATED, response.getResult()); - assertEquals(1, version); - assertEquals("people", index); - } + @Test + public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { + String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; + IndexRequest request = new IndexRequest("people"); + request.source(jsonObject, XContentType.JSON); - @Test - public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { - String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people"); - indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(request, RequestOptions.DEFAULT); + String index = response.getIndex(); + long version = response.getVersion(); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - String id = response.getId(); + assertEquals(Result.CREATED, response.getResult()); + assertEquals(1, version); + assertEquals("people", index); + } - GetRequest getRequest = new GetRequest("people"); - getRequest.id(id); + @Test + public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { + String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(jsonObject, XContentType.JSON); - GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); - System.out.println(getResponse.getSourceAsString()); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String id = response.getId(); - DeleteRequest deleteRequest = new DeleteRequest("people"); - deleteRequest.id(id); + GetRequest getRequest = new GetRequest("people"); + getRequest.id(id); - DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); + GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); + System.out.println(getResponse.getSourceAsString()); - assertEquals(Result.DELETED, deleteResponse.getResult()); - } + DeleteRequest deleteRequest = new DeleteRequest("people"); + deleteRequest.id(id); - @Test - public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { - SearchRequest searchRequest = new SearchRequest(); - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchHit[] searchHits = response.getHits().getHits(); - List results = Arrays.stream(searchHits).map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); + DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); - results.forEach(System.out::println); - } + assertEquals(Result.DELETED, deleteResponse.getResult()); + } - @Test - public void givenSearchParameters_thenReturnResults() throws Exception { - SearchSourceBuilder builder = - new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)); + @Test + public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { + SearchRequest searchRequest = new SearchRequest(); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + SearchHit[] searchHits = response.getHits() + .getHits(); + List results = Arrays.stream(searchHits) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); - SearchRequest searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + results.forEach(System.out::println); + } - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenSearchParameters_thenReturnResults() throws Exception { + SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age") + .from(5) + .to(15)); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")); + SearchRequest searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); + SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); - response2.getHits(); - response3.getHits(); - - final List results = - Stream.of(response.getHits().getHits(), - response2.getHits().getHits(), - response3.getHits().getHits()) - .flatMap(Arrays::stream) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); - - results.forEach(System.out::println); - } + builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - @Test - public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test") - .field("salary", "11500").field("age", "10").endObject(); + SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); - IndexRequest indexRequest = new IndexRequest("people"); - indexRequest.source(builder); + response2.getHits(); + response3.getHits(); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + final List results = Stream.of(response.getHits() + .getHits(), + response2.getHits() + .getHits(), + response3.getHits() + .getHits()) + .flatMap(Arrays::stream) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); - assertEquals(Result.CREATED, response.getResult()); - } + results.forEach(System.out::println); + } + + @Test + public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .field("fullName", "Test") + .field("salary", "11500") + .field("age", "10") + .endObject(); + + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(builder); + + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + + assertEquals(Result.CREATED, response.getResult()); + } } 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 ed67818d6c..a36e08d6d2 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 @@ -39,150 +39,162 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class GeoQueriesManualTest { - private static final String WONDERS_OF_WORLD = "wonders-of-world"; + private static final String WONDERS_OF_WORLD = "wonders-of-world"; - @Autowired - private RestHighLevelClient client; + @Autowired + private RestHighLevelClient client; - @Before - 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); + @Before + public void setUp() throws Exception { + String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}"; - client.indices().create(req, RequestOptions.DEFAULT); - } + CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); + req.mapping(jsonObject, XContentType.JSON); - @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); + client.indices() + .create(req, RequestOptions.DEFAULT); + } - String tajMahalId = response.getId(); + @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); - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + String tajMahalId = response.getId(); - 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); + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); - - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + Coordinate topLeft = new Coordinate(74, 31.2); + Coordinate bottomRight = new Coordinate(81.1, 24); - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); + GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); + qb.relation(ShapeRelation.INTERSECTS); - assertTrue(ids.contains(tajMahalId)); - } + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); - @Test - public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception { - String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); - indexRequest.source(jsonObject, XContentType.JSON); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); - String pyramidsOfGizaId = response.getId(); + assertTrue(ids.contains(tajMahalId)); + } - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + @Test + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception { + String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; - QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); + String pyramidsOfGizaId = response.getId(); - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(pyramidsOfGizaId)); - } + QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location") + .setCorners(31, 30, 28, 32); - @Test - public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception { - String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - - IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); - indexRequest.source(jsonObject, XContentType.JSON); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); - String lighthouseOfAlexandriaId = response.getId(); + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(pyramidsOfGizaId)); + } - QueryBuilder qb = - QueryBuilders.geoDistanceQuery("location") - .point(29.976, 31.131) - .distance(10, DistanceUnit.MILES); + @Test + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception { + String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + String lighthouseOfAlexandriaId = response.getId(); - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(lighthouseOfAlexandriaId)); - } + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); - @Test - public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception { - String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; + QueryBuilder qb = QueryBuilders.geoDistanceQuery("location") + .point(29.976, 31.131) + .distance(10, DistanceUnit.MILES); - IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); - indexRequest.source(jsonObject, XContentType.JSON); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - - String greatRannOfKutchid = response.getId(); + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); - - List allPoints = new ArrayList(); - allPoints.add(new GeoPoint(22.733, 68.859)); - allPoints.add(new GeoPoint(24.733, 68.859)); - allPoints.add(new GeoPoint(23, 70.859)); - QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(lighthouseOfAlexandriaId)); + } - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception { + String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(greatRannOfKutchid)); - } + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - @After - public void destroy() throws Exception { - DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD); - client.indices().delete(deleteIndex, RequestOptions.DEFAULT); - } + String greatRannOfKutchid = response.getId(); + + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); + + List allPoints = new ArrayList(); + allPoints.add(new GeoPoint(22.733, 68.859)); + allPoints.add(new GeoPoint(24.733, 68.859)); + allPoints.add(new GeoPoint(23, 70.859)); + QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + + 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(greatRannOfKutchid)); + } + + @After + public void destroy() throws Exception { + DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD); + client.indices() + .delete(deleteIndex, RequestOptions.DEFAULT); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 87eab00da0..9f713a276f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -31,11 +31,10 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -77,7 +76,7 @@ public class ElasticSearchManualTest { public void after() { articleRepository.deleteAll(); } - + @Test public void givenArticleService_whenSaveArticle_thenIdIsAssigned() { final List authors = asList(new Author("John Smith"), johnDoe); @@ -91,81 +90,79 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), - PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", - PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { - final Page
articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", - PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", - "elasticsearch", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) - .build(); + .build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); assertEquals(1, articles.getTotalHits()); } @Test public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { - final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")) + .build(); final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); assertEquals(1, articles.getTotalHits()); - final Article article = articles.getSearchHit(0).getContent(); + final Article article = articles.getSearchHit(0) + .getContent(); final String newTitle = "Getting started with Search Engines"; article.setTitle(newTitle); articleRepository.save(article); - assertEquals(newTitle, articleRepository.findById(article.getId()).get().getTitle()); + assertEquals(newTitle, articleRepository.findById(article.getId()) + .get() + .getTitle()); } @Test public void givenSavedDoc_whenDelete_thenRemovedFromIndex() { final String articleTitle = "Spring Data Elasticsearch"; - final Query searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); final long count = articleRepository.count(); - articleRepository.delete(articles.getSearchHit(0).getContent()); + articleRepository.delete(articles.getSearchHit(0) + .getContent()); assertEquals(count - 1, articleRepository.count()); } @Test public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { - final Query searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); assertEquals(1, articles.getTotalHits()); } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index b9de9d51af..3cedab00e4 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -48,199 +48,201 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class ElasticSearchQueryManualTest { - @Autowired - private ElasticsearchRestTemplate elasticsearchTemplate; + @Autowired + private ElasticsearchRestTemplate elasticsearchTemplate; - @Autowired - private ArticleRepository articleRepository; + @Autowired + private ArticleRepository articleRepository; - @Autowired - private RestHighLevelClient client; + @Autowired + private RestHighLevelClient client; - private final Author johnSmith = new Author("John Smith"); - private final Author johnDoe = new Author("John Doe"); + private final Author johnSmith = new Author("John Smith"); + private final Author johnDoe = new Author("John Doe"); - @Before - public void before() { - Article article = new Article("Spring Data Elasticsearch"); - article.setAuthors(asList(johnSmith, johnDoe)); - article.setTags("elasticsearch", "spring data"); - articleRepository.save(article); + @Before + public void before() { + Article article = new Article("Spring Data Elasticsearch"); + article.setAuthors(asList(johnSmith, johnDoe)); + article.setTags("elasticsearch", "spring data"); + articleRepository.save(article); - article = new Article("Search engines"); - article.setAuthors(asList(johnDoe)); - article.setTags("search engines", "tutorial"); - articleRepository.save(article); + article = new Article("Search engines"); + article.setAuthors(asList(johnDoe)); + article.setTags("search engines", "tutorial"); + articleRepository.save(article); - article = new Article("Second Article About Elasticsearch"); - article.setAuthors(asList(johnSmith)); - article.setTags("elasticsearch", "spring data"); - articleRepository.save(article); + article = new Article("Second Article About Elasticsearch"); + article.setAuthors(asList(johnSmith)); + article.setTags("elasticsearch", "spring data"); + articleRepository.save(article); - article = new Article("Elasticsearch Tutorial"); - article.setAuthors(asList(johnDoe)); - article.setTags("elasticsearch"); - articleRepository.save(article); - } + article = new Article("Elasticsearch Tutorial"); + article.setAuthors(asList(johnDoe)); + article.setTags("elasticsearch"); + articleRepository.save(article); + } - @After - public void after() { - articleRepository.deleteAll(); - } + @After + public void after() { + articleRepository.deleteAll(); + } - @Test - public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(Operator.AND)).build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(1, articles.getTotalHits()); - } + @Test + public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND)) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); + } - @Test - public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { - final NativeSearchQuery searchQuery = - new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) - .build(); - - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); - assertEquals("Search engines", articles.getSearchHit(0).getContent().getTitle()); - } + @Test + public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) + .build(); - @Test - public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { - final NativeSearchQuery searchQuery = - new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) - .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(3, articles.getTotalHits()); - } + assertEquals(1, articles.getTotalHits()); + assertEquals("Search engines", articles.getSearchHit(0) + .getContent() + .getTitle()); + } - @Test - public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")) - .build(); + @Test + public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) + .build(); - SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About")) - .build(); - - articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(0, articles.getTotalHits()); - } + assertEquals(3, articles.getTotalHits()); + } - @Test - public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { - final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), - ScoreMode.None); + @Test + public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")) + .build(); - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(2, articles.getTotalHits()); - } + assertEquals(1, articles.getTotalHits()); - @Test - public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); + searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) + .build(); - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest("blog").source(builder); + articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(0, articles.getTotalHits()); + } - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { + final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).sorted() - .collect(toList()); - assertEquals( - asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); - } + assertEquals(2, articles.getTotalHits()); + } - @Test - public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") - .order(BucketOrder.count(false)); + @Test + public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags") + .field("title"); - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest().indices("blog").source(builder); + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest("blog").source(builder); - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final Map results = response.getAggregations() + .asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString) - .collect(toList()); - assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); - } + final List keys = topTags.getBuckets() + .stream() + .map(MultiBucketsAggregation.Bucket::getKeyAsString) + .sorted() + .collect(toList()); + assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); + } - @Test - public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); + @Test + public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags") + .field("tags") + .order(BucketOrder.count(false)); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); - } + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest().indices("blog") + .source(builder); - @Test - public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery( - matchQuery("title", "spring date elasticserch").operator(Operator.AND).fuzziness(Fuzziness.ONE).prefixLength(3)) - .build(); + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); - } + final Map results = response.getAggregations() + .asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - @Test - public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery( - multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) - .build(); + final List keys = topTags.getBuckets() + .stream() + .map(MultiBucketsAggregation.Bucket::getKeyAsString) + .collect(toList()); + assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); + } - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + @Test + public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)) + .build(); - assertEquals(2, articles.getTotalHits()); - } + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - @Test - public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { - final QueryBuilder builder = boolQuery() - .must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) - .filter(termQuery("tags", "elasticsearch")); + assertEquals(1, articles.getTotalHits()); + } - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + @Test + public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(Operator.AND) + .fuzziness(Fuzziness.ONE) + .prefixLength(3)) + .build(); - assertEquals(2, articles.getTotalHits()); - } + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); + } + + @Test + public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title") + .field("tags") + .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) + .build(); + + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(2, articles.getTotalHits()); + } + + @Test + public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { + final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) + .filter(termQuery("tags", "elasticsearch")); + + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(2, articles.getTotalHits()); + } } From 3fc4ed23a46468dd0c9d257c4f4356f0a1347b49 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:44:42 -0400 Subject: [PATCH 09/12] upgrade to 4.0.0.RELEASE --- .../spring-data-elasticsearch/pom.xml | 23 +++---------------- .../com/baeldung/SpringContextManualTest.java | 4 ++-- .../ElasticSearchManualTest.java | 4 ++-- .../elasticsearch/GeoQueriesManualTest.java | 4 ++-- .../data/es/ElasticSearchManualTest.java | 4 ++-- .../data/es/ElasticSearchQueryManualTest.java | 4 ++-- 6 files changed, 13 insertions(+), 30 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 72860cb136..fc435b1f48 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -52,7 +52,7 @@ org.locationtech.jts jts-core - 1.15.0 + ${jts.version} xerces @@ -67,29 +67,12 @@ ${spring.version} test - - - net.java.dev.jna - jna - ${jna.version} - test - - 4.0.0.RC1 - 4.5.2 + 4.0.0.RELEASE 1.2.47 0.7 - 1.13 + 1.15.0 - - - - repository.spring.milestone - Spring Milestone Repository - https://repo.spring.io/milestone - - - \ No newline at end of file diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index bff910cf93..6572896eca 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -10,8 +10,8 @@ import com.baeldung.spring.data.es.config.Config; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index e4b3f6d801..2ca5f28f13 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -39,8 +39,8 @@ import org.springframework.data.elasticsearch.client.RestClients; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ public class ElasticSearchManualTest { private List listOfPersons = new ArrayList<>(); 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 a36e08d6d2..64b2ea2437 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 @@ -41,8 +41,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 9f713a276f..412cd04e09 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -33,8 +33,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 3cedab00e4..aaf0c80097 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -50,8 +50,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) From 458699cb5bc2e83be818a62bf86e784512832ede Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:48:11 -0400 Subject: [PATCH 10/12] added back elasticsearch dependency --- persistence-modules/spring-data-elasticsearch/pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index fc435b1f48..cf77f858d0 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -37,6 +37,12 @@ ${spring-data-elasticsearch.version} + + org.elasticsearch + elasticsearch + ${elasticsearch.version} + + com.alibaba fastjson @@ -71,6 +77,7 @@ 4.0.0.RELEASE + 7.6.2 1.2.47 0.7 1.15.0 From cffeff11abde9ba1ff2d912bde9cf60d33444bf6 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:53:23 -0400 Subject: [PATCH 11/12] removed unecessary dependencies --- persistence-modules/spring-data-elasticsearch/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index cf77f858d0..79363d7352 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -13,7 +13,7 @@ - + org.springframework From 177bb9c8977909e585c4dbcc92522709073e95d3 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:54:29 -0400 Subject: [PATCH 12/12] removed unecessary dependencies --- .../spring-data-elasticsearch/pom.xml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 79363d7352..6a983145ee 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -13,18 +13,6 @@ - - org.springframework spring-web