upgrade to elastic search 2

This commit is contained in:
DOHA 2016-05-28 13:05:56 +02:00
parent b67ecea631
commit 65e423c0d7
5 changed files with 96 additions and 84 deletions

View File

@ -18,7 +18,7 @@
<junit.version>4.11</junit.version> <junit.version>4.11</junit.version>
<org.slf4j.version>1.7.12</org.slf4j.version> <org.slf4j.version>1.7.12</org.slf4j.version>
<logback.version>1.1.3</logback.version> <logback.version>1.1.3</logback.version>
<elasticsearch.version>1.3.2.RELEASE</elasticsearch.version> <elasticsearch.version>2.0.0.RELEASE</elasticsearch.version>
</properties> </properties>
<dependencies> <dependencies>
@ -44,6 +44,11 @@
<artifactId>spring-data-elasticsearch</artifactId> <artifactId>spring-data-elasticsearch</artifactId>
<version>${elasticsearch.version}</version> <version>${elasticsearch.version}</version>
</dependency> </dependency>
<dependency> <groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.slf4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId> <artifactId>slf4j-api</artifactId>

View File

@ -1,7 +1,12 @@
package com.baeldung.spring.data.es.config; package com.baeldung.spring.data.es.config;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.node.NodeBuilder; import org.elasticsearch.node.NodeBuilder;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -12,11 +17,6 @@ import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@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" })
@ -27,14 +27,14 @@ public class Config {
@Bean @Bean
public Client client() { public Client client() {
try { try {
Path tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "elasticsearch_data"); final Path tmpDir = Files.createTempDirectory(Paths.get(System.getProperty("java.io.tmpdir")), "elasticsearch_data");
ImmutableSettings.Builder elasticsearchSettings = ImmutableSettings.settingsBuilder().put("http.enabled", "false").put("path.data", tmpDir.toAbsolutePath().toString()); final Settings.Builder elasticsearchSettings = Settings.settingsBuilder().put("http.enabled", "false").put("path.data", tmpDir.toAbsolutePath().toString()).put("path.home", "/usr/local/Cellar/elasticsearch/2.3.2");
logger.debug(tmpDir.toAbsolutePath().toString()); logger.debug(tmpDir.toAbsolutePath().toString());
return new NodeBuilder().local(true).settings(elasticsearchSettings.build()).node().client(); return new NodeBuilder().local(true).settings(elasticsearchSettings.build()).node().client();
} catch (IOException ioex) { } catch (final IOException ioex) {
logger.error("Cannot create temp dir", ioex); logger.error("Cannot create temp dir", ioex);
throw new RuntimeException(); throw new RuntimeException();
} }

View File

@ -1,22 +1,25 @@
package com.baeldung.spring.data.es.model; package com.baeldung.spring.data.es.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;
import java.util.Arrays;
import java.util.List;
import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed; import static org.springframework.data.elasticsearch.annotations.FieldIndex.not_analyzed;
import static org.springframework.data.elasticsearch.annotations.FieldType.Nested; import static org.springframework.data.elasticsearch.annotations.FieldType.Nested;
import static org.springframework.data.elasticsearch.annotations.FieldType.String; import static org.springframework.data.elasticsearch.annotations.FieldType.String;
import java.util.Arrays;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.InnerField;
import org.springframework.data.elasticsearch.annotations.MultiField;
@Document(indexName = "blog", type = "article") @Document(indexName = "blog", type = "article")
public class Article { public class Article {
@Id @Id
private String id; private String id;
@MultiField(mainField = @Field(type = String), otherFields = { @NestedField(index = not_analyzed, dotSuffix = "verbatim", type = String) }) @MultiField(mainField = @Field(type = String), otherFields = { @InnerField(index = not_analyzed, suffix = "verbatim", type = String) })
private String title; private String title;
@Field(type = Nested) @Field(type = Nested)

View File

@ -1,12 +1,20 @@
package com.baeldung.spring.data.es; package com.baeldung.spring.data.es;
import com.baeldung.spring.data.es.config.Config; import static java.util.Arrays.asList;
import com.baeldung.spring.data.es.model.Article; import static java.util.stream.Collectors.toList;
import com.baeldung.spring.data.es.model.Author; import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
import com.baeldung.spring.data.es.service.ArticleService; import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
import org.elasticsearch.action.ActionFuture; import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery;
import org.elasticsearch.action.index.IndexRequest; import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import org.elasticsearch.action.index.IndexResponse; import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery;
import static org.elasticsearch.index.query.QueryBuilders.nestedQuery;
import static org.elasticsearch.index.query.QueryBuilders.termQuery;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client; import org.elasticsearch.client.Client;
import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.unit.Fuzziness;
@ -28,16 +36,10 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.util.Collections; import com.baeldung.spring.data.es.config.Config;
import java.util.List; import com.baeldung.spring.data.es.model.Article;
import java.util.Map; import com.baeldung.spring.data.es.model.Author;
import java.util.concurrent.TimeUnit; import com.baeldung.spring.data.es.service.ArticleService;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.toList;
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.junit.Assert.assertEquals;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class)
@ -60,7 +62,7 @@ public class ElasticSearchQueryTest {
elasticsearchTemplate.deleteIndex(Article.class); elasticsearchTemplate.deleteIndex(Article.class);
elasticsearchTemplate.createIndex(Article.class); elasticsearchTemplate.createIndex(Article.class);
elasticsearchTemplate.putMapping(Article.class); elasticsearchTemplate.putMapping(Article.class);
elasticsearchTemplate.refresh(Article.class, true); elasticsearchTemplate.refresh(Article.class);
Article article = new Article("Spring Data Elasticsearch"); Article article = new Article("Spring Data Elasticsearch");
article.setAuthors(asList(johnSmith, johnDoe)); article.setAuthors(asList(johnSmith, johnDoe));
@ -85,23 +87,23 @@ public class ElasticSearchQueryTest {
@Test @Test
public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
} }
@Test @Test
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
assertEquals("Search engines", articles.get(0).getTitle()); assertEquals("Search engines", articles.get(0).getTitle());
} }
@Test @Test
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(3, articles.size()); assertEquals(3, articles.size());
} }
@ -118,59 +120,59 @@ public class ElasticSearchQueryTest {
@Test @Test
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith"))); final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")));
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(2, articles.size()); assertEquals(2, articles.size());
} }
@Test @Test
public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() { public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() {
TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet(); final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
Map<String, Aggregation> results = response.getAggregations().asMap(); final Map<String, Aggregation> results = response.getAggregations().asMap();
StringTerms topTags = (StringTerms) results.get("top_tags"); final StringTerms topTags = (StringTerms) results.get("top_tags");
List<String> keys = topTags.getBuckets().stream().map(b -> b.getKey()).collect(toList()); final List<String> keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList());
Collections.sort(keys); Collections.sort(keys);
assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys);
} }
@Test @Test
public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() {
TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false)); final TermsBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags").order(Terms.Order.aggregation("_count", false));
SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet(); final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation).execute().actionGet();
Map<String, Aggregation> results = response.getAggregations().asMap(); final Map<String, Aggregation> results = response.getAggregations().asMap();
StringTerms topTags = (StringTerms) results.get("top_tags"); final StringTerms topTags = (StringTerms) results.get("top_tags");
List<String> keys = topTags.getBuckets().stream().map(b -> b.getKey()).collect(toList()); final List<String> keys = topTags.getBuckets().stream().map(b -> b.getKeyAsString()).collect(toList());
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
} }
@Test @Test
public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
} }
@Test @Test
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
} }
@Test @Test
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(2, articles.size()); assertEquals(2, articles.size());
} }
} }

