Skip to content

Commit d64468e

Browse files
authored
Refactor elastic (eugenp#3467)
1 parent b1520c9 commit d64468e

5 files changed

Lines changed: 153 additions & 144 deletions

File tree

‎spring-data-elasticsearch/src/main/java/com/baeldung/elasticsearch/Person.java‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ public class Person {
1111
private Date dateOfBirth;
1212

1313
public Person() {
14-
1514
}
1615

17-
public Person(int age, String fullName, Date dateOfBirth) {
16+
Person(int age, String fullName, Date dateOfBirth) {
1817
super();
1918
this.age = age;
2019
this.fullName = fullName;

‎spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Arrays;
2020
import java.util.Date;
2121
import java.util.List;
22+
import java.util.stream.Collectors;
2223

2324
import static org.elasticsearch.node.NodeBuilder.nodeBuilder;
2425
import static org.junit.Assert.assertEquals;
@@ -78,12 +79,9 @@ public void givenSearchRequest_whenMatchAll_thenReturnAllResults() {
7879
SearchHit[] searchHits = response
7980
.getHits()
8081
.getHits();
81-
List<Person> results = new ArrayList<>();
82-
for (SearchHit hit : searchHits) {
83-
String sourceAsString = hit.getSourceAsString();
84-
Person person = JSON.parseObject(sourceAsString, Person.class);
85-
results.add(person);
86-
}
82+
List<Person> results = Arrays.stream(searchHits)
83+
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
84+
.collect(Collectors.toList());
8785
}
8886

8987
@Test
@@ -125,11 +123,10 @@ public void givenSearchParameters_thenReturnResults() {
125123
.actionGet();
126124
response2.getHits();
127125
response3.getHits();
128-
List<SearchHit> searchHits = Arrays.asList(response
129-
.getHits()
130-
.getHits());
131-
final List<Person> results = new ArrayList<>();
132-
searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class)));
126+
127+
final List<Person> results = Arrays.stream(response.getHits().getHits())
128+
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class))
129+
.collect(Collectors.toList());
133130
}
134131

135132
@Test
Lines changed: 71 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
package com.baeldung.elasticsearch;
22

3-
import static org.junit.Assert.assertTrue;
4-
5-
import java.util.Arrays;
6-
import java.util.List;
7-
import java.util.stream.Collectors;
8-
3+
import com.baeldung.spring.data.es.config.Config;
94
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
105
import org.elasticsearch.action.index.IndexResponse;
116
import org.elasticsearch.action.search.SearchResponse;
@@ -15,6 +10,7 @@
1510
import org.elasticsearch.common.unit.DistanceUnit;
1611
import org.elasticsearch.index.query.QueryBuilder;
1712
import org.elasticsearch.index.query.QueryBuilders;
13+
import org.elasticsearch.search.SearchHit;
1814
import org.junit.After;
1915
import org.junit.Before;
2016
import org.junit.Test;
@@ -24,14 +20,19 @@
2420
import org.springframework.test.context.ContextConfiguration;
2521
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2622

27-
import com.baeldung.spring.data.es.config.Config;
23+
import java.util.Arrays;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
27+
import static org.junit.Assert.assertTrue;
2828

2929
@RunWith(SpringJUnit4ClassRunner.class)
3030
@ContextConfiguration(classes = Config.class)
3131
public class GeoQueriesTest {
3232

33-
public static final String WONDERS_OF_WORLD = "wonders-of-world";
34-
public static final String WONDERS = "Wonders";
33+
private static final String WONDERS_OF_WORLD = "wonders-of-world";
34+
private static final String WONDERS = "Wonders";
35+
3536
@Autowired
3637
private ElasticsearchTemplate elasticsearchTemplate;
3738

@@ -44,136 +45,127 @@ public void setUp() {
4445
CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
4546
req.mapping(WONDERS, jsonObject);
4647
client.admin()
47-
.indices()
48-
.create(req)
49-
.actionGet();
48+
.indices()
49+
.create(req)
50+
.actionGet();
5051
}
5152

5253
@Test
5354
public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() {
5455
String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,25],[80.1,30.2]]}}";
5556
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
56-
.setSource(jsonObject)
57-
.get();
57+
.setSource(jsonObject)
58+
.get();
5859
String tajMahalId = response.getId();
5960
client.admin()
60-
.indices()
61-
.prepareRefresh(WONDERS_OF_WORLD)
62-
.get();
61+
.indices()
62+
.prepareRefresh(WONDERS_OF_WORLD)
63+
.get();
6364

