fixed format

This commit is contained in:
Mathieu Fortin 2020-04-13 13:47:17 -04:00
parent 2c5c556fed
commit 58e44705b2
2 changed files with 136 additions and 142 deletions

View File

@ -79,7 +79,6 @@
<properties> <properties>
<spring-data-elasticsearch.version>3.2.6.RELEASE</spring-data-elasticsearch.version> <spring-data-elasticsearch.version>3.2.6.RELEASE</spring-data-elasticsearch.version>
<jna.version>4.5.2</jna.version> <jna.version>4.5.2</jna.version>
<elasticsearch.version>6.8.8</elasticsearch.version>
<fastjson.version>1.2.47</fastjson.version> <fastjson.version>1.2.47</fastjson.version>
<spatial4j.version>0.6</spatial4j.version> <spatial4j.version>0.6</spatial4j.version>
<jts.version>1.13</jts.version> <jts.version>1.13</jts.version>

View File

@ -46,9 +46,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/** /**
* *
* This Manual test requires: * This Manual test requires: * Elasticsearch instance running on host * with
* * Elasticsearch instance running on host * cluster name = elasticsearch
* * with cluster name = elasticsearch
* *
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ -105,8 +104,8 @@ public class ElasticSearchQueryManualTest {
@Test @Test
public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions"))
.withQuery(matchQuery("title", "Engines Solutions")).build(); .build();
final 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());
@ -114,8 +113,8 @@ public class ElasticSearchQueryManualTest {
@Test @Test
public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data"))
.withQuery(matchQuery("title", "elasticsearch data")).build(); .build();
final 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());
} }
@ -135,7 +134,8 @@ public class ElasticSearchQueryManualTest {
@Test @Test
public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() {
final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")),
ScoreMode.None);
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
@ -148,20 +148,17 @@ public class ElasticSearchQueryManualTest {
final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title");
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
final SearchRequest searchRequest = new SearchRequest("blog") final SearchRequest searchRequest = new SearchRequest("blog").types("article").source(builder);
.types("article")
.source(builder);
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
final Map<String, Aggregation> results = response.getAggregations().asMap(); final Map<String, Aggregation> results = response.getAggregations().asMap();
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
final List<String> keys = topTags.getBuckets().stream() final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).sorted()
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
.sorted()
.collect(toList()); .collect(toList());
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
@ -170,18 +167,14 @@ public class ElasticSearchQueryManualTest {
.order(BucketOrder.count(false)); .order(BucketOrder.count(false));
final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation);
final SearchRequest searchRequest = new SearchRequest() final SearchRequest searchRequest = new SearchRequest().indices("blog").types("article").source(builder);
.indices("blog")
.types("article")
.source(builder);
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
final Map<String, Aggregation> results = response.getAggregations().asMap(); final Map<String, Aggregation> results = response.getAggregations().asMap();
final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags");
final List<String> keys = topTags.getBuckets().stream() final List<String> keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString)
.map(MultiBucketsAggregation.Bucket::getKeyAsString)
.collect(toList()); .collect(toList());
assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys);
} }
@ -197,8 +190,9 @@ public class ElasticSearchQueryManualTest {
@Test @Test
public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE) .withQuery(
.prefixLength(3)).build(); matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3))
.build();
final 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());
@ -207,8 +201,9 @@ public class ElasticSearchQueryManualTest {
@Test @Test
public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() {
final SearchQuery searchQuery = new NativeSearchQueryBuilder() final SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withQuery(multiMatchQuery("tutorial").field("title").field("tags") .withQuery(
.type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS))
.build();
final 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());
@ -216,11 +211,11 @@ public class ElasticSearchQueryManualTest {
@Test @Test
public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() {
final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) final QueryBuilder builder = boolQuery()
.must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None))
.filter(termQuery("tags", "elasticsearch")); .filter(termQuery("tags", "elasticsearch"));
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build();
.build();
final 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());