Clean up Elasticsearch examples
This commit is contained in:
@@ -17,7 +17,7 @@ import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
@Configuration
|
||||
@EnableElasticsearchRepositories(basePackages = "com.baeldung.repository")
|
||||
@EnableElasticsearchRepositories(basePackages = "com.baeldung.spring.data.es.repository")
|
||||
@ComponentScan(basePackages = {"com.baeldung.spring.data.es.service"})
|
||||
public class Config {
|
||||
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
package com.baeldung.spring.data.es.dao;
|
||||
|
||||
import com.baeldung.spring.data.es.model.Article;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.annotations.Query;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
|
||||
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
|
||||
|
||||
Page<Article> findByAuthorsName(String name, Pageable pageable);
|
||||
|
||||
@Query("{\"bool\": {\"must\": [{\"match\": {\"authors.name\": \"?0\"}}]}}")
|
||||
Page<Article> findByAuthorsNameUsingCustomQuery(String name, Pageable pageable);
|
||||
}
|
||||
@@ -5,7 +5,9 @@ import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.elasticsearch.annotations.Query;
|
||||
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface ArticleRepository extends ElasticsearchRepository<Article, String> {
|
||||
|
||||
Page<Article> findByAuthorsName(String name, Pageable pageable);
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static org.elasticsearch.index.query.FilterBuilders.regexpFilter;
|
||||
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
|
||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
@@ -31,6 +32,7 @@ public class ElasticSearchTest {
|
||||
|
||||
@Autowired
|
||||
private ElasticsearchTemplate elasticsearchTemplate;
|
||||
|
||||
@Autowired
|
||||
private ArticleService articleService;
|
||||
|
||||
@@ -126,4 +128,13 @@ public class ElasticSearchTest {
|
||||
|
||||
assertEquals(count - 1, articleService.count());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
|
||||
SearchQuery searchQuery = new NativeSearchQueryBuilder()
|
||||
.withQuery(matchQuery("title", "Search engines").operator(AND))
|
||||
.build();
|
||||
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
|
||||
assertEquals(1, articles.size());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user