6465
QueryBuilder qb = QueryBuilders.geoShapeQuery("region", ShapeBuilder.newEnvelope()
65-
.topLeft(74.00, 24.0)
66-
.bottomRight(81.1, 31.2))
67-
.relation(ShapeRelation.WITHIN);
66+
.topLeft(74.00, 24.0)
67+
.bottomRight(81.1, 31.2))
68+
.relation(ShapeRelation.WITHIN);
6869

6970
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
70-
.setTypes(WONDERS)
71-
.setQuery(qb)
72-
.execute()
73-
.actionGet();
71+
.setTypes(WONDERS)
72+
.setQuery(qb)
73+
.execute()
74+
.actionGet();
7475
List<String> ids = Arrays.stream(searchResponse.getHits()
75-
.getHits())
76-
.map(hit -> {
77-
return hit.getId();
78-
})
79-
.collect(Collectors.toList());
76+
.getHits())
77+
.map(SearchHit::getId)
78+
.collect(Collectors.toList());
8079
assertTrue(ids.contains(tajMahalId));
8180
}
8281

8382
@Test
8483
public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() {
8584
String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}";
8685
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
87-
.setSource(jsonObject)
88-
.get();
86+
.setSource(jsonObject)
87+
.get();
8988
String pyramidsOfGizaId = response.getId();
9089
client.admin()
91-
.indices()
92-
.prepareRefresh(WONDERS_OF_WORLD)
93-
.get();
90+
.indices()
91+
.prepareRefresh(WONDERS_OF_WORLD)
92+
.get();
9493

9594
QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location")
96-
.bottomLeft(28, 30)
97-
.topRight(31, 32);
95+
.bottomLeft(28, 30)
96+
.topRight(31, 32);
9897

9998
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
100-
.setTypes(WONDERS)
101-
.setQuery(qb)
102-
.execute()
103-
.actionGet();
99+
.setTypes(WONDERS)
100+
.setQuery(qb)
101+
.execute()
102+
.actionGet();
104103
List<String> ids = Arrays.stream(searchResponse.getHits()
105-
.getHits())
106-
.map(hit -> {
107-
return hit.getId();
108-
})
109-
.collect(Collectors.toList());
104+
.getHits())
105+
.map(SearchHit::getId)
106+
.collect(Collectors.toList());
110107
assertTrue(ids.contains(pyramidsOfGizaId));
111108
}
112109

113110
@Test
114111
public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() {
115112
String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}";
116113
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
117-
.setSource(jsonObject)
118-
.get();
114+
.setSource(jsonObject)
115+
.get();
119116
String lighthouseOfAlexandriaId = response.getId();
120117
client.admin()
121-
.indices()
122-
.prepareRefresh(WONDERS_OF_WORLD)
123-
.get();
118+
.indices()
119+
.prepareRefresh(WONDERS_OF_WORLD)
120+
.get();
124121

125122
QueryBuilder qb = QueryBuilders.geoDistanceQuery("location")
126-
.point(29.976, 31.131)
127-
.distance(10, DistanceUnit.MILES);
123+
.point(29.976, 31.131)
124+
.distance(10, DistanceUnit.MILES);
128125

129126
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
130-
.setTypes(WONDERS)
131-
.setQuery(qb)
132-
.execute()
133-
.actionGet();
127+
.setTypes(WONDERS)
128+
.setQuery(qb)
129+
.execute()
130+
.actionGet();
134131
List<String> ids = Arrays.stream(searchResponse.getHits()
135-
.getHits())
136-
.map(hit -> {
137-
return hit.getId();
138-
})
139-
.collect(Collectors.toList());
132+
.getHits())
133+
.map(SearchHit::getId)
134+
.collect(Collectors.toList());
140135
assertTrue(ids.contains(lighthouseOfAlexandriaId));
141136
}
142137

143138
@Test
144139
public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() {
145140
String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}";
146141
IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS)
147-
.setSource(jsonObject)
148-
.get();
142+
.setSource(jsonObject)
143+
.get();
149144
String greatRannOfKutchid = response.getId();
150145
client.admin()
151-
.indices()
152-
.prepareRefresh(WONDERS_OF_WORLD)
153-
.get();
146+
.indices()
147+
.prepareRefresh(WONDERS_OF_WORLD)
148+
.get();
154149

