Skip to content

Commit 47d3277

Browse files
committed
DATAES-67 - count request in ElasticsearchTemplate does not respect specified index
1 parent d4f2001 commit 47d3277

File tree

3 files changed

+106
-11
lines changed

3 files changed

+106
-11
lines changed

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

Lines changed: 10 additions & 2 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.
@@ -174,14 +174,22 @@ public interface ElasticsearchOperations {
174174
<T> List<String> queryForIds(SearchQuery query);
175175

176176
/**
177-
* return number of elements found by for given query
177+
* return number of elements found by given query
178178
*
179179
* @param query
180180
* @param clazz
181181
* @return
182182
*/
183183
<T> long count(SearchQuery query, Class<T> clazz);
184184

185+
/**
186+
* return number of elements found by given query
187+
*
188+
* @param query
189+
* @return
190+
*/
191+
<T> long count(SearchQuery query);
192+
185193
/**
186194
* Execute a multiGet against elasticsearch for the given ids
187195
*

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

Lines changed: 27 additions & 9 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.
@@ -239,16 +239,28 @@ public <T> FacetedPage<T> queryForPage(StringQuery query, Class<T> clazz, Search
239239
}
240240

241241
@Override
242-
public <T> long count(SearchQuery query, Class<T> clazz) {
243-
ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz);
244-
CountRequestBuilder countRequestBuilder = client.prepareCount(persistentEntity.getIndexName()).setTypes(
245-
persistentEntity.getIndexType());
246-
if (query.getQuery() != null) {
247-
countRequestBuilder.setQuery(query.getQuery());
242+
public <T> long count(SearchQuery searchQuery, Class<T> clazz) {
243+
String indexName[] = isNotEmpty(searchQuery.getIndices()) ? searchQuery.getIndices().toArray(new String[searchQuery.getIndices().size()]) : retrieveIndexNameFromPersistentEntity(clazz);
244+
String types[] = isNotEmpty(searchQuery.getTypes()) ? searchQuery.getTypes().toArray(new String[searchQuery.getTypes().size()]) : retrieveTypeFromPersistentEntity(clazz);
245+
246+
Assert.notNull(indexName, "No index defined for Query");
247+
248+
CountRequestBuilder countRequestBuilder = client.prepareCount(indexName);
249+
250+
if (types != null) {
251+
countRequestBuilder.setTypes(types);
252+
}
253+
if (searchQuery.getQuery() != null) {
254+
countRequestBuilder.setQuery(searchQuery.getQuery());
248255
}
249256
return countRequestBuilder.execute().actionGet().getCount();
250257
}
251258

259+
@Override
260+
public <T> long count(SearchQuery query) {
261+
return count(query, null);
262+
}
263+
252264
@Override
253265
public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz) {
254266
return resultsMapper.mapResults(getMultiResponse(searchQuery, clazz), clazz);
@@ -704,11 +716,17 @@ private void setPersistentEntityId(Object entity, String id) {
704716
}
705717

706718
private String[] retrieveIndexNameFromPersistentEntity(Class clazz) {
707-
return new String[]{getPersistentEntityFor(clazz).getIndexName()};
719+
if (clazz != null) {
720+
return new String[]{getPersistentEntityFor(clazz).getIndexName()};
721+
}
722+
return null;
708723
}
709724

710725
private String[] retrieveTypeFromPersistentEntity(Class clazz) {
711-
return new String[]{getPersistentEntityFor(clazz).getIndexType()};
726+
if (clazz != null) {
727+
return new String[]{getPersistentEntityFor(clazz).getIndexType()};
728+
}
729+
return null;
712730
}
713731

714732
private List<String> extractIds(SearchResponse response) {

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

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,75 @@ public void shouldIndexSampleEntityWithIndexAndTypeAtRuntime() {
11671167
assertThat(sampleEntities.getTotalElements(), greaterThanOrEqualTo(1L));
11681168
}
11691169

1170+
/*
1171+
DATAES-67
1172+
*/
1173+
@Test
1174+
public void shouldReturnCountForGivenSearchQueryWithGivenIndexUsingSearchQuery() {
1175+
// given
1176+
String documentId = randomNumeric(5);
1177+
SampleEntity sampleEntity = new SampleEntityBuilder(documentId).message("some message")
1178+
.version(System.currentTimeMillis()).build();
1179+
1180+
IndexQuery indexQuery = getIndexQuery(sampleEntity);
1181+
elasticsearchTemplate.index(indexQuery);
1182+
elasticsearchTemplate.refresh(SampleEntity.class, true);
1183+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
1184+
.withQuery(matchAllQuery())
1185+
.withIndices("test-index")
1186+
.build();
1187+
// when
1188+
long count = elasticsearchTemplate.count(searchQuery);
1189+
// then
1190+
assertThat(count, is(equalTo(1L)));
1191+
}
1192+
1193+
/*
1194+
DATAES-67
1195+
*/
1196+
@Test
1197+
public void shouldReturnCountForGivenSearchQueryWithGivenIndexAndTypeUsingSearchQuery() {
1198+
// given
1199+
String documentId = randomNumeric(5);
1200+
SampleEntity sampleEntity = new SampleEntityBuilder(documentId).message("some message")
1201+
.version(System.currentTimeMillis()).build();
1202+
1203+
IndexQuery indexQuery = getIndexQuery(sampleEntity);
1204+
elasticsearchTemplate.index(indexQuery);
1205+
elasticsearchTemplate.refresh(SampleEntity.class, true);
1206+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
1207+
.withQuery(matchAllQuery())
1208+
.withIndices("test-index")
1209+
.withTypes("test-type")
1210+
.build();
1211+
// when
1212+
long count = elasticsearchTemplate.count(searchQuery);
1213+
// then
1214+
assertThat(count, is(equalTo(1L)));
1215+
}
1216+
1217+
/*
1218+
DATAES-67
1219+
*/
1220+
@Test(expected = IllegalArgumentException.class)
1221+
public void shouldThrowAnExceptionWhenNoIndexSpecifiedForCountQuery() {
1222+
// given
1223+
String documentId = randomNumeric(5);
1224+
SampleEntity sampleEntity = new SampleEntityBuilder(documentId).message("some message")
1225+
.version(System.currentTimeMillis()).build();
1226+
1227+
IndexQuery indexQuery = getIndexQuery(sampleEntity);
1228+
elasticsearchTemplate.index(indexQuery);
1229+
elasticsearchTemplate.refresh(SampleEntity.class, true);
1230+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
1231+
.withQuery(matchAllQuery())
1232+
.build();
1233+
// when
1234+
long count = elasticsearchTemplate.count(searchQuery);
1235+
// then
1236+
assertThat(count, is(equalTo(1L)));
1237+
}
1238+
11701239
private IndexQuery getIndexQuery(SampleEntity sampleEntity) {
11711240
return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build();
11721241
}

0 commit comments

Comments
 (0)