mirror of
				https://github.com/spring-projects/spring-data-elasticsearch.git
				synced 2025-10-31 06:38:44 +00:00 
			
		
		
		
	DATAES-67 - count request in ElasticsearchTemplate does not respect specified index
This commit is contained in:
		
							parent
							
								
									d4f2001b46
								
							
						
					
					
						commit
						47d3277bd5
					
				| @ -1,5 +1,5 @@ | |||||||
| /* | /* | ||||||
|  * Copyright 2013 the original author or authors. |  * Copyright 2013-2014 the original author or authors. | ||||||
|  * |  * | ||||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); |  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
|  * you may not use this file except in compliance with the License. |  * you may not use this file except in compliance with the License. | ||||||
| @ -174,7 +174,7 @@ public interface ElasticsearchOperations { | |||||||
| 	<T> List<String> queryForIds(SearchQuery query); | 	<T> List<String> queryForIds(SearchQuery query); | ||||||
| 
 | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * return number of elements found by for given query | 	 * return number of elements found by given query | ||||||
| 	 * | 	 * | ||||||
| 	 * @param query | 	 * @param query | ||||||
| 	 * @param clazz | 	 * @param clazz | ||||||
| @ -182,6 +182,14 @@ public interface ElasticsearchOperations { | |||||||
| 	 */ | 	 */ | ||||||
| 	<T> long count(SearchQuery query, Class<T> clazz); | 	<T> long count(SearchQuery query, Class<T> clazz); | ||||||
| 
 | 
 | ||||||
|  | 	/** | ||||||
|  | 	 * return number of elements found by given query | ||||||
|  | 	 * | ||||||
|  | 	 * @param query | ||||||
|  | 	 * @return | ||||||
|  | 	 */ | ||||||
|  | 	<T> long count(SearchQuery query); | ||||||
|  | 
 | ||||||
