JAVA-16308 Potential issue in Introduction to Spring Data Elasticsearch article (#13286)

* JAVA-16308 Potential issue in Introduction to Spring Data Elasticsearch article

* JAVA-16308 Update code as per Review Comments
This commit is contained in:
anuragkumawat 2023-01-19 21:30:18 +05:30 committed by GitHub
parent 618f549b0e
commit 845ed7690e
3 changed files with 22 additions and 27 deletions

View File

@ -6,17 +6,17 @@ import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations; import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
@Configuration @Configuration
@EnableElasticsearchRepositories(basePackages = "com.baeldung.spring.data.es.repository") @EnableElasticsearchRepositories(basePackages = "com.baeldung.spring.data.es.repository")
@ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" }) @ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" })
public class Config { public class Config extends AbstractElasticsearchConfiguration {
@Bean @Bean
RestHighLevelClient client() { @Override
public RestHighLevelClient elasticsearchClient() {
ClientConfiguration clientConfiguration = ClientConfiguration.builder() ClientConfiguration clientConfiguration = ClientConfiguration.builder()
.connectedTo("localhost:9200") .connectedTo("localhost:9200")
.build(); .build();
@ -24,9 +24,4 @@ public class Config {
return RestClients.create(clientConfiguration) return RestClients.create(clientConfiguration)
.rest(); .rest();
} }
@Bean
public ElasticsearchOperations elasticsearchTemplate() {
return new ElasticsearchRestTemplate(client());
}
} }

View File

@ -22,7 +22,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
@ -41,7 +41,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class ElasticSearchManualTest { public class ElasticSearchManualTest {
@Autowired @Autowired
private ElasticsearchRestTemplate elasticsearchTemplate; private ElasticsearchOperations elasticsearchOperations;
@Autowired @Autowired
private ArticleRepository articleRepository; private ArticleRepository articleRepository;
@ -117,7 +117,7 @@ public class ElasticSearchManualTest {
final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
} }
@ -126,7 +126,7 @@ public class ElasticSearchManualTest {
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")) final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch"))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
@ -147,7 +147,7 @@ public class ElasticSearchManualTest {
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")) final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%"))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
final long count = articleRepository.count(); final long count = articleRepository.count();
@ -162,7 +162,7 @@ public class ElasticSearchManualTest {
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)) final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
} }
} }

View File

@ -39,7 +39,7 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.SearchHits; import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
@ -58,7 +58,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
public class ElasticSearchQueryManualTest { public class ElasticSearchQueryManualTest {
@Autowired @Autowired
private ElasticsearchRestTemplate elasticsearchTemplate; private ElasticsearchOperations elasticsearchOperations;
@Autowired @Autowired
private ArticleRepository articleRepository; private ArticleRepository articleRepository;
@ -101,7 +101,7 @@ public class ElasticSearchQueryManualTest {
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND)) final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
} }
@ -110,7 +110,7 @@ public class ElasticSearchQueryManualTest {
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
assertEquals("Search engines", articles.getSearchHit(0) assertEquals("Search engines", articles.getSearchHit(0)
@ -123,7 +123,7 @@ public class ElasticSearchQueryManualTest {
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(3, articles.getTotalHits()); assertEquals(3, articles.getTotalHits());
} }
@ -133,14 +133,14 @@ public class ElasticSearchQueryManualTest {
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")) NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch"))
.build(); .build();
SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
.build(); .build();
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(0, articles.getTotalHits()); assertEquals(0, articles.getTotalHits());
} }
@ -150,7 +150,7 @@ public class ElasticSearchQueryManualTest {
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(2, articles.getTotalHits()); assertEquals(2, articles.getTotalHits());
} }
@ -205,7 +205,7 @@ public class ElasticSearchQueryManualTest {
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)) final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
} }
@ -217,7 +217,7 @@ public class ElasticSearchQueryManualTest {
.prefixLength(3)) .prefixLength(3))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(1, articles.getTotalHits()); assertEquals(1, articles.getTotalHits());
} }
@ -229,7 +229,7 @@ public class ElasticSearchQueryManualTest {
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) .type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(2, articles.getTotalHits()); assertEquals(2, articles.getTotalHits());
} }
@ -241,7 +241,7 @@ public class ElasticSearchQueryManualTest {
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
.build(); .build();
final SearchHits<Article> articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); final SearchHits<Article> articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
assertEquals(2, articles.getTotalHits()); assertEquals(2, articles.getTotalHits());
} }