View File

@ -1,9 +1,15 @@
package com.baeldung.spring.data.es; package com.baeldung.spring.data.es;
import com.baeldung.spring.data.es.config.Config; import static java.util.Arrays.asList;
import com.baeldung.spring.data.es.model.Article; import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
import com.baeldung.spring.data.es.model.Author; import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
import com.baeldung.spring.data.es.service.ArticleService; import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.List;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -17,14 +23,10 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader; import org.springframework.test.context.support.AnnotationConfigContextLoader;
import java.util.List; import com.baeldung.spring.data.es.config.Config;
import com.baeldung.spring.data.es.model.Article;
import static java.util.Arrays.asList; import com.baeldung.spring.data.es.model.Author;
import static org.elasticsearch.index.query.FilterBuilders.regexpFilter; import com.baeldung.spring.data.es.service.ArticleService;
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;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class) @ContextConfiguration(classes = { Config.class }, loader = AnnotationConfigContextLoader.class)
@ -59,7 +61,7 @@ public class ElasticSearchTest {
@Test @Test
public void givenArticleService_whenSaveArticle_thenIdIsAssigned() { public void givenArticleService_whenSaveArticle_thenIdIsAssigned() {
List<Author> authors = asList(new Author("John Smith"), johnDoe); final List<Author> authors = asList(new Author("John Smith"), johnDoe);
Article article = new Article("Making Search Elastic"); Article article = new Article("Making Search Elastic");
article.setAuthors(authors); article.setAuthors(authors);
@ -71,34 +73,34 @@ public class ElasticSearchTest {
@Test @Test
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
Page<Article> articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10)); final Page<Article> articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10));
assertEquals(2L, articleByAuthorName.getTotalElements()); assertEquals(2L, articleByAuthorName.getTotalElements());
} }
@Test @Test
public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() {
Page<Article> articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10)); final Page<Article> articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10));
assertEquals(3L, articleByAuthorName.getTotalElements()); assertEquals(3L, articleByAuthorName.getTotalElements());
} }
@Test @Test
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpFilter("title", ".*data.*")).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
} }
@Test @Test
public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
Article article = articles.get(0); final Article article = articles.get(0);
final String newTitle = "Getting started with Search Engines"; final String newTitle = "Getting started with Search Engines";
article.setTitle(newTitle); article.setTitle(newTitle);
articleService.save(article); articleService.save(article);
@ -111,8 +113,8 @@ public class ElasticSearchTest {
final String articleTitle = "Spring Data Elasticsearch"; final String articleTitle = "Spring Data Elasticsearch";
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
final long count = articleService.count(); final long count = articleService.count();
@ -123,8 +125,8 @@ public class ElasticSearchTest {
@Test @Test
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build();
List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
assertEquals(1, articles.size()); assertEquals(1, articles.size());
} }
} }