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:
parent
618f549b0e
commit
845ed7690e
|
@ -6,17 +6,17 @@ 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.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
|
||||
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
|
||||
|
||||
@Configuration
|
||||
@EnableElasticsearchRepositories(basePackages = "com.baeldung.spring.data.es.repository")
|
||||
@ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" })
|
||||
public class Config {
|
||||
public class Config extends AbstractElasticsearchConfiguration {
|
||||
|
||||
@Bean
|
||||
RestHighLevelClient client() {
|
||||
@Override
|
||||
public RestHighLevelClient elasticsearchClient() {
|
||||
ClientConfiguration clientConfiguration = ClientConfiguration.builder()
|
||||
.connectedTo("localhost:9200")
|
||||
.build();
|
||||
|
@ -24,9 +24,4 @@ public class Config {
|
|||
return RestClients.create(clientConfiguration)
|
||||
.rest();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ElasticsearchOperations elasticsearchTemplate() {
|
||||
return new ElasticsearchRestTemplate(client());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,7 +22,7 @@ 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.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
||||
|
@ -41,7 +41,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
public class ElasticSearchManualTest {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchRestTemplate elasticsearchTemplate;
|
||||
private ElasticsearchOperations elasticsearchOperations;
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
@ -117,7 +117,7 @@ public class ElasticSearchManualTest {
|
|||
final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
|
||||
.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());
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ public class ElasticSearchManualTest {
|
|||
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
|
||||
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch"))
|
||||
.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());
|
||||
|
||||
|
@ -147,7 +147,7 @@ public class ElasticSearchManualTest {
|
|||
|
||||
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%"))
|
||||
.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());
|
||||
final long count = articleRepository.count();
|
||||
|
@ -162,7 +162,7 @@ public class ElasticSearchManualTest {
|
|||
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
||||
final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND))
|
||||
.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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -39,7 +39,7 @@ 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.ElasticsearchRestTemplate;
|
||||
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
|
||||
import org.springframework.data.elasticsearch.core.SearchHits;
|
||||
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
||||
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
|
||||
|
@ -58,7 +58,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
|||
public class ElasticSearchQueryManualTest {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchRestTemplate elasticsearchTemplate;
|
||||
private ElasticsearchOperations elasticsearchOperations;
|
||||
|
||||
@Autowired
|
||||
private ArticleRepository articleRepository;
|
||||
|
@ -101,7 +101,7 @@ public class ElasticSearchQueryManualTest {
|
|||
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
|
||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND))
|
||||
.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());
|
||||
}
|
||||
|
||||
|
@ -110,7 +110,7 @@ public class ElasticSearchQueryManualTest {
|
|||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
|
||||
.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("Search engines", articles.getSearchHit(0)
|
||||
|
@ -123,7 +123,7 @@ public class ElasticSearchQueryManualTest {
|
|||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
|
||||
.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());
|
||||
}
|
||||
|
@ -133,14 +133,14 @@ public class ElasticSearchQueryManualTest {
|
|||
NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch"))
|
||||
.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());
|
||||
|
||||
searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About"))
|
||||
.build();
|
||||
|
||||
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||
articles = elasticsearchOperations.search(searchQuery, Article.class, IndexCoordinates.of("blog"));
|
||||
assertEquals(0, articles.getTotalHits());
|
||||
}
|
||||
|
||||
|
@ -150,7 +150,7 @@ public class ElasticSearchQueryManualTest {
|
|||
|
||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
|
||||
.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());
|
||||
}
|
||||
|
@ -205,7 +205,7 @@ public class ElasticSearchQueryManualTest {
|
|||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1))
|
||||
.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());
|
||||
}
|
||||
|
@ -217,7 +217,7 @@ public class ElasticSearchQueryManualTest {
|
|||
.prefixLength(3))
|
||||
.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());
|
||||
}
|
||||
|
@ -229,7 +229,7 @@ public class ElasticSearchQueryManualTest {
|
|||
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
|
||||
.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());
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ public class ElasticSearchQueryManualTest {
|
|||
|
||||
final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder)
|
||||
.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());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue