Skip to content

Commit 55219c9

Browse files
committed
DATAES-77 - Search across different indices
applied Spring Data coding/formatting rules
1 parent d24a925 commit 55219c9

File tree

12 files changed

+229
-218
lines changed

12 files changed

+229
-218
lines changed

src/main/java/org/springframework/data/elasticsearch/core/DefaultEntityMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class DefaultEntityMapper implements EntityMapper {
3333
public DefaultEntityMapper() {
3434
objectMapper = new ObjectMapper();
3535
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
36-
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
36+
objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
3737
}
3838

3939
@Override

src/main/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplate.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static org.apache.commons.lang.StringUtils.*;
2020
import static org.elasticsearch.action.search.SearchType.*;
2121
import static org.elasticsearch.client.Requests.*;
22-
import static org.elasticsearch.cluster.metadata.AliasAction.Type.ADD;
22+
import static org.elasticsearch.cluster.metadata.AliasAction.Type.*;
2323
import static org.elasticsearch.common.collect.Sets.*;
2424
import static org.elasticsearch.index.VersionType.*;
2525
import static org.springframework.data.elasticsearch.core.MappingBuilder.*;
@@ -32,7 +32,6 @@
3232

3333
import org.apache.commons.collections.CollectionUtils;
3434
import org.elasticsearch.action.admin.cluster.state.ClusterStateRequest;
35-
import org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequestBuilder;
3635
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
3736
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
3837
import org.elasticsearch.action.admin.indices.mapping.delete.DeleteMappingRequest;
@@ -345,7 +344,6 @@ public UpdateResponse update(UpdateQuery query) {
345344
return updateRequestBuilder.execute().actionGet();
346345
}
347346

348-
349347
@Override
350348
public void bulkIndex(List<IndexQuery> queries) {
351349
BulkRequestBuilder bulkRequest = client.prepareBulk();
@@ -563,11 +561,11 @@ private <T> boolean createIndexIfNotCreated(Class<T> clazz) {
563561
}
564562

565563
private <T> boolean createIndexWithSettings(Class<T> clazz) {
566-
if(clazz.isAnnotationPresent(Setting.class)) {
564+
if (clazz.isAnnotationPresent(Setting.class)) {
567565
String settingPath = clazz.getAnnotation(Setting.class).settingPath();
568-
if(isNotBlank(settingPath)) {
566+
if (isNotBlank(settingPath)) {
569567
String settings = readFileFromClasspath(settingPath);
570-
if(isNotBlank(settings)) {
568+
if (isNotBlank(settings)) {
571569
return createIndex(getPersistentEntityFor(clazz).getIndexName(), settings);
572570
}
573571
} else {
@@ -577,7 +575,6 @@ private <T> boolean createIndexWithSettings(Class<T> clazz) {
577575
return createIndex(getPersistentEntityFor(clazz).getIndexName(), getDefaultSettings(getPersistentEntityFor(clazz)));
578576
}
579577

580-
581578
@Override
582579
public boolean createIndex(String indexName, Object settings) {
583580
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);

src/main/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public SimpleElasticsearchPersistentEntity(TypeInformation<T> typeInformation) {
6868
this.refreshInterval = typeInformation.getType().getAnnotation(Document.class).refreshInterval();
6969
this.indexStoreType = typeInformation.getType().getAnnotation(Document.class).indexStoreType();
7070
}
71-
if(clazz.isAnnotationPresent(Setting.class)) {
71+
if (clazz.isAnnotationPresent(Setting.class)) {
7272
this.settingPath = typeInformation.getType().getAnnotation(Setting.class).settingPath();
7373
}
7474
}

src/test/java/org/springframework/data/elasticsearch/core/ElasticsearchTemplateTests.java

Lines changed: 78 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013 the original author or authors.
2+
* Copyright 2013-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,11 +33,11 @@
3333
import org.elasticsearch.search.highlight.HighlightBuilder;
3434
import org.elasticsearch.search.sort.FieldSortBuilder;
3535
import org.elasticsearch.search.sort.SortOrder;
36-
import org.junit.*;
36+
import org.junit.Before;
37+
import org.junit.Ignore;
38+
import org.junit.Test;
3739
import org.junit.runner.RunWith;
3840
import org.springframework.beans.factory.annotation.Autowired;
39-
import org.springframework.data.annotation.Id;
40-
import org.springframework.data.annotation.Version;
4141
import org.springframework.data.domain.Page;
4242
import org.springframework.data.domain.PageRequest;
4343
import org.springframework.data.domain.Pageable;
@@ -75,25 +75,11 @@ public class ElasticsearchTemplateTests {
7575
public void before() {
7676
elasticsearchTemplate.deleteIndex(SampleEntity.class);
7777
elasticsearchTemplate.createIndex(SampleEntity.class);
78-
elasticsearchTemplate.deleteIndex(INDEX_1_NAME);
79-
elasticsearchTemplate.deleteIndex(INDEX_2_NAME);
80-
elasticsearchTemplate.refresh(SampleEntity.class, true);
78+
elasticsearchTemplate.deleteIndex(INDEX_1_NAME);
79+
elasticsearchTemplate.deleteIndex(INDEX_2_NAME);
80+
elasticsearchTemplate.refresh(SampleEntity.class, true);
8181
}
8282

83-
84-
// @After
85-
/** // doing a cleanup to ensure that no indexes are left behind after the test run */
86-
/**
87-
public void after() {
88-
89-
// it is safe to call deleteIndex as it checks for index existance before deleting it
90-
elasticsearchTemplate.deleteIndex(INDEX_NAME);
91-
elasticsearchTemplate.deleteIndex(INDEX_1_NAME);
92-
elasticsearchTemplate.deleteIndex(INDEX_2_NAME);
93-
94-
}**/
95-
96-
9783
@Test
9884
public void shouldReturnCountForGivenSearchQuery() {
9985
// given
@@ -734,7 +720,7 @@ public void shouldReturnListForGivenStringQuery() {
734720
.rate(15)
735721
.version(System.currentTimeMillis()).build();
736722

737-
List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3));
723+
List<IndexQuery> indexQueries = getIndexQueries(Arrays.asList(sampleEntity1, sampleEntity2, sampleEntity3));
738724

739725
// when
740726
elasticsearchTemplate.bulkIndex(indexQueries);
@@ -1047,7 +1033,6 @@ public void shouldAddAliasForVariousRoutingValues() {
10471033
}
10481034

10491035

1050-
10511036
@Test
10521037
public void shouldIndexDocumentForSpecifiedSource() {
10531038

@@ -1527,81 +1512,79 @@ public void shouldCreateGivenSettingsForGivenIndex() {
15271512
}
15281513

15291514

1530-
@Test
1531-
public void shouldTestResultsAcrossMultipleIndices() {
1532-
// given
1533-
String documentId1 = randomNumeric(5);
1534-
SampleEntity sampleEntity1 = new SampleEntityBuilder(documentId1).message("some message")
1535-
.version(System.currentTimeMillis()).build();
1536-
1537-
IndexQuery indexQuery1 = new IndexQueryBuilder().withId(sampleEntity1.getId())
1538-
.withIndexName("test-index-1")
1539-
.withObject(sampleEntity1)
1540-
.build();
1541-
1542-
String documentId2 = randomNumeric(5);
1543-
SampleEntity sampleEntity2 = new SampleEntityBuilder(documentId2).message("some test message")
1544-
.version(System.currentTimeMillis()).build();
1515+
@Test
1516+
public void shouldTestResultsAcrossMultipleIndices() {
1517+
// given
1518+
String documentId1 = randomNumeric(5);
1519+
SampleEntity sampleEntity1 = new SampleEntityBuilder(documentId1).message("some message")
1520+
.version(System.currentTimeMillis()).build();
15451521

1546-
IndexQuery indexQuery2 = new IndexQueryBuilder().withId(sampleEntity2.getId())
1547-
.withIndexName("test-index-2")
1548-
.withObject(sampleEntity2)
1549-
.build();
1522+
IndexQuery indexQuery1 = new IndexQueryBuilder().withId(sampleEntity1.getId())
1523+
.withIndexName("test-index-1")
1524+
.withObject(sampleEntity1)
1525+
.build();
15501526

1551-
elasticsearchTemplate.bulkIndex(Arrays.asList(indexQuery1, indexQuery2));
1552-
elasticsearchTemplate.refresh("test-index-1", true);
1553-
elasticsearchTemplate.refresh("test-index-2", true);
1527+
String documentId2 = randomNumeric(5);
1528+
SampleEntity sampleEntity2 = new SampleEntityBuilder(documentId2).message("some test message")
1529+
.version(System.currentTimeMillis()).build();
15541530

1555-
SearchQuery searchQuery = new NativeSearchQueryBuilder()
1556-
.withQuery(matchAllQuery())
1557-
.withIndices("test-index-1", "test-index-2")
1558-
.build();
1559-
// when
1560-
List<SampleEntity> sampleEntities = elasticsearchTemplate.queryForList(searchQuery, SampleEntity.class);
1531+
IndexQuery indexQuery2 = new IndexQueryBuilder().withId(sampleEntity2.getId())
1532+
.withIndexName("test-index-2")
1533+
.withObject(sampleEntity2)
1534+
.build();
15611535

1562-
// then
1563-
assertThat(sampleEntities.size(), is(equalTo(2)));
1564-
}
1536+
elasticsearchTemplate.bulkIndex(Arrays.asList(indexQuery1, indexQuery2));
1537+
elasticsearchTemplate.refresh("test-index-1", true);
1538+
elasticsearchTemplate.refresh("test-index-2", true);
15651539

1566-
@Test
1567-
/**
1568-
* This is basically a demonstration to show composing entities out of heterogeneous indexes.
1569-
*/
1570-
public void shouldComposeObjectsReturnedFromHeterogeneousIndexes() {
1540+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
1541+
.withQuery(matchAllQuery())
1542+
.withIndices("test-index-1", "test-index-2")
1543+
.build();
1544+
// when
1545+
List<SampleEntity> sampleEntities = elasticsearchTemplate.queryForList(searchQuery, SampleEntity.class);
15711546

1572-
// Given
1547+
// then
1548+
assertThat(sampleEntities.size(), is(equalTo(2)));
1549+
}
15731550

1574-
HetroEntity1 entity1 = new HetroEntity1(randomNumeric(3), "aFirstName");
1575-
HetroEntity2 entity2 = new HetroEntity2(randomNumeric(4), "aLastName");
1551+
@Test
1552+
/**
1553+
* This is basically a demonstration to show composing entities out of heterogeneous indexes.
1554+
*/
1555+
public void shouldComposeObjectsReturnedFromHeterogeneousIndexes() {
15761556

1577-
IndexQuery idxQuery1 = new IndexQueryBuilder().withIndexName(INDEX_1_NAME).withId(entity1.getId()).withObject(entity1).build();
1578-
IndexQuery idxQuery2 = new IndexQueryBuilder().withIndexName(INDEX_2_NAME).withId(entity2.getId()).withObject(entity2).build();
1557+
// Given
15791558

1559+
HetroEntity1 entity1 = new HetroEntity1(randomNumeric(3), "aFirstName");
1560+
HetroEntity2 entity2 = new HetroEntity2(randomNumeric(4), "aLastName");
15801561

1581-
elasticsearchTemplate.bulkIndex(Arrays.asList(idxQuery1, idxQuery2));
1582-
elasticsearchTemplate.refresh(INDEX_1_NAME, true);
1583-
elasticsearchTemplate.refresh(INDEX_2_NAME, true);
1562+
IndexQuery idxQuery1 = new IndexQueryBuilder().withIndexName(INDEX_1_NAME).withId(entity1.getId()).withObject(entity1).build();
1563+
IndexQuery idxQuery2 = new IndexQueryBuilder().withIndexName(INDEX_2_NAME).withId(entity2.getId()).withObject(entity2).build();
15841564

1585-
// When
1565+
elasticsearchTemplate.bulkIndex(Arrays.asList(idxQuery1, idxQuery2));
1566+
elasticsearchTemplate.refresh(INDEX_1_NAME, true);
1567+
elasticsearchTemplate.refresh(INDEX_2_NAME, true);
15861568

1587-
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTypes("hetro").withIndices(INDEX_1_NAME, INDEX_2_NAME).build();
1588-
Page<ResultAggregator> page = elasticsearchTemplate.queryForPage(searchQuery, ResultAggregator.class, new SearchResultMapper() {
1589-
@Override
1590-
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
1591-
List<ResultAggregator> values = new ArrayList<ResultAggregator>();
1592-
for (SearchHit searchHit : response.getHits()) {
1593-
String id = String.valueOf(searchHit.getSource().get("id"));
1594-
String firstName = StringUtils.isNotEmpty((String)searchHit.getSource().get("firstName"))?(String)searchHit.getSource().get("firstName"):"";
1595-
String lastName = StringUtils.isNotEmpty((String)searchHit.getSource().get("lastName"))?(String)searchHit.getSource().get("lastName"):"";
1596-
values.add(new ResultAggregator(id, firstName, lastName));
1597-
}
1598-
return new FacetedPageImpl<T>((List<T>) values);
1599-
}
1600-
});
1569+
// When
16011570

1602-
assertThat(page.getTotalElements(), is(2l));
1571+
SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery()).withTypes("hetro").withIndices(INDEX_1_NAME, INDEX_2_NAME).build();
1572+
Page<ResultAggregator> page = elasticsearchTemplate.queryForPage(searchQuery, ResultAggregator.class, new SearchResultMapper() {
1573+
@Override
1574+
public <T> FacetedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
1575+
List<ResultAggregator> values = new ArrayList<ResultAggregator>();
1576+
for (SearchHit searchHit : response.getHits()) {
1577+
String id = String.valueOf(searchHit.getSource().get("id"));
1578+
String firstName = StringUtils.isNotEmpty((String) searchHit.getSource().get("firstName")) ? (String) searchHit.getSource().get("firstName") : "";
1579+
String lastName = StringUtils.isNotEmpty((String) searchHit.getSource().get("lastName")) ? (String) searchHit.getSource().get("lastName") : "";
1580+
values.add(new ResultAggregator(id, firstName, lastName));
1581+
}
1582+
return new FacetedPageImpl<T>((List<T>) values);
1583+
}
1584+
});
16031585

1604-
}
1586+
assertThat(page.getTotalElements(), is(2l));
1587+
}
16051588

16061589

16071590
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
@@ -1616,22 +1599,17 @@ private List<IndexQuery> getIndexQueries(List<SampleEntity> sampleEntities) {
16161599
return indexQueries;
16171600
}
16181601

1619-
@Document(indexName = INDEX_2_NAME, replicas = 0, shards = 1)
1620-
class ResultAggregator {
1621-
1622-
private String id;
1623-
private String firstName;
1624-
private String lastName;
1625-
1626-
ResultAggregator(String id, String firstName, String lastName) {
1627-
this.id = id;
1628-
this.firstName = firstName;
1629-
this.lastName = lastName;
1630-
}
1631-
1632-
}
1633-
1634-
1602+
@Document(indexName = INDEX_2_NAME, replicas = 0, shards = 1)
1603+
class ResultAggregator {
16351604

1605+
private String id;
1606+
private String firstName;
1607+
private String lastName;
16361608

1609+
ResultAggregator(String id, String firstName, String lastName) {
1610+
this.id = id;
1611+
this.firstName = firstName;
1612+
this.lastName = lastName;
1613+
}
1614+
}
16371615
}

src/test/java/org/springframework/data/elasticsearch/core/geo/ElasticsearchTemplateGeoTests.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingGeoBox() {
187187
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
188188
new Criteria("location").boundedBy(
189189
new GeoBox(new GeoPoint(53.5171d, 0),
190-
new GeoPoint(49.5171d, 0.2062d))));
190+
new GeoPoint(49.5171d, 0.2062d))
191+
)
192+
);
191193
//when
192194
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
193195

@@ -217,7 +219,8 @@ public void shouldFindAuthorMarkersInBoxForGivenCriteriaQueryUsingGeoPoints() {
217219
CriteriaQuery geoLocationCriteriaQuery3 = new CriteriaQuery(
218220
new Criteria("location").boundedBy(
219221
new GeoPoint(53.5171d, 0),
220-
new GeoPoint(49.5171d, 0.2062d)));
222+
new GeoPoint(49.5171d, 0.2062d))
223+
);
221224
//when
222225
List<AuthorMarkerEntity> geoAuthorsForGeoCriteria3 = elasticsearchTemplate.queryForList(geoLocationCriteriaQuery3, AuthorMarkerEntity.class);
223226

src/test/java/org/springframework/data/elasticsearch/core/mapping/SimpleElasticsearchPersistentEntityTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public void shouldThrowExceptionGivenVersionPropertyIsNotLong() throws NoSuchFie
3838
SimpleElasticsearchPersistentProperty persistentProperty = new SimpleElasticsearchPersistentProperty(
3939
EntityWithWrongVersionType.class.getDeclaredField("version"), new PropertyDescriptor("version",
4040
EntityWithWrongVersionType.class), new SimpleElasticsearchPersistentEntity<EntityWithWrongVersionType>(
41-
typeInformation), new SimpleTypeHolder());
41+
typeInformation), new SimpleTypeHolder()
42+
);
4243

4344
// when
4445
new SimpleElasticsearchPersistentEntity(typeInformation).addPersistentProperty(persistentProperty);
@@ -53,13 +54,15 @@ public void shouldThrowExceptionGivenMultipleVersionPropertiesArePresent() throw
5354
EntityWithMultipleVersionField.class.getDeclaredField("version1"), new PropertyDescriptor("version1",
5455
EntityWithMultipleVersionField.class),
5556
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
56-
new SimpleTypeHolder());
57+
new SimpleTypeHolder()
58+
);
5759

5860
SimpleElasticsearchPersistentProperty persistentProperty2 = new SimpleElasticsearchPersistentProperty(
5961
EntityWithMultipleVersionField.class.getDeclaredField("version2"), new PropertyDescriptor("version2",
6062
EntityWithMultipleVersionField.class),
6163
new SimpleElasticsearchPersistentEntity<EntityWithMultipleVersionField>(typeInformation),
62-
new SimpleTypeHolder());
64+
new SimpleTypeHolder()
65+
);
6366

6467
SimpleElasticsearchPersistentEntity simpleElasticsearchPersistentEntity = new SimpleElasticsearchPersistentEntity(
6568
typeInformation);

0 commit comments

Comments
 (0)