From 574c9f6b499101274f3e59867417f99ee47a5467 Mon Sep 17 00:00:00 2001 From: David Morley Date: Tue, 15 Mar 2016 05:35:11 -0500 Subject: [PATCH] Clean up Elasticsearch examples --- spring-data-elasticsearch/.classpath | 31 ------------------- spring-data-elasticsearch/.project | 29 ----------------- .../spring/data/es/config/Config.java | 2 +- .../spring/data/es/dao/ArticleRepository.java | 15 --------- .../data/es/repository/ArticleRepository.java | 2 ++ .../spring/data/es/ElasticSearchTest.java | 11 +++++++ 6 files changed, 14 insertions(+), 76 deletions(-) delete mode 100644 spring-data-elasticsearch/.classpath delete mode 100644 spring-data-elasticsearch/.project delete mode 100644 spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/dao/ArticleRepository.java diff --git a/spring-data-elasticsearch/.classpath b/spring-data-elasticsearch/.classpath deleted file mode 100644 index 698778fef3..0000000000 --- a/spring-data-elasticsearch/.classpath +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-data-elasticsearch/.project b/spring-data-elasticsearch/.project deleted file mode 100644 index 09b9a781ed..0000000000 --- a/spring-data-elasticsearch/.project +++ /dev/null @@ -1,29 +0,0 @@ - - - spring-data-elasticsearch - - - - - - org.eclipse.jdt.core.javabuilder - - - - - org.eclipse.m2e.core.maven2Builder - - - - - org.springframework.ide.eclipse.core.springbuilder - - - - - - org.springframework.ide.eclipse.core.springnature - org.eclipse.jdt.core.javanature - org.eclipse.m2e.core.maven2Nature - - diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java index 78e4083a28..88b9323066 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java @@ -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 { diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/dao/ArticleRepository.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/dao/ArticleRepository.java deleted file mode 100644 index 313eba5b36..0000000000 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/dao/ArticleRepository.java +++ /dev/null @@ -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 { - - Page
findByAuthorsName(String name, Pageable pageable); - - @Query("{\"bool\": {\"must\": [{\"match\": {\"authors.name\": \"?0\"}}]}}") - Page
findByAuthorsNameUsingCustomQuery(String name, Pageable pageable); -} diff --git a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java index 27628950d7..8aef865401 100644 --- a/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java +++ b/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/repository/ArticleRepository.java @@ -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 { Page
findByAuthorsName(String name, Pageable pageable); diff --git a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchTest.java b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchTest.java index 34ccfd788e..7b48772d3f 100644 --- a/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchTest.java +++ b/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchTest.java @@ -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
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } }