From 1ed9c7db328be10c999c321f3dbca82f29b011dc Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 12 May 2020 21:41:51 -0400 Subject: [PATCH] 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()