| 	/** | 	/** | ||||||
| 	 * Execute a multiGet against elasticsearch for the given ids | 	 * Execute a multiGet against elasticsearch for the given ids | ||||||
| 	 * | 	 * | ||||||
|  | |||||||
| @ -1,5 +1,5 @@ | |||||||
| /* | /* | ||||||
|  * Copyright 2013 the original author or authors. |  * Copyright 2013-2014 the original author or authors. | ||||||
|  * |  * | ||||||
|  * Licensed under the Apache License, Version 2.0 (the "License"); |  * Licensed under the Apache License, Version 2.0 (the "License"); | ||||||
|  * you may not use this file except in compliance with the License. |  * you may not use this file except in compliance with the License. | ||||||
| @ -239,16 +239,28 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public <T> long count(SearchQuery query, Class<T> clazz) { | 	public <T> long count(SearchQuery searchQuery, Class<T> clazz) { | ||||||
| 		ElasticsearchPersistentEntity<T> persistentEntity = getPersistentEntityFor(clazz); | 		String indexName[] = isNotEmpty(searchQuery.getIndices()) ? searchQuery.getIndices().toArray(new String[searchQuery.getIndices().size()]) : retrieveIndexNameFromPersistentEntity(clazz); | ||||||
| 		CountRequestBuilder countRequestBuilder = client.prepareCount(persistentEntity.getIndexName()).setTypes( | 		String types[] = isNotEmpty(searchQuery.getTypes()) ? searchQuery.getTypes().toArray(new String[searchQuery.getTypes().size()]) : retrieveTypeFromPersistentEntity(clazz); | ||||||
| 				persistentEntity.getIndexType()); | 
 | ||||||
| 		if (query.getQuery() != null) { | 		Assert.notNull(indexName, "No index defined for Query"); | ||||||
| 			countRequestBuilder.setQuery(query.getQuery()); | 
 | ||||||
|  | 		CountRequestBuilder countRequestBuilder = client.prepareCount(indexName); | ||||||
|  | 
 | ||||||
|  | 		if (types != null) { | ||||||
|  | 			countRequestBuilder.setTypes(types); | ||||||
|  | 		} | ||||||
|  | 		if (searchQuery.getQuery() != null) { | ||||||
|  | 			countRequestBuilder.setQuery(searchQuery.getQuery()); | ||||||
| 		} | 		} | ||||||
| 		return countRequestBuilder.execute().actionGet().getCount(); | 		return countRequestBuilder.execute().actionGet().getCount(); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	@Override | ||||||
|  | 	public <T> long count(SearchQuery query) { | ||||||
|  | 		return count(query, null); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	@Override | 	@Override | ||||||
| 	public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz) { | 	public <T> LinkedList<T> multiGet(SearchQuery searchQuery, Class<T> clazz) { | ||||||
| 		return resultsMapper.mapResults(getMultiResponse(searchQuery, clazz), clazz); | 		return resultsMapper.mapResults(getMultiResponse(searchQuery, clazz), clazz); | ||||||
| @ -704,11 +716,17 @@ public class ElasticsearchTemplate implements ElasticsearchOperations { | |||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private String[] retrieveIndexNameFromPersistentEntity(Class clazz) { | 	private String[] retrieveIndexNameFromPersistentEntity(Class clazz) { | ||||||
| 		return new String[]{getPersistentEntityFor(clazz).getIndexName()}; | 		if (clazz != null) { | ||||||
|  | 			return new String[]{getPersistentEntityFor(clazz).getIndexName()}; | ||||||
|  | 		} | ||||||
|  | 		return null; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private String[] retrieveTypeFromPersistentEntity(Class clazz) { | 	private String[] retrieveTypeFromPersistentEntity(Class clazz) { | ||||||
| 		return new String[]{getPersistentEntityFor(clazz).getIndexType()}; | 		if (clazz != null) { | ||||||
|  | 			return new String[]{getPersistentEntityFor(clazz).getIndexType()}; | ||||||
|  | 		} | ||||||
|  | 		return null; | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	private List<String> extractIds(SearchResponse response) { | 	private List<String> extractIds(SearchResponse response) { | ||||||
|  | |||||||
| @ -1167,6 +1167,75 @@ public class ElasticsearchTemplateTests { | |||||||
| 		assertThat(sampleEntities.getTotalElements(), greaterThanOrEqualTo(1L)); | 		assertThat(sampleEntities.getTotalElements(), greaterThanOrEqualTo(1L)); | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
|  | 	/* | ||||||
|  | 	DATAES-67 | ||||||
|  | 	 */ | ||||||
|  | 	@Test | ||||||
|  | 	public void shouldReturnCountForGivenSearchQueryWithGivenIndexUsingSearchQuery() { | ||||||
|  | 		// given | ||||||
|  | 		String documentId = randomNumeric(5); | ||||||
|  | 		SampleEntity sampleEntity = new SampleEntityBuilder(documentId).message("some message") | ||||||
|  | 				.version(System.currentTimeMillis()).build(); | ||||||
|  | 
 | ||||||
|  | 		IndexQuery indexQuery = getIndexQuery(sampleEntity); | ||||||
|  | 		elasticsearchTemplate.index(indexQuery); | ||||||
|  | 		elasticsearchTemplate.refresh(SampleEntity.class, true); | ||||||
|  | 		SearchQuery searchQuery = new NativeSearchQueryBuilder() | ||||||
|  | 				.withQuery(matchAllQuery()) | ||||||
|  | 				.withIndices("test-index") | ||||||
|  | 				.build(); | ||||||
|  | 		// when | ||||||
|  | 		long count = elasticsearchTemplate.count(searchQuery); | ||||||
|  | 		// then | ||||||
|  | 		assertThat(count, is(equalTo(1L))); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/* | ||||||
|  | 	DATAES-67 | ||||||
|  | 	 */ | ||||||
|  | 	@Test | ||||||
|  | 	public void shouldReturnCountForGivenSearchQueryWithGivenIndexAndTypeUsingSearchQuery() { | ||||||
|  | 		// given | ||||||
|  | 		String documentId = randomNumeric(5); | ||||||
|  | 		SampleEntity sampleEntity = new SampleEntityBuilder(documentId).message("some message") | ||||||
|  | 				.version(System.currentTimeMillis()).build(); | ||||||
|  | 
 | ||||||
|  | 		IndexQuery indexQuery = getIndexQuery(sampleEntity); | ||||||
|  | 		elasticsearchTemplate.index(indexQuery); | ||||||
|  | 		elasticsearchTemplate.refresh(SampleEntity.class, true); | ||||||
|  | 		SearchQuery searchQuery = new NativeSearchQueryBuilder() | ||||||
|  | 				.withQuery(matchAllQuery()) | ||||||
|  | 				.withIndices("test-index") | ||||||
|  | 				.withTypes("test-type") | ||||||
|  | 				.build(); | ||||||
|  | 		// when | ||||||
|  | 		long count = elasticsearchTemplate.count(searchQuery); | ||||||
|  | 		// then | ||||||
|  | 		assertThat(count, is(equalTo(1L))); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	/* | ||||||
|  | 	DATAES-67 | ||||||
|  | 	 */ | ||||||
|  | 	@Test(expected = IllegalArgumentException.class) | ||||||
|  | 	public void shouldThrowAnExceptionWhenNoIndexSpecifiedForCountQuery() { | ||||||
|  | 		// given | ||||||
|  | 		String documentId = randomNumeric(5); | ||||||
|  | 		SampleEntity sampleEntity = new SampleEntityBuilder(documentId).message("some message") | ||||||
|  | 				.version(System.currentTimeMillis()).build(); | ||||||
|  | 
 | ||||||
|  | 		IndexQuery indexQuery = getIndexQuery(sampleEntity); | ||||||
|  | 		elasticsearchTemplate.index(indexQuery); | ||||||
|  | 		elasticsearchTemplate.refresh(SampleEntity.class, true); | ||||||
|  | 		SearchQuery searchQuery = new NativeSearchQueryBuilder() | ||||||
|  | 				.withQuery(matchAllQuery()) | ||||||
|  | 				.build(); | ||||||
|  | 		// when | ||||||
|  | 		long count = elasticsearchTemplate.count(searchQuery); | ||||||
|  | 		// then | ||||||
|  | 		assertThat(count, is(equalTo(1L))); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
| 	private IndexQuery getIndexQuery(SampleEntity sampleEntity) { | 	private IndexQuery getIndexQuery(SampleEntity sampleEntity) { | ||||||
| 		return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build(); | 		return new IndexQueryBuilder().withId(sampleEntity.getId()).withObject(sampleEntity).build(); | ||||||
| 	} | 	} | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user