fixed mapping and tests

This commit is contained in:
Mathieu Fortin
2020-05-12 21:41:51 -04:00
parent 35c4288ba3
commit ec6956371d
6 changed files with 32 additions and 14 deletions

View File

@@ -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() {

View File

@@ -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
*/

View File

@@ -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
*/

View File

@@ -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
*/

View File

@@ -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<Author> authors = asList(new Author("John Smith"), johnDoe);

View File

@@ -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()