155150
QueryBuilder qb = QueryBuilders.geoPolygonQuery("location")
156-
.addPoint(22.733, 68.859)
157-
.addPoint(24.733, 68.859)
158-
.addPoint(23, 70.859);
151+
.addPoint(22.733, 68.859)
152+
.addPoint(24.733, 68.859)
153+
.addPoint(23, 70.859);
159154

160155
SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD)
161-
.setTypes(WONDERS)
162-
.setQuery(qb)
163-
.execute()
164-
.actionGet();
156+
.setTypes(WONDERS)
157+
.setQuery(qb)
158+
.execute()
159+
.actionGet();
165160
List<String> ids = Arrays.stream(searchResponse.getHits()
166-
.getHits())
167-
.map(hit -> {
168-
return hit.getId();
169-
})
170-
.collect(Collectors.toList());
161+
.getHits())
162+
.map(SearchHit::getId)
163+
.collect(Collectors.toList());
171164
assertTrue(ids.contains(greatRannOfKutchid));
172165
}
173166

174167
@After
175168
public void destroy() {
176169
elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD);
177170
}
178-
179171
}

‎spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchIntegrationTest.java‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
package com.baeldung.spring.data.es;
22

3-
import static java.util.Arrays.asList;
4-
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
5-
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
6-
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
7-
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
8-
import static org.junit.Assert.assertEquals;
9-
import static org.junit.Assert.assertNotNull;
10-
11-
import java.util.List;
12-
3+
import com.baeldung.spring.data.es.config.Config;
4+
import com.baeldung.spring.data.es.model.Article;
5+
import com.baeldung.spring.data.es.model.Author;
6+
import com.baeldung.spring.data.es.service.ArticleService;
137
import org.junit.Before;
148
import org.junit.Test;
159
import org.junit.runner.RunWith;
@@ -22,10 +16,15 @@
2216
import org.springframework.test.context.ContextConfiguration;
2317
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2418

25-
import com.baeldung.spring.data.es.config.Config;
26-
import com.baeldung.spring.data.es.model.Article;
27-
import com.baeldung.spring.data.es.model.Author;
28-
import com.baeldung.spring.data.es.service.ArticleService;
19+
import java.util.List;
20+
21+
import static java.util.Arrays.asList;
22+
import static org.elasticsearch.index.query.MatchQueryBuilder.Operator.AND;
23+
import static org.elasticsearch.index.query.QueryBuilders.fuzzyQuery;
24+
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
25+
import static org.elasticsearch.index.query.QueryBuilders.regexpQuery;
26+
import static org.junit.Assert.assertEquals;
27+
import static org.junit.Assert.assertNotNull;
2928

3029
@RunWith(SpringJUnit4ClassRunner.class)
3130
@ContextConfiguration(classes = Config.class)
@@ -72,21 +71,24 @@ public void givenArticleService_whenSaveArticle_thenIdIsAssigned() {
7271
@Test
7372
public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() {
7473

75-
final Page<Article> articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10));
74+
final Page<Article> articleByAuthorName = articleService
75+
.findByAuthorName(johnSmith.getName(), new PageRequest(0, 10));
7676
assertEquals(2L, articleByAuthorName.getTotalElements());
7777
}
7878

7979
@Test
8080
public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() {
8181

82-
final Page<Article> articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10));
82+
final Page<Article> articleByAuthorName = articleService
83+
.findByAuthorNameUsingCustomQuery("John Smith", new PageRequest(0, 10));
8384
assertEquals(3L, articleByAuthorName.getTotalElements());
8485
}
8586

8687
@Test
8788
public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() {
8889

89-
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")).build();
90+
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*"))
91+
.build();
9092
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
9193

9294
assertEquals(1, articles.size());
@@ -112,7 +114,8 @@ public void givenSavedDoc_whenDelete_thenRemovedFromIndex() {
112114

113115
final String articleTitle = "Spring Data Elasticsearch";
114116

115-
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
117+
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
118+
.withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build();
116119
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
117120
assertEquals(1, articles.size());
118121
final long count = articleService.count();
@@ -124,7 +127,8 @@ public void givenSavedDoc_whenDelete_thenRemovedFromIndex() {
124127

125128
@Test
126129
public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() {
127-
final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)).build();
130+
final SearchQuery searchQuery = new NativeSearchQueryBuilder()
131+
.withQuery(matchQuery("title", "Search engines").operator(AND)).build();
128132
final List<Article> articles = elasticsearchTemplate.queryForList(searchQuery, Article.class);
129133
assertEquals(1, articles.size());
130134
}

0 commit comments

Comments
 (0)